[英]Pytest - how to link a specific setup and teardown functions to a specific test function?
[英]How to run a specific function at the end of a specific scenario test in pytest BDD using fixtures
背景我需要在针对特定测试场景运行场景后运行一些特定方法
我试过的
场景如下
Scenario: Test Fixture
Given I am a mechanic
When I start a car
Then I should get to know the primitive issues
步骤定义如下所示
@pytest.mark.usefixtures("stop_car")
@scenario('../FeatureFiles/Test.feature', 'Test Fixture')
def test_mechanic():
logging.info('Test Mechanic')
@given("I am a mechanic")
def given_mechanic():
print('given_mechanic')
@when("I start a car")
def when_mechanic():
print('when_mechanic')
@then("I should get to know the primitive issues")
def then_mechanic():
print('then_mechanic')
assert 1 < 0, 'Failed validation'
@pytest.fixture
def stop_car():
print('stop car')
面临的问题
这里的问题是 'stop_car()' function 在场景执行之前被触发。 我需要在场景结束时运行。 即使任何断言在 Given、When 或 Then 中失败,在任何情况下都应执行方法“stop_car()”
任何需要的固定装置都会在需要时在测试之前运行。 将其视为需要在测试之前运行(固定)的东西,并在测试中使用它的结果。 如果您不需要yield
,您可以将控制权交给主脚本。 该部分完成后,运行 function 的 rest。
@pytest.fixture
def stop_car():
print('this runs before scenario')
yield
print('stop car')
这是相关文件
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.