pytest 中用例需要多个前置(conftest)、显示setup和teardown执行步骤
conftest.py
import pytest
@pytest.fixture()
def test_1():
    print('\n')
    print('=============前置------test1=============')
@pytest.fixture()
def test_2():
    print('==========前置==test2============')
    yield
    print('\n')
    print('==========后置==test2============')
test_fun.py
def test_fun1(test_1, test_2):
    print('=--------------test-fun1--------------')
输出结果
collected 1 item                                                                                                                                                          
test_fun.py 
=============前置------test1=============
==========前置==test2============
=--------------test-fun1--------------
.
==========后置==test2============
我们可以使用 --setup-show 来查看详细的setup和teardown执行顺序
输出结果
=========================================================================== test session starts ===========================================================================
platform linux -- Python 3.7.3, pytest-6.2.4, py-1.10.0, pluggy-0.13.1
rootdir: /home/uniontech/Desktop/pytest_test
plugins: anyio-3.6.1, metadata-1.11.0, repeat-0.9.1, rerunfailures-10.2, allure-pytest-2.8.6, html-3.1.1
collected 1 item                                                                                                                                                          
test_fun.py 
        SETUP    F test_1
        SETUP    F test_2
        test_fun.py::test_fun1 (fixtures used: test_1, test_2).
        TEARDOWN F test_2
        TEARDOWN F test_1
============================================================================ 1 passed in 0.01s ============================================================================
{11:09}~/Desktop/pytest_test ➭ 
 
                    
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号