pytest runtime parameter description, pytest detailed explanation, pytest.ini detailed explanation

1. Introduction to Pytest

1.pytest is a very mature, full-featured Python testing framework with the following main features:

Simple and flexible, easy to use, supports parameterization
2. It can support simple unit tests and complex functional tests, and can also be used for automated tests such as selenium and appium. Requests+pytest interface automated testing 3. pytest
has many third-party plug-ins. And you can customize extensions, the more useful ones are:
pytest-selenium (integrated selenium)
pytest-html (perfect html test report generation)
pytest-rerunfailures (repeated execution of failed test cases)
pytest-xdist (multi-CPU distribution)
4. Test cases Skip and xfail processing
5. Can be well integrated with jenkins
6. Pytest also supports allure test reports
7. It is compatible with unittest and is an extension of unittest

2. Pytest and common plug-in installation

Write the following content into a txt file, such as require-install.txt
pytest
pytest-html
pytest-xdist
pytest-rerunfailures

Then use this command in the Dos window to install all modules: pip install -r require-install.txt
Verify the installation results through the command: pytest --version. If the following error is reported, you need to adjust the version of python or pytest. This is because the versions of python and pytest are incompatible:Insert image description here

Here are two error examples. One is win7, python3.7.4 and pytest5.0+. It will be ok if you downgrade python to 3.7.3. The other is Windows 10, python 3.8.1, pytest 5.3.3. Downgrade pytest. It is also ok for versions below 4.6.9, so it is recommended that you either downgrade the python version or pytest version to try.
The author uses: win7+python3.7.3+pytest5.4.1, and there is no problem in personal testing.

3. Basic application of pytest framework

  1. The situation when pytest runs the function and the methods in the running class is as follows:
importpytestdeftest01():print('测试用例一')defabcd():print('测试用例二')classTestCase:deftest_03(self):print('测试用例三')defdefg(self):print('测试用例四')if__name__=='__main__':pytest.main(['-s','pytest-demo.py'])

The running results are as follows:

Insert image description here

Notes:
(1) Test functions and test methods in classes must start with test_. Of course, the beginning can be customized in pytest.
(2) The test class must start with Test and cannot have an init method
(3) The assertion must use assert.
Execution method:
(1) Run in main function mode:
specify the running file: pytest.main(['-s',' pytest-demo.py module name'])
Note: If the py file starts with test_ or ends with _test, you can use pytest.main() to run it. Because pytest.main() will run all files starting with test_ or ending with _test in the current directory.
(2) Run in command line mode.
Click on: Terminal in the lower left corner of Pycharm to open the command line window in the current directory.
Enter the command execution: pytest pytest-demo.py module name
Note: If the py file starts with test_ or ends with _test, you can use the pytest command to run, because pytest will run all files in the current directory that start with test_ or end with _test file at the end.

4. Runtime parameter description

-s: Display the print content.
For example: pytest pytest-demo.py -s
is equivalent to: pytest.main(['-s','pytest-demo.py'])

::: Specify test case running
running function: such as: pytest pytest-demo.py::test_01
is equivalent to: pytest.main(['-s','pytest-demo.py::test01'])
running class Method: For example: pytest pytest-demp.py::TestCase::test_03
is equivalent to: pytest.main(['-s', 'pytest-demo.py::TestCase::test_03'])
–html=path/ report.html: Generate a test report in xml/html format (pytest-html needs to be installed first)
such as: pytest pytest-demp.py --html-./report.html
is equivalent to: pytest.main(['-s', 'pytest-demo.py','–html=./report.html'])
–maxfail=1: Terminate the test if one failure occurs.
For example: pytest pytest-demo.py –maxfail=1
is equivalent to: pytest .main(['-s','pytest-demo.py','–maxfail=1'])
-n: pytest-xdist multi-threaded operation (pytest-xdist needs to be installed first)
such as: pytest pytest-demo.py -n 2
is equivalent to: pytest.main(['-s','pytest-demo.py','-n=2']).
Use time.sleep(2) plus waiting time test in the use case and find that many Thread time is reduced.
–reruns NUM: Retry running the test case (pytest-rerunfailures needs to be installed first),
such as: pytest pytest-demo.py --reruns 2.
Once the use case fails, it will be executed twice. It seems that this cannot be run with main.

It can be seen from this: pytest.main(['-s','pytest-demo.py module name']), the main method is passed in a list format, and multiple parameters can be passed in the list.

5. pytest ini configuration file

The pytest.ini file is the main configuration file of pytest and can change the default behavior of pytest.

1. The placement location of pytest.ini: Generally placed in the root directory of the project (that is, under the top-level folder of the current project) 2. The
role of pytest.ini: Specify the running mode of pytest (after entering pytest in cmd, it will be read Configuration information in pytest.ini, run in the specified way)
3. Use the pytest -h command under cmd to view the setting options of pytest.ini (the screenshots below are only some of the options).
Commonly used setting options are as follows:
[pytest]
addopts = -s ... #You can add multiple command line parameters, separated by spaces
testpaths = .../pytestproject #Test case folder, you can configure it yourself, .../pytestproject is the pytestproject folder of the upper layer.
python_files = test*.py #Configure the module file name for test search
python_classes = Test* #Configure the test class name for test search
python_funtions = test #Configure the test function name for test search

Examples are as follows:
[pytest]
addopts = -s --html=./report.html
testpaths = …/pytestproject
python_files = test*.py
python_classes = Test*
python_funtions = test*

Note:
1. Automatically read the configuration file when running, and run all module files starting with test under pytestproject.

Finally, I would like to thank everyone who reads my article carefully. Reciprocity is always necessary. Although it is not a very valuable thing, if you can use it, you can take it directly:

Insert image description here

This information should be the most comprehensive and complete preparation warehouse for [software testing] friends. This warehouse has also accompanied tens of thousands of test engineers through the most difficult journey. I hope it can also help you!

Guess you like

Origin blog.csdn.net/NHB456789/article/details/132759334