pytest 是一个流行的 Python 测试框架,它提供了许多命令行选项,可以帮助用户更好地控制测试执行过程。在使用 pytest 进行测试时,熟悉 pytest 的命令行选项非常重要,这将有助于减少错误和提高测试效率。本文将详细介绍 pytest 的命令行选项,并给出示例。
一、pytest 命令行选项分类
pytest 的命令行选项可以分为三类:
- 环境配置选项:这些选项用于设置 pytest 的环境配置,例如设置日志级别、覆盖配置文件、设置测试模式等。
- 测试过滤选项:这些选项用于过滤和选择测试用例,例如指定测试目录、选择特定测试模块、运行指定测试函数等。
- 测试执行选项:这些选项用于控制 pytest 的测试执行过程,例如重试失败测试、生成测试报告、并行执行测试等。
下面,我们将逐一介绍这些命令行选项的具体用法。
二、环境配置选项
--version:打印 pytest 版本号。
-h, --help:显示 pytest 命令行选项和使用说明。
--verbose, -v:增加测试结果输出的详细程度。
--quiet, -q:减少测试结果输出的详细程度。
--traceconfig:显示解析和加载配置文件时的详细信息。
--pdb:当测试失败或者发生异常时,进入 Python 调试器。
--pdbcls:自定义调试器的类型。
--capture:设置 pytest 的标准输出流捕获方式,有三个值可选:sys、fd、no。
--norecursedirs:设定哪些目录不搜索测试用例。
--rootdir:设置 pytest 的根目录。
--maxfail=n:设定在第 n 次测试失败后停止测试执行。
--junit-xml=PATH:将测试结果输出到 JUnit XML 文件中。
三、测试过滤选项
path:指定测试目录或文件,可以是相对路径或绝对路径。例如:pytest tests/。
-m:选择特定标记的测试用例进行执行。例如:pytest -m "slow"。
-k:选择包含某个关键字的测试用例进行执行。例如:pytest -k "add or subtract"。
-x:遇到一条测试用例失败就停止测试执行。
--pdb:当测试失败或者发生异常时,进入 Python 调试器。
--lf:只重跑上次测试失败的测试用例。
--ff:只重跑上次测试失败的测试用例,并在全部测试结束后再重新运行一遍这些测试用例。
--sw:重跑上次修改过的测试模块。
--last-failed-no-failures:只重跑上次失败的测试用例(如果没有失败的测试用例,则不执行测试)。
--collect-only:只执行测试用例的收集阶段,不运行测试用例的执行阶段。
--pdbcls:自定义调试器的类型。
--trace:显示 pytest 的内部跟踪信息。
--count:运行指定数量的测试用例。例如:pytest -v --count=10。
四、测试执行选项
-n:并行运行测试用例,可以在后面跟一个数字指定并发度。例如:pytest -n 4。
-x:遇到一条测试用例失败就停止测试执行。
--maxfail=n:设定在第 n 次测试失败后停止测试执行。
--last-failed:只重跑上次失败的测试用例。
--failed-first:先运行之前失败的测试用例。
--reruns=n:在测试用例失败的情况下,重新运行 n 次测试。
--pdb:当测试失败或者发生异常时,进入 Python 调试器。
--pdbcls:自定义调试器的类型。
--junit-xml=PATH:将测试结果输出到 JUnit XML 文件中。
--html=PATH:将测试结果输出到 HTML 文件中。
--tb=long/short/line/native/no:设置输出错误信息的格式。
--capture=no:禁止捕获标准输出和标准错误,直接将它们输出到终端。
--capture=sys/stdout/stderr:设置 pytest 的标准输出流捕获方式,有三个值可选:sys、fd、no。
--show-capture=all/failed/no:控制是否显示捕获的标准输出流。
--disable-warnings:禁用 pytest 的警告信息。
以上是 pytest 的命令行选项介绍,通过使用这些选项,可以更好地控制 pytest 的测试执行过程,减少错误和提高测试效率。接下来我将给出部分命令行选项的代码示例及运行效果。
五、代码示例及运行效果
1.--version:打印 pytest 版本号。
该选项可以通过执行 pytest --version 命令来使用,代码示例如下:
1 2 | $ pytest - - version This is pytest version 5.0 . 1 , imported from / usr / local / lib / python3. 6 / site - packages / pytest.py |
2.-h, --help:显示 pytest 命令行选项和使用说明。
该选项可以通过执行 pytest --help 命令来使用,代码示例如下:
1 2 3 4 5 6 7 8 9 10 | $ pytest - - help usage: pytest [options] [file_or_dir] [file_or_dir] [...] positional arguments: file_or_dir test file or root directory to search for tests optional arguments: - h, - - help show this help message and exit - - version display pytest version and information about plugins ... |
3.--verbose, -v:增加测试结果输出的详细程度。
该选项可以通过执行 pytest -v 命令来使用,代码示例如下:
1 2 3 4 5 | $ pytest - v test_example.py::test_add PASSED [ 50 % ] test_example.py::test_subtract PASSED [ 100 % ] = = = = = = = = = = = = = = = = = = = = = = = = = = = = 2 passed in 0.03s = = = = = = = = = = = = = = = = = = = = = = = = = = = = = |
4.--quiet, -q:减少测试结果输出的详细程度。
该选项可以通过执行 pytest -q 命令来使用,代码示例如下:
1 2 3 | $ pytest - q .. 2 passed in 0.03s |
5.--traceconfig:显示解析和加载配置文件时的详细信息。
该选项可以通过执行 pytest --traceconfig 命令来使用,代码示例如下:
1 2 3 4 5 6 | $ pytest - - traceconfig using: pytest - 5.0 . 1 , py - 1.8 . 0 , pluggy - 0.12 . 0 active plugins: * pytest_cov - 2.7 . 1 at / Users / user / anaconda3 / envs / test / lib / python3. 6 / site - packages / pytest_cov / plugin.py disabled plugins: collector: <_pytest.main.Session object at 0x107cbe3c8 > |
6.--pdb:当测试失败或者发生异常时,进入 Python 调试器。
该选项可以通过执行 pytest --pdb 命令来使用,在测试用例出现错误时会自动进入 pdb 调试器,代码示例如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | $ pytest - - pdb test_example.py::test_add PASSED [ 50 % ] test_example.py::test_subtract FAILED [ 100 % ] = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = FAILURES = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = ________________________________ test_subtract ________________________________ def test_subtract(): assert subtract( 4 , 3 ) = = 1 > assert subtract( 2 , 3 ) = = - 1 E assert 2 = = - 1 E + where 2 = subtract( 2 , 3 ) test_example.py: 10 : AssertionError - - - - - - - - - - - - - - - - - - - - - - - - - - - - - PDB - - - - - - - - - - - - - - - - - - - - - - - - - - - - - > / Users / user / example / test_example.py( 10 )test_subtract() ... (Pdb) |
7.--capture:设置 pytest 的标准输出流捕获方式,有三个值可选:sys、fd、no。
该选项可以通过执行 pytest --capture=sys 命令来使用,将标准输出流和标准错误输出重定向到 pytest 的日志系统中,代码示例如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | $ pytest - - capture = sys test_example.py::test_add PASSED [ 50 % ] test_example.py::test_subtract FAILED [ 100 % ] = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = FAILURES = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = ________________________________ test_subtract ________________________________ def test_subtract(): assert subtract( 4 , 3 ) = = 1 > assert subtract( 2 , 3 ) = = - 1 E assert 2 = = - 1 E + where 2 = subtract( 2 , 3 ) test_example.py: 10 : AssertionError [Capture] capturing logcall output to 'log' : sys |
8.--norecursedirs:设定哪些目录不搜索测试用例。
该选项可以通过执行 pytest --norecursedirs=examples 命令来使用,将 examples 目录从搜索测试用例的路径中排除,代码示例如下:
1 2 3 4 5 | $ pytest - - norecursedirs = examples test_example.py::test_add PASSED [ 50 % ] test_example.py::test_subtract PASSED [ 100 % ] = = = = = = = = = = = = = = = = = = = = = = = = = = = = 2 passed in 0.03s = = = = = = = = = = = = = = = = = = = = = = = = = = = = = |
9.--rootdir:设置 pytest 的根目录。
该选项可以通过执行 pytest --rootdir=/path/to/project 命令来使用,将 pytest 的根目录设置为 /path/to/project,代码示例如下:
1 2 3 4 5 | $ pytest - - rootdir = / path / to / project test_example.py::test_add PASSED [ 50 % ] test_example.py::test_subtract PASSED [ 100 % ] = = = = = = = = = = = = = = = = = = = = = = = = = = = = 2 passed in 0.03s = = = = = = = = = = = = = = = = = = = = = = = = = = = = = |
10.--maxfail=n:设定在第 n 次测试失败后停止测试执行。
该选项可以通过执行 pytest --maxfail=1 命令来使用,当第一条测试用例失败时会停止测试执行,代码示例如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | $ pytest - - maxfail = 1 test_example.py::test_add PASSED [ 50 % ] test_example.py::test_subtract FAILED [ 100 % ] = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = FAILURES = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = ________________________________ test_subtract ________________________________ def test_subtract(): assert subtract( 4 , 3 ) = = 1 > assert subtract( 2 , 3 ) = = - 1 E assert 2 = = - 1 E + where 2 = subtract( 2 , 3 ) test_example.py: 10 : AssertionError = = = = = = = = = = = = = = = = = = = = = = = = = = 1 failed, 1 passed in 0.04s = = = = = = = = = = = = = = = = = = = = = = = = = = |
11.--junit-xml=PATH:将测试结果输出到 JUnit XML 文件中。
该选项可以通过执行 pytest --junit-xml=test_results.xml 命令来使用,将测试结果输出到 test_results.xml 文件中,代码示例如下:
1 2 3 4 5 | $ pytest - - junit - xml = test_results.xml test_example.py::test_add PASSED [ 50 % ] test_example.py::test_subtract PASSED [ 100 % ] = = = = = = = = = = = = = = = = = = = = = = = = = = = = 2 passed in 0.03s = = = = = = = = = = = = = = = = = = = = = = = = = = = = = |
12.path:指定测试目录或文件,可以是相对路径或绝对路径。例如:pytest tests/。
该选项可以通过执行 pytest tests/ 命令来使用,只运行 tests 目录下的测试用例,代码示例如下:
1 2 3 4 5 | $ pytest tests / test_example.py::test_add PASSED [ 50 % ] test_example.py::test_subtract PASSED [ 100 % ] = = = = = = = = = = = = = = = = = = = = = = = = = = = = 2 passed in 0.03s = = = = = = = = = = = = = = = = = = = = = = = = = = = = = |
13.-m:选择特定标记的测试用例进行执行。例如:pytest -m "slow"。
该选项可以通过在测试用例中使用 @pytest.mark 标记来进行使用,例如:
1 2 3 4 5 6 7 8 | import pytest @pytest .mark.slow def test_slow(): pass def test_not_slow(): pass |
然后执行 pytest -m "slow" 命令来只运行标有 @pytest.mark.slow 标记的测试用例,代码示例如下:
1 2 3 4 5 6 | $ pytest - m "slow" test_example.py: 3 : PytestUnknownMarkWarning: Unknown pytest.mark.slow - is this a typo? You can register custom marks to avoid this warning - for details, see https: / / docs.pytest.org / en / latest / mark.html @pytest .mark.slow no tests ran in 0.00s PytestUnknownMarkWarning: Unknown pytest.mark.slow - is this a typo? You can register custom marks to avoid this warning - for details, see https: / / docs.pytest.org / en / latest / mark.html |
14.-k:选择包含某个关键字的测试用例进行执行。例如:pytest -k "add or subtract"。
该选项可以通过执行 pytest -k "add or subtract" 命令来使用,只运行测试用例名称中包含 "add" 或者 "subtract" 字符串的测试用例,代码示例如下:
1 2 3 4 5 | $ pytest - k "add or subtract" test_example.py::test_add PASSED [ 50 % ] test_example.py::test_subtract PASSED [ 100 % ] = = = = = = = = = = = = = = = = = = = = = = = = = = = = 2 passed in 0.03s = = = = = = = = = = = = = = = = = = = = = = = = = = = = = |
15.-x:遇到一条测试用例失败就停止测试执行。
该选项可以通过执行 pytest -x 命令来使用,在第一条测试用例失败时会停止测试执行,代码示例如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | $ pytest - x test_example.py::test_add PASSED [ 50 % ] test_example.py::test_subtract FAILED [ 100 % ] = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = FAILURES = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = ________________________________ test_subtract ________________________________ def test_subtract(): assert subtract( 4 , 3 ) = = 1 > assert subtract( 2 , 3 ) = = - 1 E assert 2 = = - 1 E + where 2 = subtract( 2 , 3 ) test_example.py: 10 : AssertionError = = = = = = = = = = = = = = = = = = = = = = = = = = 1 failed, 1 passed in 0.04s = = = = = = = = = = = = = = = = = = = = = = = = = = |
16.--lf:只重跑上次测试失败的测试用例。
该选项可以通过执行 pytest --lf 命令来使用,在上次测试用例出现错误时会自动重新运行失败的测试用例,代码示例如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | $ pytest - - lf test_example.py::test_add PASSED [ 50 % ] test_example.py::test_subtract FAILED [ 100 % ] = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = FAILURES = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = ______________________ test_subtract[ 2 - 3 ] ______________________ x = 2 y = 3 def test_subtract(x, y): assert subtract(x, y) = = - 1 tests / test_example.py: 10 : AssertionError - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - rerun test call - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - test_example.py::test_subtract[x |
17.--ff:只重跑最后一次测试失败的测试用例。
该选项可以通过执行 pytest --ff 命令来使用,在上次测试用例出现错误时会自动重新运行最后一个失败的测试用例,代码示例如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | $ pytest - - ff test_example.py::test_add PASSED [ 50 % ] test_example.py::test_subtract FAILED [ 100 % ] = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = FAILURES = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = ________________________________ test_subtract ________________________________ def test_subtract(): assert subtract( 4 , 3 ) = = 1 > assert subtract( 2 , 3 ) = = - 1 E assert 2 = = - 1 E + where 2 = subtract( 2 , 3 ) test_example.py: 10 : AssertionError - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Captured stdout - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - subtraction of 2 and 3 is - 1 = = = = = = = = = = = = = = = = = = = = = = = = = = 1 failed, 1 passed in 0.04s = = = = = = = = = = = = = = = = = = = = = = = = = = |
18.--failed-first:在所有测试用例之前先运行上次测试失败的测试用例。
该选项可以通过执行 pytest --failed-first 命令来使用,会先运行上次测试失败的测试用例,然后再运行剩下的测试用例,代码示例如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | $ pytest - - failed - first test_example.py::test_subtract FAILED [ 100 % ] test_example.py::test_add PASSED = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = FAILURES = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = ________________________________ test_subtract ________________________________ def test_subtract(): assert subtract( 4 , 3 ) = = 1 > assert subtract( 2 , 3 ) = = - 1 E assert 2 = = - 1 E + where 2 = subtract( 2 , 3 ) test_example.py: 10 : AssertionError - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Captured stdout - - - - - - - - - - - - - - - - - - - - - - - - - - - - - subtraction of 2 and 3 is - 1 = = = = = = = = = = = = = = = = = = = = = = = = = = 1 failed, 1 passed in 0.03s = = = = = = = = = = = = = = = = = = = = = = = = = = |
19.-n, --numprocesses:指定并发进程数量。
该选项可以通过执行 pytest -n 4 命令来使用,将测试用例分配到 4 个并发进程中运行,代码示例如下:
1 2 3 4 5 6 7 | $ pytest - n 4 test_example.py::test_add PASSED [ 25 % ] test_example.py::test_add PASSED [ 50 % ] test_example.py::test_subtract PASSED [ 75 % ] test_example.py::test_subtract PASSED [ 100 % ] = = = = = = = = = = = = = = = = = = = = = = = = = = = = 4 passed in 0.02s = = = = = = = = = = = = = = = = = = = = = = = = = = = = = |
20.-d, --dist:启用分布式测试。
该选项可以通过执行 pytest --dist=loadfile 命令来使用,通过加载多个 Python 进程并在所有进程中分发测试运行,代码示例如下:
1 2 3 4 5 6 7 | $ pytest - - dist = loadfile test_example.py::test_add PASSED [ 25 % ] test_example.py::test_add PASSED [ 50 % ] test_example.py::test_subtract PASSED [ 75 % ] test_example.py::test_subtract PASSED [ 100 % ] = = = = = = = = = = = = = = = = = = = = = = = = = = = = 4 passed in 0.02s = = = = = = = = = = = = = = = = = = = = = = = = = = = = = |
21.--durations=n:按运行时间对测试用例进行排序,并显示最慢 n 条测试用例的运行时长。
该选项可以通过执行 pytest --duration=2 命令来使用,显示运行时间最慢的两条测试用例的运行时长,代码示例如下:
1 2 3 4 5 6 7 8 | $ pytest - - duration = 2 test_example.py::test_add PASSED [ 50 % ] test_example.py::test_subtract PASSED [ 100 % ] = = = = = = = = = = = = = = = = = = = = = = = = slowest 2 test durations = = = = = = = = = = = = = = = = = = = = = = = = = 0.01s call test_example.py::test_subtract 0.01s call test_example.py::test_add = = = = = = = = = = = = = = = = = = = = = = = = = = = = 2 passed in 0.02s = = = = = = = = = = = = = = = = = = = = = = = = = = = = = |
22.--show-capture:在结果中显示测试用例标准输出流和标准错误输出。
该选项可以通过执行 pytest --show-capture 命令来使用,将测试用例的标准输出流和标准错误输出显示在测试结果中,代码示例如下:
1 2 3 4 5 6 7 8 9 | $ pytest - - show - capture test_example.py::test_add PASSED [ 50 % ] test_example.py::test_subtract PASSED [ 100 % ] = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = Captured stdout = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = subtraction of 4 and 3 is 1 subtraction of 2 and 3 is - 1 = = = = = = = = = = = = = = = = = = = = = = = = = = = = 2 passed in 0.03s = = = = = = = = = = = = = = = = = = = = = = = = = = = = = |
以上就是 pytest 命令行选项的详细介绍和相应的代码示例,希望可以帮助你更好地使用 pytest 进行测试。
现在我也找了很多测试的朋友,做了一个分享技术的交流群,共享了很多我们收集的技术文档和视频教程。
如果你不想再体验自学时找不到资源,没人解答问题,坚持几天便放弃的感受
可以加入我们一起交流。而且还有很多在自动化,性能,安全,测试开发等等方面有一定建树的技术大牛
分享他们的经验,还会分享很多直播讲座和技术沙龙
可以免费学习!划重点!开源的!!!
qq群号:485187702【暗号:csdn11】
最后感谢每一个认真阅读我文章的人,看着粉丝一路的上涨和关注,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走! 希望能帮助到你!【100%无套路免费领取】
![](https://i-blog.csdnimg.cn/blog_migrate/2815321957aa34726929e036e2a11775.png)
![](https://i-blog.csdnimg.cn/blog_migrate/d97bb7346b9e77f5005fb10c001f41da.png)