这可能是由于Allure报告无法正确处理pytest的收集器(collector)而导致的。可以使用pytest传递--collectonly
和--json
选项来获取测试用例的JSON表示形式。然后,使用Allure API中的testResultContainer
和testResult
对象动态地创建测试套件和测试用例。下面是一个示例代码:
import json
import allure
def load_test_cases():
with open('test_cases.json') as f:
return json.load(f)
def test_something():
allure.dynamic.description('Something happens')
if __name__ == '__main__':
test_cases = load_test_cases()
suite = allure.testPlan('My Test Plan')
for case in test_cases:
test_case = allure.testCase(case['name'])
test_case.title(case['title'])
test_case.description(case['description'])
test_case.epic(case['epic'])
test_case.feature(case['feature'])
suite.append(test_case)
allure.attach('Test Plan', json.dumps(suite.to_dict(), indent=4), allure.attachment_type.JSON)
这个示例代码假定有一个名为test_cases.json的文件,其中包含测试用例及其元数据的JSON表示形式。在示例中,使用allure.dynamic.description()
方法动态地设置测试用例的描述。然后使用Allure API中的其他对象设置其他元数据,例如标题、史诗、特征等。最后,在__main__
中,加载测试用例并使用testResultContainer
对象创建测试套件。使用suite.append()
方法将testResult
对象添加到测试套件中,并使用allure.attach()
方法将测试套件JSON表示形式附加到Allure报告中。