Table of contents
1. The default test case rules
2. Execution order of test cases
3.1.1. Main function mode: 4 operation modes
3.1.2. The file framework is as shown below
3.2.1. Command line mode: 4 operation modes
3.2.2. Framework of the second operation mode
3.3. Run by reading the configuration file pytest.ini
3.3.1.pytest.ini file considerations
3.3.2.pytest.ini file interpretation
3.3.3.pytest.ini file source code
1. The default test case rules
1.1. Module name [test_login]: must start with test_ or end with _test 1.2. Test class [Testlogin]: must start with Test, and cannot have an init method 1.3. Test method [test_01_baili]: must start with test 1.4. All The package package must have an __init__.py file
2. Execution order of test cases
unittest: Absolute execution order according to the size of the ASCII code
pytest: top to bottom by default
Change the default execution order of use cases: use mark mark
3. How to run the test case
For details on the meaning of running parameters, see: Analysis of Common Parameters Running in the Pytest Framework_Momo 18S Blog-CSDN Blog
3.1. Main function mode
if __name__ == '__main__':
pytest.main(['-vs','-m smoke','test_01_baili'])
3.1.1. Main function mode: 4 operation modes
#1. Run all pytest.main() #2. Specify module [file] pytest.main(['-vs','test_login.py']) #3. Specify directory folder pytest.main(['-vs ','./interface_testcase']) #4. Specify the directory folder 2; specify the use case through nodeid to run: nodeid is composed of module name, separator, class name, method name, and function name Execution function: no class name pytest.main (['-vs', './interface_testcase/test_interface.py::test_01_func']) execute method pytest.main(['-vs', './interface_testcase/test_interface.py::TestInterface::test_01'])
3.1.2. The file framework is as shown below
3.2. Command line mode
3.2.1. Command line mode: 4 operation modes
#1. Run all files: pytest #2. Execute the [smoke] use case of a certain file; expressions can also be used after -m, adding and, or, not keywords between tags pytest -vs test_login.py pytest -v -m smoke test_login.py #3. Specify the directory folder pytest -vs ./interface_testcase #4. Specify the directory folder 2; specify the use case through nodeid to run: nodeid consists of module name, separator, class name, method name, The function name constitutes the execution function: no class name pytest -vs ./interface_testcase/test_interface.py::test_01_func execution method pytest -vs ./interface_testcase/test_interface.py::TestInterface::test_01
3.2.2. Framework of the second operation mode
Remarks : The framework of the fourth operating mode is the same as that of the main function mode
3.3. Run by reading the configuration file pytest.ini
3.3.1.pytest.ini file considerations
pytest.ini is generally used in the implementation of enterprise automation testing, and is also the core configuration file of the pytest unit testing framework
- Location: generally placed in the root directory of the project;
- name: must be pytest.ini
- Encoding format: must be ANSI, you can use notpad++ to modify the encoding format
- Function: You can change the default test case rules of pytest
- Running rules: Whether it is running in the mode of the main function or running in the command line mode, it will read the configuration file pytest.ini
3.3.2.pytest.ini file interpretation
Note: An error will be reported when executing with Chinese. After removing all Chinese, you can use notpad++ to modify the encoding format
3.3.3.pytest.ini file source code
[pytest]
addopts = -vs -m "smkoe"
testpaths = ./testcase/
python_files = test_*.py
python_classes = test*
python_functions = test
Remarks: Modify the naming rules of [pytest.ini file source code] module name, class name, and method name. The names of corresponding modules, classes, and methods also need to be modified according to the rules of pytest.ini