简体   繁体   English

如何在 pytest BDD 使用固定装置的特定场景测试结束时运行特定的 function

[英]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.

相关问题 pytest-如何将特定的设置和拆卸功能链接到特定的测试功能? - Pytest - how to link a specific setup and teardown functions to a specific test function? 如何使用 pytest 以特定顺序运行目录中的所有测试脚本文件 - How to run all test script file inside a directory in a specific order using pytest PyTest 参数化夹具? 如何将一个大测试function拆分成几个 - PyTest parameterizing fixtures? How to split a big test function into several 使用两个不同的 pytest 夹具运行测试 - Run a test with two different pytest fixtures 在自定义场景运行器期间的第一次测试中刷新 pytest 固定装置 - Refreshing pytest fixtures in first test during custom scenario runner 在特定 pytest 标记上禁用自动使用装置 - Disable autouse fixtures on specific pytest marks 如何在示例中使用 pytest bdd 为场景语句传递相同参数的不同值 - How to pass different values for same parameter in Example using pytest bdd for scenario statement 如何在 Python 中测试特定的日志记录格式(使用 `pytest` 和 `unittest`) - How to test specific logging formatting in Python (using `pytest` and `unittest`) pytest bdd通过@scenario模块 - pytest bdd pass @scenario module 如何在pytest中使用tox仅运行特定目录? - How to run only specific directory using tox in pytest?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM