pytest-bdd步骤定义无法读取参数,误报找不到fixture问题
问题解决:pytest-bdd 报错 fixture 'phrase' not found
问题原因
你的Feature文件中Given语句带引号("test" the application),但步骤定义的解析模板未包含引号,导致pytest-bdd无法正确提取参数,反而将phrase识别为需要注入的fixture,进而报错。
解决方案
有两种可行的修正方式:
方式1:修正步骤定义的解析模板(保留Feature中的引号)
修改步骤定义文件,将解析模板中的引号包含进去,确保能正确捕获参数:
from pytest_bdd import given, parsers, scenario scenario('../features/test_app.feature', 'Publishing the article') @given(parsers.parse('"{phrase}" the application')) def test_app(phrase): print(phrase)
方式2:修改Feature文件(移除引号)
如果不需要在Feature中保留引号,直接修改Given语句,同时保持步骤定义不变:
Feature: Blog A site where you can publish your articles. Scenario: Publishing the article Given test the application
原理说明
pytest-bdd会优先尝试从步骤语句中提取参数,当无法匹配到对应的参数规则时,会默认将步骤函数的参数视为需要依赖的fixture。由于你没有定义名为phrase的fixture,因此触发了fixture 'phrase' not found错误。
内容的提问来源于stack exchange,提问作者Saranath Balasundaram
相关产品推荐
相关产品推荐

