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