1. Pytest installation
In the CMD command window, execute the Pytest test framework installation command: pip install -U pytest
(it means that if it is not installed, it will be installed. If it is installed, it will be upgraded to the latest version)
Check whether the pytest test framework is successfully installed in CMD: pytest – - version
C:\Users\L>pytest --version pytest 6.1.2
2. Pytest common plug-ins
Pytest has many powerful third-party plug-ins, and these plug-ins can realize many practical operations, and you can also customize pytest plug-ins.
Easy to use, such as:
- Pytest Selenium: integrate Selenium.
- Pytest html: generate automated test reports in html format.
- Pytest rerunfailures: repeated execution of failed case s.
- Pytest xdist: distributed execution of test cases, or multi CPU distribution.
- Pytest ordering: used to change the execution order of test cases.
- Allure pytest: used to generate beautiful test reports.
Pytest plug-in list website: https://plugincompat.herokuapp.com , which contains many plug-in packages. You can choose to use them according to your work needs.
Note: I often install Python toolkit.
Many installation packages may be installed in a Python project. Re installation is required to create a virtual environment (new project), which is very troublesome and time-consuming, or re installation is avoided during project deployment.
You can record all installation packages for an existing project in requirements Txt file, and then click to install all installation packages in another environment.
requirements.txt file, which is used to record all dependent packages and their exact version numbers for deployment in the new environment.
- Use the following command to generate the dependent packages in the current virtual environment into a file with a version number:
pip freeze > requirements.txt - When you need to create a full copy of this virtual environment, you can create a new virtual environment and run the following command on it:
pip install -r requirements.txt
As mentioned above:
We can write the module names of all plug-ins into one txt folder.
pytest-html pytest-rerunfailures pytest-xdist pytest-ordering allure-pytest
Then execute PIP install - R requirements. On the CMD command line Txt command. (Global installation)
It can also be executed on the command line in PyCharm, but only for this project.
Tip: you can install the specified version of the module
Specify the version by using = =, > =, < =, > <. If not, install the latest version.
For example: PIP install pytest xdist = = 2.2.0
Pytest xdist module version 2.2.0 will be installed.
3. The first example of Pytest running
We can write code directly in the pycham example.
""" 1.Learning objectives master pytest Basic methods of writing test cases 2.Operation steps 2.1 Import pytest 2.2 Write test cases directly Default must be test At the beginning, you can also go to Pytest Modify the beginning of the test case file in. 2.3 Execute use case pytest.main("-s file name") 3.python assert Assert assert condition,"Abnormal information" 3.demand """ # 1. Import pytest import pytest # 2. Write the required test cases # This type of method represents a function def test_login(): print("Login steps") # The assertion used by pytest is Python's own assertion assert keyword assert "abcd" in "abcdefg" def test_register(): print("Registration steps") assert False # 3.setup and teardown # def setup_function(): # print("open browser / open APP") # # def teardown_function(): # print("close browser / APP") # 3. Execute test cases if __name__ == '__main__': # Notice the format. The main parameter is a list pytest.main(["-s", "test_pytest_01.py"]) """ Execution results: ============================= test session starts ============================= platform win32 -- Python 3.7.7, pytest-6.2.2, py-1.10.0, pluggy-0.13.1 rootdir: J:\PyCharmWorkSpace\Pytest_d\pytest_demo collected 2 items test_pytest_01.py Login steps. Registration steps F (. Indicates success, F (indicates failure) ================================== FAILURES =================================== ________________________________ test_register ________________________________ (The following is a detailed description of the failure) def test_register(): print("Registration steps") > assert False E assert False test_pytest_01.py:28: AssertionError =========================== short test summary info =========================== FAILED test_pytest_01.py::test_register - assert False ========================= 1 failed, 1 passed in 0.09s ========================= Process finished with exit code 0 """
4. How the Pytest framework works
(1) Pytest main function mode
- Run all test cases: pytest main()
You can also add a parameter: pytest main(['-vs'])Tip: note that all test cases used, including test cases on different files, will be executed.
- Execute the test case of the specified file:
pytest.main(['-s','-v','test_a.py'])
You can also pytest main(['-vs','test_a.py']) - Execute all test cases under the specified package:
pytest.main(['-vs','./interface_testcase'])
Tip: the argument to the main function is a list data type.
(2) Command line mode
- Run all test cases: pytest
- Execute the test case of the specified file: pytest file path / file name
For example: pytest - vs/ test_ a.py - Execute all test cases under the specified package: pytest package path / package name
For example: pytest - vs/ interface_ testcase
(3) Execute the specified test case through node id
nodeid consists of package name + module file name, separator, class name, method name, function name and parameter. It is divided by:: as follows:
- Run the specified use case class in the module
pytest -vs ./interface_testcase/test_interface.py::test_01_func
perhaps
pytest.main(['-vs' ./interface_testcase/test_interface.py::test_01_func) - Run the specified use case method in the module
pytest -vs test_interface.py::test_01_func::test_method
perhaps
pytest.main(['-vs' test_interface.py::test_01_func::test_method)
5. Run test cases in PyCharm as Pytest
Step 1:
Click File - > Settings - > tools - > Python integrated tools - > testing
Change the default test runner from unittests to pytest and apply it.
Step 2:
Edit configurations in PyCharm Configure to run test cases in pytest mode.
Click Edit configurations... In the upper right corner of PyCharm,
In Edit configurations Click the + sign in the upper left corner to add Python tests - > pytest
Step 3:
Then select the test file that target runs. You can select module (file name), such as test_01.py, or select the file path scripts path.
After setting, click apply.
last:
After executing the test case, the view result panel is as follows:
You will find that it is much easier to use and more beautiful than checking the test results before.