1. 运行单个用例
pytest -v test_day01.py::test_simple_case1
运行效果:
Administrator@PC-202102061358 MINGW64 /d/python_demo/pytest $ pytest -v test_day01.py::test_simple_case1 =========================================================================================== test session starts ============================================================================================ platform win32 -- Python 3.8.10, pytest-6.2.5, py-1.10.0, pluggy-1.0.0 -- d:python_demopytestvenvscriptspython.exe cachedir: .pytest_cache rootdir: D:python_demopytest collected 1 item test_day01.py::test_simple_case1 PASSED [100%] ============================================================================================ 1 passed in 0.03s =============================================================================================
只运行了test_day01.py下的test_simple_case1这个用例
"::"前后不能包含空格,否则无法运行
pytest --help || pytest -h
运行效果:
$ pytest --help
usage: pytest [options] [file_or_dir] [file_or_dir] [...]
positional arguments:
file_or_dir
general:
-k expression only run tests which match the given substring expression. An expression is a python evaluatable expression where all names are substring-matched against test names and their parent
classes. Example: -k 'test_method or test_other' matches all test functions and classes whose name contains 'test_method' or 'test_other', while -k 'not test_method' matches those
that don't contain 'test_method' in their names. -k 'not test_method and not test_other' will eliminate the matches. Additionally keywords are matched to classes and functions
containing extra names in their 'extra_keyword_matches' set, as well as functions which have names assigned directly to them. The matching is case-insensitive.
-m MARKEXPR only run tests matching given mark expression.
For example: -m 'mark1 and not mark2'.
--markers show markers (builtin, plugin and per-project ones).
-x, --exitfirst exit instantly on first error or failed test.
--fixtures, --funcargs
show available fixtures, sorted by plugin appearance (fixtures with leading '_' are only shown with '-v')
--fixtures-per-test show fixtures per test
--pdb start the interactive Python debugger on errors or KeyboardInterrupt.
--pdbcls=modulename:classname
start a custom interactive Python debugger on errors. For example: --pdbcls=IPython.terminal.debugger:TerminalPdb
--trace Immediately break when running each test.
--capture=method per-test capturing method: one of fd|sys|no|tee-sys.
-s shortcut for --capture=no.
--runxfail report the results of xfail tests as if they were not marked
--lf, --last-failed rerun only the tests that failed at the last run (or all if none failed)
--ff, --failed-first run all tests, but run the last failures first.
This may re-order tests and thus lead to repeated fixture setup/teardown.
--nf, --new-first run tests from new files first, then the rest of the tests sorted by file mtime
--cache-show=[CACHESHOW]
show cache contents, don't perform collection or tests. Optional argument: glob (default: '*').
--cache-clear remove all cache contents at start of test run.
--lfnf={all,none}, --last-failed-no-failures={all,none}
which tests to run with no previously (known) failures.
--sw, --stepwise exit on test failure and continue from last failing test next time
--sw-skip, --stepwise-skip
ignore the first failing test but stop on the next failing test
...ellipsis more command lines ...
general中包含了大部分常用的命令,除此之外的更多命令可参考这篇文章:
pytest 132 个命令行参数用法 · TesterHome
-k 参数让测试人员可以使用表达式来筛选想要运行的测试用例
# 运行名称中包含 case1 或 case2 的用例
pytest -k "case1 or case2"
# 运行名称中同时包含 test 和 login 的用例
pytest -k "test and login"
#运行名称中不包含 logout 的用例
pytest -k "not logout"
4. -m 参数
-m 参数可以运行被标记的一个或多个用例
如:为test_upload()、test_detail()加上@pytest.mark.setup_cases装饰器
import pytest
...
@pytest.mark.setup_cases
test_upload():
...
@pytest.mark.setup_cases
test_detail()
...
运行:
pytest -m "setup_cases"
输入该命令将会执行以上两个cases。当然,-m参数也可以指定多个标记名称,这些标记的名称可通过 “and” 、 “or” 、“not” 进行筛选,方法与-k参数相同。
-x 参数又称为 “exitfirst”,pytest理论上会执行所有搜索到的用例,当一个用例失败后,会将该用例测试结果记录并继续执行。但是,有的时候我们想让pytest一遇到错误就停止,这时候就可以使用 “exitfirst” 了。
pytest -x
遭遇失败后,命令行中会显示:
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! stopping after 1 failures !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
6. -maxfail=num 参数
-x 参数一遇到错误就停止。而-maxfail参数则可以指定失败的次数上限。
pytest -maxfail=5
"="前后不能包含空格,否则无法运行
-s 或 —capture=no 都关闭了输出捕获,我们调试时的print并不会显示。
pytest -s
# or
pytest --capture=no
8. —lf(—last-failed) 与 —ff(—failed-first)
当跑完一轮测试后,我们可以用—lf 重新运行上次失败的用例集。
# firstly run the test suite
pytest
# then use --lf run the failed cases
pytest --lf
而—ff 与 -lf 不同的地方在于,-lf 只运行上次失败的用例;—ff 会首先运行失败的用例,再执行余下成功的用例
# firstly run the test suite
pytest
# use --ff run the failed ones first, and then the succecss ones
pytest --ff
—lf 与 —ff 运行结果对比:
# --lf
collected 3 items / 2 deselected / 1 selected
run-last-failure: rerun previous 1 failure
test_day01.py F
# ====================================================
# --ff
collected 3 items
run-last-failure: rerun previous 1 failure first
test_day01.py F..
9. -v || —verbose 参数
-v/—verbose 可以更详细的显示运行结果。
...
# diffrent content with -v
test_day01.py::test_simple_case PASSED [ 33%]
test_day01.py::test_simple_case1 FAILED [ 66%]
test_day01.py::test_print_sys PASSED [100%]
10. -q || —quiet 参数
与 -v 相反,-q 会简化输出信息。
-l 参数会输出失败用例中局部变量的值
...
================================================================================================= FAILURES =================================================================================================
____________________________________________________________________________________________ test_simple_case1 _____________________________________________________________________________________________
def test_simple_case1():
dic = {"key1": "123", "key2": "456"}
> assert dic == {"key1": "1231", "key2": "456"}
E AssertionError: assert {'key1': '123', 'key2': '456'} == {'key1': '123...'key2': '456'}
E Omitting 1 identical items, use -vv to show
E Differing items:
E {'key1': '123'} != {'key1': '1231'}
E Use -v to get the full diff
dic = {'key1': '123', 'key2': '456'}
输出内容包含了 dic 变量的值
- —tb=no,屏蔽所有错误信息的输出
- —tb=line,仅显示错误的位置
- —tb=short,仅输出assert一行以及系统判定的内容
- —tb=long,错误信息最为详细
- —tb=auto(default),如果多个用例失败,仅打印第一个和最后一个的错误信息,格式与long一致
- —tb=native,只显示Python标准库的错误信息
用来统计N个运行时长最长的用例
$ pytest request_test.py --durations=100000
运行效果:
$ pytest request_test.py --durations=100000
=========================================================================================== test session starts ============================================================================================
platform win32 -- Python 3.8.10, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: D:python_demopytest
collected 3 items
request_test.py ... [100%]
========================================================================================= slowest 100000 durations =========================================================================================
0.25s call request_test.py::test_upload
0.24s call request_test.py::test_detail
0.23s call request_test.py::test_clean
(6 durations < 0.005s hidden. Use -vv to show these durations.)
============================================================================================ 3 passed in 0.80s ============================================================================================```



