简体   繁体   English

pytest-如何将特定的设置和拆卸功能链接到特定的测试功能?

[英]Pytest - how to link a specific setup and teardown functions to a specific test function?

Similar questions have been asked but none of the answers solved my problem: 有人提出了类似的问题,但没有一个答案解决了我的问题:

I have the following 3 functions: 我具有以下3个功能:

def specific_setup_function():
    print("\n\nsetup_function\n\n")


def specific_teardown_function():
    print("\n\nteardown_function\n\n")


def specific_test():
    print('\n\nTEST\n\n')

How do i make pytest execute specific_test() right after specific_setup_function and right before specific_teardown_function() , regardless of how many other functions there are in my test module ? 如何让我pytest执行specific_test()之后specific_setup_function和前右specific_teardown_function()无论有多少其他的功能也有在我的测试模块?

In this example it will execute in the specified order because these are the only functions but in the more general case i want the previous code to be equivalent to the following, ALWAYS: 在此示例中,它将按指定的顺序执行,因为这些是唯一的函数,但在更一般的情况下,我希望以前的代码始终等效于以下代码:

def test():
    specific_setup_function()
    print('\n\nTEST\n\n')
    specific_teardown_function()

So specific_test will be linked to specific_setup_function and specific_teardown_function . 因此, specific_test将链接到specific_setup_functionspecific_teardown_function

Is there a way ? 有办法吗?

You'll want to use fixtures with a finalizer . 您将需要使用带有finalizer的fixture

@pytest.fixture
def specific_fixture(request):
    print("\n\nsetup_function\n\n")
    def fin():
        print("\n\nteardown_function\n\n")
    request.addfinalizer(fin)
    return 6  # You can return a value from a fixture


def specific_test(specific_fixture):
    print('\n\nTEST: %s\n\n' % specific_fixture)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在pytest中为特定方法或功能定义设置和拆卸功能? - How can I define a setup and teardown function for a specific method or function in pytest? 如何在python unittest中指定测试特定的设置和拆卸 - how to specify test specific setup and teardown in python unittest Pytest 设置和拆卸功能 - 与自己编写的功能相同吗? - Pytest setup and teardown functions - same as self written functions? 如何在另一个文件 test_1.py 中处理 pytest 的设置和拆卸方法中生成的 session - How to handle session which is generated in the setup and teardown methods in pytest in another file test_1.py 如何在 pytest 中的 setup_function 和 teardown_function 中使用夹具 - How to use fixtures within setup_function and teardown_function in pytest ScopeMismatch 当我尝试进行 setup_teardown pytest function - ScopeMismatch when i try to make setup_teardown pytest function 如何在 pytest BDD 使用固定装置的特定场景测试结束时运行特定的 function - How to run a specific function at the end of a specific scenario test in pytest BDD using fixtures 用于会话的Pytest设置/拆卸挂钩 - Pytest setup/teardown hooks for session 在 pytest 中使用 conftest 进行设置/拆卸 - setup/teardown using conftest in pytest 使用pytest进行设置和拆卸 - Using pytest to do setup and teardown
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM