pytest 提供许多运行时参数,用于控制输出的详细程度,比如: pytest -v。我挑几个自认为有价值的说一下。

参数 -v

首先定义一组测试用例:

# test_mod1.py

def test_case1():
    x = "this"
    assert "h" in x, "字符串中没有 h"


def test_case2():
    x = 1
    y = 2
    assert type(x) != type(y), "x 不等于 y"

def test_case3():
    x = "this"
    assert "h" in x, "字符串中没有 h"
  1. 运行时,不带 -v 参数:
===================================================================== test session starts ======================================================================
collected 3 items                                                                                                                                                

Test\test_mod1.py .F.                                                                                                                                     [100%]

=========================================================================== FAILURES =========================================================================== 
__________________________________________________________________________ test_case2 __________________________________________________________________________ 

    def test_case2():
        x = 1
        y = 2
>       assert type(x) != type(y), "x 不等于 y"
E       AssertionError: x 不等于 y
E       assert <class 'int'> != <class 'int'>
E        +  where <class 'int'> = type(1)
E        +  and   <class 'int'> = type(2)

Test\test_mod1.py:11: AssertionError
=================================================================== short test summary info ==================================================================== 
FAILED Test/test_mod1.py::test_case2 - AssertionError: x 不等于 y
================================================================= 1 failed, 2 passed in 0.07s ================================================================== 

. 表示通过,F 表示不通过,按顺序显示。

  1. 带 -v 参数:
===================================================================== test session starts ======================================================================
collected 3 items                                                                                                                                                

Test/test_mod1.py::test_case1 PASSED                                                                                                                      [ 33%] 
Test/test_mod1.py::test_case2 FAILED                                                                                                                      [ 66%]
Test/test_mod1.py::test_case3 PASSED                                                                                                                      [100%] 

=========================================================================== FAILURES =========================================================================== 
__________________________________________________________________________ test_case2 __________________________________________________________________________ 

    def test_case2():
        x = 1
        y = 2
>       assert type(x) != type(y), "x 不等于 y"
E       AssertionError: x 不等于 y
E       assert <class 'int'> != <class 'int'>
E        +  where <class 'int'> = type(1)
E        +  and   <class 'int'> = type(2)

Test\test_mod1.py:11: AssertionError
=================================================================== short test summary info ==================================================================== 
FAILED Test/test_mod1.py::test_case2 - AssertionError: x 不等于 y
================================================================= 1 failed, 2 passed in 0.07s ================================================================== 

显示详细的测试执行信息,包括每个测试用例的完整名称(模块名、类名、方法名)和结果。

参数 -r

这个参数主要控制 short test summary info 里的信息输出。可以与 -v 一起使用,如 -vra。

首先定义一组测试用例:

# test_mod1.py
import pytest


def test_pass():
    x = "this"
    assert "h" in x, "字符串中没有 h"


def test_fail():
    x = 1
    y = 2
    assert type(x) != type(y), "x 不等于 y"

def test_pass1():
    x = "this"
    assert "h" in x, "字符串中没有 h"
@pytest.mark.xfail(reason="xfail")
def test_xfail():
    x = 1
    y = 2
    assert type(x) != type(y), "x 不等于 y"

@pytest.mark.skip(reason="skip")
def test_skip():
    x = 1
    y = 2
    assert type(x) != type(y), "x 不等于 y"
  1. -rf:输出断言失败的测试用例

是 pytest 的默认值,如果不使用 -r 系列参数,默认就是 -rf(-vrf 等价于 -v)。

===================================================================== test session starts ======================================================================
collected 5 items                                                                                                                                                

Test/test_mod1.py::test_pass PASSED                                                                                                                       [ 20%] 
Test/test_mod1.py::test_fail FAILED                                                                                                                       [ 40%]
Test/test_mod1.py::test_pass1 PASSED                                                                                                                      [ 60%] 
Test/test_mod1.py::test_xfail XFAIL (xfail)                                                                                                               [ 80%] 
Test/test_mod1.py::test_skip SKIPPED (skip)                                                                                                               [100%] 

=========================================================================== FAILURES =========================================================================== 
__________________________________________________________________________ test_fail ___________________________________________________________________________ 

    def test_fail():
        x = 1
        y = 2
>       assert type(x) != type(y), "x 不等于 y"
E       AssertionError: x 不等于 y
E       assert <class 'int'> != <class 'int'>
E        +  where <class 'int'> = type(1)
E        +  and   <class 'int'> = type(2)

Test\test_mod1.py:13: AssertionError
=================================================================== short test summary info ==================================================================== 
FAILED Test/test_mod1.py::test_fail - AssertionError: x 不等于 y
====================================================== 1 failed, 2 passed, 1 skipped, 1 xfailed in 0.08s ======================================================= 
  1. -ra:输出除断言成功之外的所有测试用例
===================================================================== test session starts ======================================================================
collected 5 items                                                                                                                                                

Test/test_mod1.py::test_pass PASSED                                                                                                                       [ 20%] 
Test/test_mod1.py::test_fail FAILED                                                                                                                       [ 40%]
Test/test_mod1.py::test_pass1 PASSED                                                                                                                      [ 60%] 
Test/test_mod1.py::test_xfail XFAIL (xfail)                                                                                                               [ 80%] 
Test/test_mod1.py::test_skip SKIPPED (skip)                                                                                                               [100%] 

=========================================================================== FAILURES =========================================================================== 
__________________________________________________________________________ test_fail ___________________________________________________________________________ 

    def test_fail():
        x = 1
        y = 2
>       assert type(x) != type(y), "x 不等于 y"
E       AssertionError: x 不等于 y
E       assert <class 'int'> != <class 'int'>
E        +  where <class 'int'> = type(1)
E        +  and   <class 'int'> = type(2)

Test\test_mod1.py:13: AssertionError
=================================================================== short test summary info ==================================================================== 
SKIPPED [1] Test\test_mod1.py:24: skip
XFAIL Test/test_mod1.py::test_xfail - xfail
FAILED Test/test_mod1.py::test_fail - AssertionError: x 不等于 y
====================================================== 1 failed, 2 passed, 1 skipped, 1 xfailed in 0.08s ======================================================= 
  1. -rA:输出所有的测试用例
===================================================================== test session starts ======================================================================
collected 5 items                                                                                                                                                

Test/test_mod1.py::test_pass PASSED                                                                                                                       [ 20%] 
Test/test_mod1.py::test_fail FAILED                                                                                                                       [ 40%]
Test/test_mod1.py::test_pass1 PASSED                                                                                                                      [ 60%] 
Test/test_mod1.py::test_xfail XFAIL (xfail)                                                                                                               [ 80%] 
Test/test_mod1.py::test_skip SKIPPED (skip)                                                                                                               [100%] 

=========================================================================== FAILURES =========================================================================== 
__________________________________________________________________________ test_fail ___________________________________________________________________________ 

    def test_fail():
        x = 1
        y = 2
>       assert type(x) != type(y), "x 不等于 y"
E       AssertionError: x 不等于 y
E       assert <class 'int'> != <class 'int'>
E        +  where <class 'int'> = type(1)
E        +  and   <class 'int'> = type(2)

Test\test_mod1.py:13: AssertionError
============================================================================ PASSES ============================================================================ 
=================================================================== short test summary info ==================================================================== 
PASSED Test/test_mod1.py::test_pass
PASSED Test/test_mod1.py::test_pass1
SKIPPED [1] Test\test_mod1.py:24: skip
XFAIL Test/test_mod1.py::test_xfail - xfail
FAILED Test/test_mod1.py::test_fail - AssertionError: x 不等于 y
====================================================== 1 failed, 2 passed, 1 skipped, 1 xfailed in 0.08s ======================================================= 
  1. -rp:输出断言成功的测试用例
===================================================================== test session starts ======================================================================
collected 5 items                                                                                                                                                

Test/test_mod1.py::test_pass PASSED                                                                                                                       [ 20%] 
Test/test_mod1.py::test_fail FAILED                                                                                                                       [ 40%]
Test/test_mod1.py::test_pass1 PASSED                                                                                                                      [ 60%] 
Test/test_mod1.py::test_xfail XFAIL (xfail)                                                                                                               [ 80%] 
Test/test_mod1.py::test_skip SKIPPED (skip)                                                                                                               [100%] 

=========================================================================== FAILURES =========================================================================== 
__________________________________________________________________________ test_fail ___________________________________________________________________________ 

    def test_fail():
        x = 1
        y = 2
>       assert type(x) != type(y), "x 不等于 y"
E       AssertionError: x 不等于 y
E       assert <class 'int'> != <class 'int'>
E        +  where <class 'int'> = type(1)
E        +  and   <class 'int'> = type(2)

Test\test_mod1.py:13: AssertionError
=================================================================== short test summary info ==================================================================== 
PASSED Test/test_mod1.py::test_pass
PASSED Test/test_mod1.py::test_pass1
====================================================== 1 failed, 2 passed, 1 skipped, 1 xfailed in 0.08s ======================================================= 
  1. -rx:输出被 @pytest.mark.xfail 装饰的测试用例
===================================================================== test session starts ======================================================================
collected 5 items                                                                                                                                                

Test/test_mod1.py::test_pass PASSED                                                                                                                       [ 20%] 
Test/test_mod1.py::test_fail FAILED                                                                                                                       [ 40%]
Test/test_mod1.py::test_pass1 PASSED                                                                                                                      [ 60%] 
Test/test_mod1.py::test_xfail XFAIL (xfail)                                                                                                               [ 80%] 
Test/test_mod1.py::test_skip SKIPPED (skip)                                                                                                               [100%] 

=========================================================================== FAILURES =========================================================================== 
__________________________________________________________________________ test_fail ___________________________________________________________________________ 

    def test_fail():
        x = 1
        y = 2
>       assert type(x) != type(y), "x 不等于 y"
E       AssertionError: x 不等于 y
E       assert <class 'int'> != <class 'int'>
E        +  where <class 'int'> = type(1)
E        +  and   <class 'int'> = type(2)

Test\test_mod1.py:13: AssertionError
=================================================================== short test summary info ==================================================================== 
XFAIL Test/test_mod1.py::test_xfail - xfail
====================================================== 1 failed, 2 passed, 1 skipped, 1 xfailed in 0.07s ======================================================= 
  1. -rs:输出被 @pytest.mark.skip 装饰的测试用例
===================================================================== test session starts ======================================================================
collected 5 items                                                                                                                                                

Test/test_mod1.py::test_pass PASSED                                                                                                                       [ 20%] 
Test/test_mod1.py::test_fail FAILED                                                                                                                       [ 40%]
Test/test_mod1.py::test_pass1 PASSED                                                                                                                      [ 60%] 
Test/test_mod1.py::test_xfail XFAIL (xfail)                                                                                                               [ 80%] 
Test/test_mod1.py::test_skip SKIPPED (skip)                                                                                                               [100%] 

=========================================================================== FAILURES =========================================================================== 
__________________________________________________________________________ test_fail ___________________________________________________________________________ 

    def test_fail():
        x = 1
        y = 2
>       assert type(x) != type(y), "x 不等于 y"
E       AssertionError: x 不等于 y
E       assert <class 'int'> != <class 'int'>
E        +  where <class 'int'> = type(1)
E        +  and   <class 'int'> = type(2)

Test\test_mod1.py:13: AssertionError
=================================================================== short test summary info ==================================================================== 
SKIPPED [1] Test\test_mod1.py:24: skip
====================================================== 1 failed, 2 passed, 1 skipped, 1 xfailed in 0.07s ======================================================= 
  1. -rN:不输出任何内容
===================================================================== test session starts ======================================================================
collected 5 items                                                                                                                                                

Test/test_mod1.py::test_pass PASSED                                                                                                                       [ 20%] 
Test/test_mod1.py::test_fail FAILED                                                                                                                       [ 40%]
Test/test_mod1.py::test_pass1 PASSED                                                                                                                      [ 60%] 
Test/test_mod1.py::test_xfail XFAIL (xfail)                                                                                                               [ 80%] 
Test/test_mod1.py::test_skip SKIPPED (skip)                                                                                                               [100%] 

=========================================================================== FAILURES =========================================================================== 
__________________________________________________________________________ test_fail ___________________________________________________________________________ 

    def test_fail():
        x = 1
        y = 2
>       assert type(x) != type(y), "x 不等于 y"
E       AssertionError: x 不等于 y
E       assert <class 'int'> != <class 'int'>
E        +  where <class 'int'> = type(1)
E        +  and   <class 'int'> = type(2)

Test\test_mod1.py:13: AssertionError
====================================================== 1 failed, 2 passed, 1 skipped, 1 xfailed in 0.07s ======================================================= 

参数 –tb

用于设置测试失败时输出的 FAILURES 信息的错误堆栈信息的详细程度和格式。可以与 -v 一起使用,如 -v –tb=no。

首先定义一组测试用例:

# test_mod1.py
def test_01():
    assert "a" == "b"

def test_02():
    assert "a" == "b"

def test_03():
    assert "a" == "b"
  1. –tb=auto:默认值

官方文档中说只输出第一条与最后一条测试用例,但是测试下来与 –tb=long 一致。这里说明一下,输出效果如下。

===================================================================== test session starts ======================================================================
collected 3 items                                                                                                                                                

Test/test_mod1.py::test_01 FAILED                                                                                                                         [ 33%]
Test/test_mod1.py::test_02 FAILED                                                                                                                         [ 66%] 
Test/test_mod1.py::test_03 FAILED                                                                                                                         [100%] 

=========================================================================== FAILURES =========================================================================== 
___________________________________________________________________________ test_01 ____________________________________________________________________________ 

    def test_01():
>       assert "a" == "b"
E       AssertionError: assert 'a' == 'b'
E
E         - b
E         + a

Test\test_mod1.py:6: AssertionError
___________________________________________________________________________ test_02 ____________________________________________________________________________ 

    def test_02():
>       assert "a" == "b"
E       AssertionError: assert 'a' == 'b'
E
E         - b
E         + a

Test\test_mod1.py:9: AssertionError
___________________________________________________________________________ test_03 ____________________________________________________________________________ 

    def test_03():
>       assert "a" == "b"
E       AssertionError: assert 'a' == 'b'
E
E         - b
E         + a

Test\test_mod1.py:12: AssertionError
=================================================================== short test summary info ==================================================================== 
FAILED Test/test_mod1.py::test_01 - AssertionError: assert 'a' == 'b'
FAILED Test/test_mod1.py::test_02 - AssertionError: assert 'a' == 'b'
FAILED Test/test_mod1.py::test_03 - AssertionError: assert 'a' == 'b'
====================================================================== 3 failed in 0.08s ======================================================================= 
  1. –tb=short:仅显示断言行和错误类型
===================================================================== test session starts ======================================================================
collected 3 items                                                                                                                                                

Test/test_mod1.py::test_01 FAILED                                                                                                                         [ 33%]
Test/test_mod1.py::test_02 FAILED                                                                                                                         [ 66%] 
Test/test_mod1.py::test_03 FAILED                                                                                                                         [100%] 

=========================================================================== FAILURES =========================================================================== 
___________________________________________________________________________ test_01 ____________________________________________________________________________ 
Test\test_mod1.py:6: in test_01
    assert "a" == "b"
E   AssertionError: assert 'a' == 'b'
E     
E     - b
E     + a
___________________________________________________________________________ test_02 ____________________________________________________________________________ 
Test\test_mod1.py:9: in test_02
    assert "a" == "b"
E   AssertionError: assert 'a' == 'b'
E     
E     - b
E     + a
___________________________________________________________________________ test_03 ____________________________________________________________________________ 
Test\test_mod1.py:12: in test_03
    assert "a" == "b"
E   AssertionError: assert 'a' == 'b'
E     
E     - b
E     + a
=================================================================== short test summary info ==================================================================== 
FAILED Test/test_mod1.py::test_01 - AssertionError: assert 'a' == 'b'
FAILED Test/test_mod1.py::test_02 - AssertionError: assert 'a' == 'b'
FAILED Test/test_mod1.py::test_03 - AssertionError: assert 'a' == 'b'
====================================================================== 3 failed in 0.07s ======================================================================= 
  1. –tb=line:单行输出错误文件、行号和错误摘要
===================================================================== test session starts ======================================================================
collected 3 items                                                                                                                                                

Test/test_mod1.py::test_01 FAILED                                                                                                                         [ 33%] 
Test/test_mod1.py::test_02 FAILED                                                                                                                         [ 66%] 
Test/test_mod1.py::test_03 FAILED                                                                                                                         [100%] 

=========================================================================== FAILURES =========================================================================== 
D:\Projects\Python\PythonTest\Test\test_mod1.py:6: AssertionError: assert 'a' == 'b'
D:\Projects\Python\PythonTest\Test\test_mod1.py:9: AssertionError: assert 'a' == 'b'
D:\Projects\Python\PythonTest\Test\test_mod1.py:12: AssertionError: assert 'a' == 'b'
=================================================================== short test summary info ==================================================================== 
FAILED Test/test_mod1.py::test_01 - AssertionError: assert 'a' == 'b'
FAILED Test/test_mod1.py::test_02 - AssertionError: assert 'a' == 'b'
FAILED Test/test_mod1.py::test_03 - AssertionError: assert 'a' == 'b'
====================================================================== 3 failed in 0.01s ======================================================================= 
  1. –tb=no:不显示 FAILURES 信息
===================================================================== test session starts ======================================================================
collected 3 items                                                                                                                                                

Test/test_mod1.py::test_01 FAILED                                                                                                                         [ 33%] 
Test/test_mod1.py::test_02 FAILED                                                                                                                         [ 66%] 
Test/test_mod1.py::test_03 FAILED                                                                                                                         [100%] 

=================================================================== short test summary info ==================================================================== 
FAILED Test/test_mod1.py::test_01 - AssertionError: assert 'a' == 'b'
FAILED Test/test_mod1.py::test_02 - AssertionError: assert 'a' == 'b'
FAILED Test/test_mod1.py::test_03 - AssertionError: assert 'a' == 'b'
====================================================================== 3 failed in 0.01s ======================================================================= 

END



© 转载需要保留原始链接,未经明确许可,禁止商业使用。CC BY-NC-ND 4.0