2

異なるファイルに次のテストケースがあるとしましょう

  • TestOne.py {タグ: One、Two}
  • TestTwo.py {タグ: 2}
  • TestThree.py {タグ: 3}

それぞれが unittest.TestCase を継承しています。これらのタグを検索してそれらのテストケースのみを実行するための main.py スクリプトを使用できるように、これらのファイルにメタデータ情報を埋め込む機能は Python にありますか?

例: {tags: Two} でテストケースを実行したい場合は、テストケース TestOne.py と TestTwo.py のみを実行する必要があります。

4

2 に答える 2

2

もちろん、メイン モジュールでタグを定義する方が簡単ですが、タグをテスト ファイルと共に保存することが重要な場合は、次のようにテスト ファイルでタグを定義することをお勧めします。

TestOne.py で:

test_tags = ['One', 'Two']
...

initialize次に、この方法でメイン モジュールの関数内のすべてのタグを読み取ることができます。

test_modules = ['TestOne', 'TestTwo', 'TestThree']
test_tags_dict = {}

def initialize():
    for module_name in test_modules:
        module = import_string(module)

        if hasattr(module, 'test_tags'):
            for tag in module.test_tags:
                if tag not in test_tags_dict:
                    test_tags_dict[tag] = []
                test_tags_dict[tag].append(module)

したがってrun_test_with_tag、特定のタグのすべてのテストを実行する関数を実装できます。

def run_test_with_tag(tag):
    for module in test_tags_dict.get(tag, []):
        # Run module tests here ...
于 2013-04-11T06:19:22.837 に答える