使用pytest运行测试时allure报告出现fixture 'result_type'未找到错误
问题原因
pytest会自动将测试函数(函数名以test开头)的入参识别为需要注入的fixture,你当前没有定义名为result_type的fixture,因此触发报错。
解决方案
可根据你的实际使用场景选择以下任意一种方案:
- 方案1:定义
result_typefixture
如果你需要在多个测试用例中复用result_type的值,可以直接在测试文件或者项目根目录的conftest.py中添加fixture定义,示例代码如下:
import pytest @pytest.fixture def result_type(): # 这里返回你实际需要的结果类型,比如"pass"或者"fail" return "pass"
如果需要动态获取测试用例的执行结果作为result_type的值,可以修改fixture实现逻辑获取测试上下文的执行状态。
方案2:修改函数名,避免被pytest识别为测试用例
如果testResultPngToPdf本身是工具函数,不需要pytest独立执行,直接把函数名修改为非test开头即可,比如改为generate_result_pdf,之后你在其他测试逻辑中调用该函数时自行传入result_type参数即可,pytest不会再将其识别为测试用例校验fixture。方案3:使用参数化装饰器传入参数
如果你确实需要将该函数作为独立测试用例执行,且需要传入不同的result_type值验证逻辑,可以使用pytest的参数化装饰器直接传值,不需要额外定义fixture:
import pytest import allure @allure.step("Generating Test Results") @pytest.mark.parametrize("result_type", ["pass", "fail"]) def testResultPngToPdf(result_type): '''Function call to make pdf for all screen shots taken during execution''' logging.info(f"Total Screenshots captured : {len(screenshots_list)}") logging.info(f"Result type value : {result_type}") screenshots_list[-1] = pass_fail_watermark(result_type, screenshots_list[-1]) pdf_filename = ("TestResults/"+scenario_to_run+"_" +current_time+"_"+result_type+".pdf") screenshots_list[0].save(pdf_filename, "PDF", resolution=100.0, save_all=True, append_images=screenshots_list[1:]) screenshots_list.clear()
内容的提问来源于stack exchange,提问作者Kajal M
相关产品推荐
相关产品推荐

