pytest is a very mature and full-featured Python testing framework with the following features:
Simple, flexible and easy to use.
Support parameterization. It
can support simple unit testing and complex functional testing. It can also be used to do selenium/appnium and other automated testing, interface automated testing (pytest+requests).
pytest has many third-party plug-ins and can be self- tested. Define extensions, such as pytest-selenium (integrated selenium), pytest-html (perfect html test report generation), pytest-rerunfailures (repeated execution of failed cases), pytest-xdist (multi-CPU distribution) and other
test cases skip Processing with xfail, it
can be well integrated with jenkins
report framework-allure also supports pytest
Install Pytest
pip install -U pytest
Pytest use case design principles
The test class starts with Test and cannot have an init method
. Functions starting with test_. Classes starting
with Test
All packages must have __init__.py files.
Asserts use assert
Two ways to run Pytest
You can set the corresponding parameters when running the script. For details, please see the parameter description
. Code operation in Pycharm
@pytest.mark.xfail()
Command line operation mode
pytest test.py
# 运行指定类下的指定方法
pytest 文件名::类名::方法名
Pytest parameter description
-v description: can output more detailed execution information of the use case, such as the file where the use case is located and the name of the use case, etc.
-s description: enter the debugging information in our use case, such as print information, etc.
-x: encounter errors Use case, exit execution immediately, and output the result
-collect-only: Show all the use cases to be executed
-vv: Show detailed test results
-tb=no: Do not show the error details of the use case failure
-maxfail=num When the use case is wrong When the specified number is reached, stop the test
ini configuration file
There is a space in the middle of multiple parameters to
create a pytest.ini file (fixed file name writing)
# [pyteset]
[pytest]
addopts=-vv -s
testpaths=../HC/huace
python_files=test*.py
python_classes=huace
python_functions=test*
Run the code directly from the command line: pytest
will automatically find this pytest.ini configuration file to run
I recommend a software testing exchange group, QQ: 642830685. The group will share software testing resources, test interview questions and industry information from time to time. You can actively exchange technology in the group, and there are big guys to answer questions for you.