我需要获取场景名称或其他关于当前在@给定方法中运行的测试的唯一信息。
在我的测试中,我声明我有一些资源。此资源是从web api中提取/创建的,如下所示:
@given('I have a new article')
def new_article(vcr_fixture):
with vcr_fixture.use_cassette('new_article'): # I need to use different cassette name for different scenarios
return create_new_article()这里的问题是,我可以有多个具有不同参数的场景,并且我希望多次使用相同的资源。在这种情况下,我需要为每个情况下不同的磁带。而且我不能使用这些参数来区分盒式磁带,因为它们可以在创建资源之后应用(例如,添加注释)。我尝试将请求添加到@给定的fixture,但在其中找不到任何唯一的信息。
发布于 2016-12-09 06:14:59
要检查您的方案名称,您可以使用解析器。
from pytest_bdd import parsers
...
@given(parsers.parse('I have a {article_name}')
def new_article(vcr_fixture, article_name):
with vcr_fixture.use_cassette(article_name):
return create_new_article()我不确定这是否能回答你的问题..
发布于 2018-08-17 19:42:55
nAlthough user2292262的建议是有效的,但它并不像人们开始学习pytest-bdd时所期望的那样工作(至少是我所期望的)。
解决方案AIUI是为您的测试中的每一篇文章创建一个fixture,而不是自动完成(即,为您动态创建所需数据的函数)。
这在测试环境中应该是合理可行的,因为您拥有的数据集不是真正的数据集,而是一个子集,并且您拥有的作为pre(给定动词)的数据应该更少。如果无法控制,您可以考虑使用脚本填充fixture,方法是查询DB并相应地编写一个模板化文件。
@given(parsers.parse('I have an article called foo')
def article_foo(vcr_fixture):
with vcr_fixture.use_cassette('foo'):
return create_new_article()
@given(parsers.parse('I have an article called bar')
def article_bar(vcr_fixture):
with vcr_fixture.use_cassette('bar'):
return create_new_article()
etc.另一个解决方案的“工厂”代码中的问题
@given(parsers.parse('I have a {article_name}')
def new_article(vcr_fixture, article_name):
with vcr_fixture.use_cassette(article_name):
return create_new_article()你仍然想把它当做
Given I have an article called Foo
And I have an article called Bar
When I edit Foo
Then Bar is untouched但是这种情况行不通,因为在幕后,您实际上使用了两次相同的fixture,但是使用了不同的“内容”。
换句话说,使用该代码,某人将“new_article”两个“data”实例。这违背了pytest的喜好。
此外,从纯粹的观点来看,BDD中的给定动词应该是事实,而不是创建事实的东西。
https://stackoverflow.com/questions/40343838
复制相似问题