我正在编写一些pytest测试文件,这些文件附加到没有示例或步骤表的功能文件中。我不明白的是如何使用内联变量(USER1和USER2),它们是给定的、When和Then步骤中的字符串(下面简单的When示例)以便第一次执行“When”步骤时使用John,第二次使用“When”步骤时使用Peter。在
我一直在读这些文档http://pytest-bdd.readthedocs.io/en/latest/#step-arguments以及它说的使用解析器?也许我误解了文件,但我不太清楚我该如何做下面的事情。也许用户需要在dict中?{'user1':'John','user2':'Peter'}。我知道在特性文件中使用examples表或steps表会很好,但是在这种情况下,我需要知道如何在后台(仅在pytest文件中)这样做。在
提前谢谢大家
USER1 = 'John'
USER2 = 'Peter'
@scenario('User logs in')
def test_user_logs_in():
"""User logs in to website."""
pass
@given('I go to a website')
def I_go_to_a_website():
Do something
@When('{user} logs in')
def user_logs_in(user):
Do something with user1 the first time this step is used
Do something with user2 the second time this step is used.
@then('I should see the account page')
def should_see_account_page():
Do something
如果您想用USER1运行所有测试,然后用USER2运行一次,那么您需要的是parametrized tests。在
本质上,定义测试一次。然后用一组可选的id在元组中定义一组变量,pytest将对每组参数运行一次该测试。在
让我们举一个基本的例子。在
而不是用不同的参数反复定义测试,比如:
^{pr2}$您可以使用
pytest.mark.parametrize
修饰符:运行此测试时,将看到以下输出。在
所以,回到你的问题上,你想做这样的事情:
如果您的列表很长,最好使用fixture。在
我认为这是一种更容易管理的做事方式。你也可以参数化你的固定装置,这样魔法就非常深入了。在
相关问题 更多 >
编程相关推荐