Pytest is a python-based testing framework for writing and executing test code. pytest is mainly used for API testing, you can write code to test API, database, UI, etc.
pytest is a very mature full-featured Python testing framework, which mainly has the following advantages:
simple and flexible, easy to use. The syntax of pytest is concise and clear, easy to understand and use.
Parameterization is supported. pytest can parameterize test cases through decorators or fixture methods to improve the coverage of test cases.
It can support simple unit tests and complex functional tests, and can also be used for automated testing such as selenium/appnium and interface automated testing (pytest+requests).
pytest has many third-party plug-ins, and can customize extensions, such as pytest-selenium (integrated selenium), pytest-html (perfect html test report generation), pytest-rerunfailures (failure case repeated execution), pytest-xdist (Multi-CPU distribution) etc.
Skip and xfail processing of test cases. pytest provides a flexible mechanism to skip test cases or expected failures, and one or some test cases can be skipped during the test as needed.
It can be well integrated with jenkins. pytest can be seamlessly integrated with continuous integration tools such as Jenkins to facilitate automated testing and report generation.
The report framework - allure also supports pytest. pytest can be integrated with the Allure reporting framework to generate detailed HTML test reports for easy analysis and display of test results.
pytest is a powerful, flexible and easy-to-use Python testing framework, which is suitable for various types of testing requirements and has high practical value.
Install
# 安装
pip install pytest
# 帮助
pytest -h
format requirement
File name: test_*.py or *_test.py
Function name: start with test
Common parameters
-s
Display the standard output, equivalent to –capture=no
, pytest does not output print logging
the output by default, unless the assert fails.
-v
Show detailed report.
-k
Search for test cases by keyword .
-q
Display a compact report.
-m
Only run the marked test cases.
-x
Stop testing immediately when a use case fails.
-c file
Load configuration file from file.
-l (--showlocals)
Display local variables and their values when backtracking with case failure information.
-rsxX
Report® why test cases were skipped (s), expected to fail (x), expected to fail but actually passed (X).
-strict
Disallow the use of mark flags not registered in the configuration file (pytest.ini).
--maxfail=n
Stop running tests after failure n.
–reruns=num
Failed cases are rerun num times. Requires pytest-rerunfailures
the plugin module to be installed.
--lf (--last-failed)
Execute only the last failed use case. If there are no failed cases or no cache files found, the default is to run all cases. Used
--lfnf =[all, none]
at the same time, it means that all test cases are executed when no test cases or cache files are found, and it means that no test cases are executed when no test cases or cache files are found.--lf
=all
=none
pytest.main(['--lf','--lfnf=none', "test.py"])
--ff (--failed-first)
Execute the failed use cases first before executing other use cases.
--nf (--new-first)
Start by running tests from a new file or newly modified use case.
--sw (--stepwise)
Exit on test failure and start the test next time in the failed test case.
--stepwise-skip
Ignore the first failed test and exit on the second test failure.
--keep-duplicates
Repeated testing.
--durations=n
Display the slowest n use cases. Note: pytest will not show test times < 0.01s by default unless the parameter -vv is added.
--fixtures
Show all available fixtures.
--tb=style
Stack trace information printing mode (auto/long/short/line/native/no]).
--setup-show
Show fixture execution steps.
--cache-show=[CACHESHOW]
Display cache contents, do not perform collection or testing.
--cache-clear
Clear pytest cache before running.
--continue-on-collection-errors
Enforce testing even if a collection (collection case phase) error occurs.
--rootdir=ROOTDIR
Defines the root directory for tests.
--color=color
Color of terminal output (yes/no/auto).
--collect-only
Only use cases are collected, not executed.
--assert=MODE
"plain" does not perform any assertion debugging, "rewrite" rewrites the assert statement in the test module to provide assert expression information
basic test
file name:test_one.py
# 测试函数
def test_division():
assert 1/1.0==1
# 测试类
class TestOne:
def test_addition(self):
"""
测试方法
"""
assert 1 + 1 == 2
def testsquare(self):
"""
测试方法
"""
assert 2*2 == 3
def tesequality():
"""
无效
"""
assert 10 == 11
run:
pytest -v
-v
Indicates to view details.
Found 3 test cases, 1 failed and 2 passed.
test subset
Find a subset by function name
test_sub.py
# 测试子集
class TestSub:
def test_compare_one(self):
"""
测试方法
"""
assert 1 + 1 == 2
def test_compare_two(self):
"""
测试方法
"""
assert 1 + 2 == 3
pytest -v -k compare
Use the parameter value pytest -k <substring>
of the command -k
to filter the function name.
group mark
# -*- coding: utf-8 -*-
import pytest
# 测试子集
class TestGroup:
@pytest.mark.group
def test_group_one(self):
"""
测试方法
"""
assert 1 + 1 == 2
@pytest.mark.group
def test_group_two(self):
"""
测试方法
"""
assert 1 + 2 == 3
pytest -v -m group
Here the decorator is used @pytest.mark.group
to mark the function, and then the pytest -v -m group
in -m
is used to find this grouping mark.
fixture function
import pytest
# 测试fixture
class TestFixture:
@pytest.fixture
def input_value(self):
return 36
def test_division(self, input_value):
"""
测试方法
"""
assert input_value / 6 == 6
Here, @pytest.fixture
the modified function input_value is used to prepare the data in advance for test_division
use. This method can only be used in one file. If you want to use it globally, you can configure Conftest.py.
to parameterize
import pytest
@pytest.mark.parametrize("num, output",[(1,11),(2,22),(3,35),(4,44)])
def test_multiplication_11(num, output):
assert 11*num == output
pytest test_parame.py -v
skip test
import pytest
@pytest.mark.xfail
@pytest.mark.great
def test_greater():
num = 100
assert num > 100
@pytest.mark.xfail
@pytest.mark.great
def test_greater_equal():
num = 100
assert num >= 100
@pytest.mark.skip
@pytest.mark.others
def test_less():
num = 100
assert num < 200
pytest test_xfail_skip.py -v
@pytest.mark.xfail
Marked as xfail status.
@pytest.mark.skip
Skip directly.
See the official website for more .