将参数从 WHEN 传递给 THEN
Passing parameter from WHEN to a THEN
如何在 pytest bdd 中将参数从 WHEN 传递到 THEN?
例如,如果我有以下代码:
@when('<n1> is a number divisible by 10')
def n1_is_a_number_divisible_by_10(n1):
assert (n1 % 10) == 0
newN1 = n1/10
return newN1
@then('the result will also be divisible by 3')
def the_result_will_also_be_divisible_by_3(newN1):
assert newN1 % 3 == 0
如何将 newN1 从 when 传递到 then?
(我试过将 newN1 设置为全局变量...这行得通,但在 python 中经常不赞成将其设置为全局变量)。
您不能通过返回将任何内容从 "when" 传递到 "then"。通常你想避免这种情况。但是,如果必须,请在两个步骤中使用 pytest fixture 作为消息框:
@pytest.fixture(scope='function')
def context():
return {}
@when('<n1> is a number divisible by 10')
def n1_is_a_number_divisible_by_10(n1, context):
assert (n1 % 10) == 0
newN1 = n1 / 10
context['partial_result'] = newN1
@then('the result will also be divisible by 3')
def the_result_will_also_be_divisible_by_3(context):
assert context['partial_result'] % 3 == 0
"context" 夹具的结果被传递到 "when" 步骤,填充,然后传递到 "then" 部分。它不是每次都重新创建。
作为旁注,测试您的测试不是最佳选择 - 您在 "when" 部分这样做,断言可分性。但这只是我猜的一些示例代码。
在 pytest-bdd
version 4.1.0 中,when
步骤现在像 given
步骤一样支持 target_fixture
。
的示例
# test_blog.py
from pytest_bdd import scenarios, given, when, then
from my_app.models import Article
scenarios("blog.feature")
@given("there is an article", target_fixture="article")
def there_is_an_article():
return Article()
@when("I request the "
"deletion of the article", target_fixture="request_result") # <--- here
def there_should_be_a_new_article(article, http_client):
return http_client.delete(f"/articles/{article.uid}")
@then("the request should be successful")
def article_is_published(request_result):
assert request_result.status_code == 200
如何在 pytest bdd 中将参数从 WHEN 传递到 THEN?
例如,如果我有以下代码:
@when('<n1> is a number divisible by 10')
def n1_is_a_number_divisible_by_10(n1):
assert (n1 % 10) == 0
newN1 = n1/10
return newN1
@then('the result will also be divisible by 3')
def the_result_will_also_be_divisible_by_3(newN1):
assert newN1 % 3 == 0
如何将 newN1 从 when 传递到 then?
(我试过将 newN1 设置为全局变量...这行得通,但在 python 中经常不赞成将其设置为全局变量)。
您不能通过返回将任何内容从 "when" 传递到 "then"。通常你想避免这种情况。但是,如果必须,请在两个步骤中使用 pytest fixture 作为消息框:
@pytest.fixture(scope='function')
def context():
return {}
@when('<n1> is a number divisible by 10')
def n1_is_a_number_divisible_by_10(n1, context):
assert (n1 % 10) == 0
newN1 = n1 / 10
context['partial_result'] = newN1
@then('the result will also be divisible by 3')
def the_result_will_also_be_divisible_by_3(context):
assert context['partial_result'] % 3 == 0
"context" 夹具的结果被传递到 "when" 步骤,填充,然后传递到 "then" 部分。它不是每次都重新创建。
作为旁注,测试您的测试不是最佳选择 - 您在 "when" 部分这样做,断言可分性。但这只是我猜的一些示例代码。
在 pytest-bdd
version 4.1.0 中,when
步骤现在像 given
步骤一样支持 target_fixture
。
# test_blog.py
from pytest_bdd import scenarios, given, when, then
from my_app.models import Article
scenarios("blog.feature")
@given("there is an article", target_fixture="article")
def there_is_an_article():
return Article()
@when("I request the "
"deletion of the article", target_fixture="request_result") # <--- here
def there_should_be_a_new_article(article, http_client):
return http_client.delete(f"/articles/{article.uid}")
@then("the request should be successful")
def article_is_published(request_result):
assert request_result.status_code == 200