preamble
PreviouswritingsBasically have understood the pytest command to use, collect use cases, finxture use and scope of action, today a brief introduction to the role of the file and the actual project such as the use of this file!
Example scenarios
First of all, let's think about this question: if we are writing tests, each use case in the test file needs to be logged in first before completing the later operations, then how do we realize it? This requires us to master the use of files.
sample code (computing)
Create a directory as follows
ConftestFile | |test_file_01.py |test_file_02.py |__init__.py # import pytest @() def login(): print('\n---------------conftest filelogin method start execution ----------------------------') print('login in ') print('End of execution of the login method of the ---------------- file ---------------------------') # 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 run the example code to see the output first
1. In pycharm you can right click on the directory to run the
2. You can run pytest by typing pytest -vs in the cmd directory
test_file_01.py ---------------conftestfileloginmethod starts executing---------------------------- login in ----------------fileloginEnd of method execution--------------------------- . ------------------用例file1test case1commence------------------ login after : in test_file_01->case test_01 -------------------用例file1test case1End of implementation------------------------ [ 50%] test_file_02.py ---------------conftestfileloginmethod starts executing---------------------------- login in ----------------fileloginEnd of method execution--------------------------- . ------------------用例file2test case2commence------------------ login after : in test_file_01->case test_01 -------------------用例file2test case2End of implementation------------------------ [100%] ========================== 2 passed in 0.04 seconds ===========================
You can see that each test file of the test case execution before the execution of the file's login method, through this pattern we can realize the test case preconditions for the preparation of the work!
The actual application of the conftest file needs to be combined with the fixture to use, then the parameter scope in the fixture also applies to the characteristics of the fixture in the conftest, here to explain again
If the scope parameter of the fixture is session, then all the test files will be executed once before the execution.
If the scope parameter of the fixture is module, then the fixture in the conftest file will be executed once before each test file is executed.
If the scope parameter of the fixture is class, then the fixture in the conftest file will be executed once before the execution of the test class in each test file.
If the scope parameter of the fixture is function, then the fixture in the conftest file will be executed once before the test cases in all files are executed.
summarize
Theory often need to practice the verification, so to master the specific use of conftest, but also need to use more code verification! My code above just verify the test file to test the use of functions, the actual work is not only only the use of functions, but also often not only the existence of a file. The following is my summary of the characteristics, I hope to help you it!
The name of the file is fixed and cannot be modified in any way
2. file and use case files in the same directory, then the role of the entire directory
The __init__.py file must exist in the directory where the file is located
Files cannot be imported by other files
5. All test files in the same directory will be executed before running the file
This is the whole content of this article.