Pytest学习2

用法和调用
通过调用python -m pytest进行测试

我们可以从命令行通过Python解释器调用测试:

python -m pytest [...]

这基本上等同于直接调用命令行脚本,出了那在python中调用,也可将将之添加到sys.path中

退出代码的可能及编号

运行pytest可以产生六种不同的退出代码
退出代码为0: 收集并成功通过所有测试
退出代码为1: 收集并运行测试但一些测试失败
退出代码为2: 用户终端了测试执行
退出代码为3: 执行测试时发生内部错误
退出代码为4: pytest命令行使用错误
退出代码为5: 没有收集任何测试

它们由_pytest.main.ExitCode枚举表示.可以使用以下命令直接导入和访问作为公共API一部分的退出代码
from pytest import ExitCode

Pytest支持集中从命令行运行和选择测试的方法

在模块中运行测试
pytest test_mod.py
在目录中运行测试
pytest testing/
按关键字表达式运行测试
pytest -k "MyClass and not method"
这将运行包括与给定字符串表达式匹配的名称的测试,该表达式可以包括使用文件名,类名和函数名作为变量的Python运算符.
以上示例将运行TestMyClass.test_something但不会运行TestMyClass.test_method_simple.

按照节点ID运行测试

每个收集的测试都分配一个唯一的nodeid,包括模块文件名,后面跟说明符,比如类名,函数名和参数化参数,用::字符分隔

在模块中运行特定测试:
pytest test_mod.py::test_func

另一个在命令中指定测试方法的示例:

pytest test_mod.py::TestClass::test_method
通过标记表达式运行测试
pytest -m slow

将运行@pytest.mark.slow装饰器修饰的所有测试.

从包中运行测试
pytest --pyargs.pkg.testing

这将导入pkg.testing并使用其文件系统位置来查找和运行测试

修改Python回溯打印

修改回溯打印的示例:

pytest --showlocals # show local variables in tracebacks
pytest -l           # show local variables (shortcut)

pytest --tb=auto    # (default) 'long' tracebacks for the first and last
                     # entry, but 'short' style for the other entries
pytest --tb=long    # exhaustive, informative traceback formatting
pytest --tb=short   # shorter traceback format
pytest --tb=line    # only one line per failure
pytest --tb=native  # Python standard library formatting
pytest --tb=no      # no traceback at all

详细的总结报告

-r标志可用于在测试会话结束时显示’简短测试摘要信息’,使大型测试套件可以轻松获得所有故障,跳过,xfails等的清晰图像.
例:

# content of test_example.py
import pytest


@pytest.fixture
def error_fixture():
    assert 0


def test_ok():
    print("ok")


def test_fail():
    assert 0


def test_error(error_fixture):
    pass


def test_skip():
    pytest.skip("skipping this test")


def test_xfail():
    pytest.xfail("xfailing this test")


@pytest.mark.xfail(reason="always xfail")
def test_xpass():
    pass

测试后

==================================================== test session starts =====================================================
platform darwin -- Python 2.7.10, pytest-4.6.5, py-1.8.0, pluggy-0.13.0
rootdir: /Users/admin/Desktop/pytest_zzz
collected 6 items                                                                                                            

xfails_test.py .FEsxX                                                                                                  [100%]

=========================================================== ERRORS ===========================================================
________________________________________________ ERROR at setup of test_error ________________________________________________

    @pytest.fixture
    def error_fixture():
>       assert 0
E       assert 0

xfails_test.py:7: AssertionError
========================================================== FAILURES ==========================================================
_________________________________________________________ test_fail __________________________________________________________

    def test_fail():
>       assert 0
E       assert 0

xfails_test.py:15: AssertionError
================================================== short test summary info ===================================================
SKIPPED [1] /Users/admin/Desktop/pytest_zzz/xfails_test.py:24: skipping this test
XFAIL xfails_test.py::test_xfail
  reason: xfailing this test
XPASS xfails_test.py::test_xpass always xfail
ERROR xfails_test.py::test_error - assert 0
FAILED xfails_test.py::test_fail - assert 0
======================== 1 failed, 1 passed, 1 skipped, 1 xfailed, 1 xpassed, 1 error in 0.04 seconds ========================

描述-r在它之后选项接收数量的字符,使用a的意思是’所有排除被通过’
这里是可以使用的可用字符的完整列表

  • f ===>失败
  • E ===>错误
  • s ===>跳过
  • x ===>xfailed
  • X ===>xpassed
  • p ===>过去了
  • p ===>通过输出
  • a ===>除了pP
  • A ===>全部

可以使用多个字符,例如,只能查看失败和跳过的测试,我们可以执行

$ pytest -rfs
=========================== test session starts ============================
platform linux -- Python 3.x.y, pytest-5.x.y, py-1.x.y, pluggy-0.x.y
cachedir: $PYTHON_PREFIX/.pytest_cache
rootdir: $REGENDOC_TMPDIR
collected 6 items

test_example.py .FEsxX                                               [100%]

================================== ERRORS ==================================
_______________________ ERROR at setup of test_error _______________________

    @pytest.fixture
    def error_fixture():
>       assert 0
E       assert 0

test_example.py:6: AssertionError
================================= FAILURES =================================
________________________________ test_fail _________________________________

    def test_fail():
>       assert 0
E       assert 0

test_example.py:14: AssertionError
========================= short test summary info ==========================
FAILED test_example.py::test_fail - assert 0
SKIPPED [1] $REGENDOC_TMPDIR/test_example.py:23: skipping this test
== 1 failed, 1 passed, 1 skipped, 1 xfailed, 1 xpassed, 1 error in 0.12s ===

使用p列表通过测试,同时P添加额外的’PASSES’部分,其中包含已通过但已捕获输出的测试:

$ pytest -rpP
=========================== test session starts ============================
platform linux -- Python 3.x.y, pytest-5.x.y, py-1.x.y, pluggy-0.x.y
cachedir: $PYTHON_PREFIX/.pytest_cache
rootdir: $REGENDOC_TMPDIR
collected 6 items

test_example.py .FEsxX                                               [100%]

================================== ERRORS ==================================
_______________________ ERROR at setup of test_error _______________________

    @pytest.fixture
    def error_fixture():
>       assert 0
E       assert 0

test_example.py:6: AssertionError
================================= FAILURES =================================
________________________________ test_fail _________________________________

    def test_fail():
>       assert 0
E       assert 0

test_example.py:14: AssertionError
================================== PASSES ==================================
_________________________________ test_ok __________________________________
--------------------------- Captured stdout call ---------------------------
ok
========================= short test summary info ==========================
PASSED test_example.py::test_ok
== 1 failed, 1 passed, 1 skipped, 1 xfailed, 1 xpassed, 1 error in 0.12s ===
在PDB(Python Debugger) 开始测试时进行删除

pytest允许用户通过命令行选项在每次测试开始时立即进入PDB提示符:

pytest --trace

这将在每次测试开始时调用Python调试器

设置断点

要在代码中设置断点,请在代码中使用本机Python调用,pytest会自动禁用该测试的输出捕获:import pdb;pdb.set_trace()

  • 其他测试中的输出不受影响
  • 任何先前的测试输出已经被捕获并将被处理
  • 结束调试器会话时(通过continue命令),输出捕获将恢复.
分析测试执行持续时间

要获得最慢的10个测试持续时间的列表:

pytest --durations=10
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值