参数说明

Pytest 执行的时候,支持一些参数开启特定功能。
这些参数一般都有短命令,比如 --help 可以简写为 --h,两者等价!

help

查看使用帮助信息

pytest -h
usage: pytest [options] [file_or_dir] [file_or_dir] [...]

positional arguments:
  file_or_dir

general:
  -k EXPRESSION         Only run tests which match the given...
  -m MARKEXPR           Only run tests matching given mark exp...
  --markers             show markers 
  ...

verbose

详细模式,显示每个测试的详细结果,比如我有一个测试文件,里边有两个测试用例

test_a.py
def test_one():
    print("test_abc")

def test_two():
    assert 1 == 1

我直接不加任何参数,执行测试命令,会输出如下结果:

pytest
=== test session starts ===
collected 2 items # 扫描到2个需要测试的用例
test_a.py ..   [100%] # 两个点代表,test_a.py 里两个用例均通过,测试进度 100%
=== 2 passed in 0.00s ===

如果我加上 -v 参数,执行测试命令,会输出如下结果:

pytest -v
=== test session starts ===
collected 2 items # 扫描到2个需要测试的用例
test_a.py::test_one PASSED [50%] # test_one 用例通过,测试进度 50%
test_a.py::test_two PASSED [100%] # test_two 用例通过,测试进度 100%
=== 2 passed in 0.00s ===

noCapture

-s--capture=no的短命令,禁用捕获并显示print输出和日志,还是上面的例子

=== test session starts ===
collected 2 items 
test_a.py test_one执行啦
..
=== 2 passed in 0.00s ===

通常情况下,我们习惯上边两个参数组合着一起用 -vs 一起用,这样既能看到详细的测试结果,又能看到print输出和日志。