python pytest Advanced conftest. py Detailed Explanation

  • 2021-07-03 00:35:38
  • OfStack

Preface

The previous articles have basically understood the use of pytest commands, collection of use cases, finxture use and scope of action, today a brief introduction to the role of 1 conftest. py file and the actual project such as the use of this file!

Instance scenario

First of all, we think about such a question: If when we write tests, every use case in a test file needs to be logged in before completing the following operations, then how should we achieve it? This requires us to master the use of conftest. py file.

Instance code

Create the following 1 directory


ConftestFile
|conftest.py
|test_file_01.py
|test_file_02.py
|__init__.py
# conftest.py
import pytest
@pytest.fixture()
def login():    
    print('\n---------------conftest Documents login Method starts execution ----------------------------')
    print('login in conftest.py')
    print('----------------conftest.py Documents login End of method execution ---------------------------')
# test_file_01.py
def test_01(login):
    print('\n------------------ Use case file 1 Test case 1 Start execution ------------------')
    print('login after : in test_file_01->case test_01')
    print('------------------- Use case file 1 Test case 1 End of execution ------------------------')
# test_file_02.py
def test_02(login):
    print('\n------------------ Use case file 2 Test case 2 Start execution ------------------')
    print('login after : in test_file_01->case test_01')
    print('------------------- Use case file 2 Test case 2 End of execution ------------------------')

Let's first run the example code 1 to see the output

1. You can right-click the directory to run in pycharm

2. Enter pytest-vs in the cmd directory to run


test_file_01.py 
---------------conftest Documents login Method starts execution ----------------------------
login in conftest.py
----------------conftest.py Documents login End of method execution ---------------------------
.
------------------ Use case file 1 Test case 1 Start execution ------------------
login after : in test_file_01->case test_01
------------------- Use case file 1 Test case 1 End of execution ------------------------
[ 50%]
test_file_02.py 
---------------conftest Documents login Method starts execution ----------------------------
login in conftest.py
----------------conftest.py Documents login End of method execution ---------------------------
.
------------------ Use case file 2 Test case 2 Start execution ------------------
login after : in test_file_01->case test_01
------------------- Use case file 2 Test case 2 End of execution ------------------------
[100%]
========================== 2 passed in 0.04 seconds ===========================

You can see that the login method in conftest. py file is executed before the test cases of each test file are executed. Through this mode, we can prepare the preconditions of test cases!

The conftest file needs to be used in combination with fixture in practical application, so the parameter scope in fixture is also applicable to the characteristics of fixture in conftest, which will be explained below

1. If the scope parameter of fixture in conftest is session, then all test files are executed once before execution

2. If the scope parameter of fixture in conftest is module, fixture in conftest file will be executed once before every test file is executed

3. If the scope parameter of fixture in conftest is class, fixture in conftest file will be executed once before the test class in each test file is executed

4. If the scope parameter of fixture in conftest is function, fixture in conftest file will be executed once before the test cases of all files are executed

Summarize

Theory often needs to be verified by practice, so if you want to master the specific use of conftest, you need to use more code verification! My above code only verifies that the test function in the test file uses conftest. py. In actual work, not only the function is used, but also there is often not only an conftest. py file. The following is my summary of the characteristics, I hope it will be helpful to everyone!

1. conftest. py file name is fixed and cannot be modified

2. Files and use case files are in the same directory, then conftest. py works on the entire directory

3. The directory where the conftest. py file is located must have the __init__. py file

4. conftest. py file cannot be imported by another file

5. The conftest. py file will be executed before all test files in the same directory are run


Related articles: