[英]How to run a specific function at the end of a specific scenario test in pytest BDD using fixtures
Background I need to run some specific method after a scenario run for a specific test scenario背景我需要在针对特定测试场景运行场景后运行一些特定方法
What I tried我试过的
Scenario is as below场景如下
Scenario: Test Fixture
Given I am a mechanic
When I start a car
Then I should get to know the primitive issues
Step definition looked as below步骤定义如下所示
@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')
Issue Faced面临的问题
The problem here is the 'stop_car()' function is triggered before the execution of the scenario.这里的问题是 'stop_car()' function 在场景执行之前被触发。 I need to run at the end of the scenario.我需要在场景结束时运行。 Even if any assertion failed in Given, When or Then the method 'stop_car()' should be executed in any case即使任何断言在 Given、When 或 Then 中失败,在任何情况下都应执行方法“stop_car()”
Any required fixture is run ahead of the test when it's required.任何需要的固定装置都会在需要时在测试之前运行。 Think about it as something which needs to be run (fixed) before the test and in the test you use the result of that.将其视为需要在测试之前运行(固定)的东西,并在测试中使用它的结果。 If you do not need the result you can just yield
the control to main script.如果您不需要yield
,您可以将控制权交给主脚本。 Once that part is finished the rest of the function is run.该部分完成后,运行 function 的 rest。
@pytest.fixture
def stop_car():
print('this runs before scenario')
yield
print('stop car')
here is the relevant documentation这是相关文件
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.