pytest入门

如何调用pytest

通常,pytest命令会执行在当前文件夹或者子文件夹下所有test_.py或_test.py类型的py文件。
1.通过Python -m pytest 调用pytest

python -m pytest [...]

这种调用方式等同于直接调用pytest […],但是要注意这种通过python调用的方式会同时将当前目录添加到sys.path。

1.1 退出码

pytest有六种退出码:

  • Exit 0:找到所有的测试用例并测试通过
  • Exit 1:找到所有的测试用例并运行,但是部分用例执行失败
  • Exit 2:用户中断了测试
  • Exit 3: 执行过程中发生了内部错误
  • Exit 4:pytest命令行使用出错
  • Exit 5:没有找到任何测试用例
1.2 版本信息,参数名,环境变量
pytest --version #显示pytest的import路径
pytest --fixyures #显示内置的函数参数
pytest -h | --help #帮助信息
1.3 第一(N)次失败后停止
pytest -x #第一次失败后停止测试
pytest --maxfail = 2 #第二次失败后停止测试
1.4 指定/选择测试用例

执行指定模块的用例

pytest test_mod.py

执行目录下的用例

pytest testing

指定关键字的用例:

pytest -k "MyClass and not method"  
#这种方式会执行文件名、类名以及函数名与给定的字符串表达式相匹配的测试用例。上面的表达式会执行TestMyClass.test_something但是不会执行TestMyClass.test_method_simple中的测试用例

通过节点id进行测试

pytest test_mod.py::test_func
pytest test_mod.py::TestClass::test_method
#每个被选中的测试用例都会被分配唯一的nodeid,由模块文件名和以下说明符组成:参数化的类名、函数名和参数,使用::分割。

通过标记符进行测试

pytest -m slow
#运行所有通过装饰器@pytest.mark.slow进行装饰的测试用例

通过包运行

pytest --pyargs pkg.testing
#这种方式会导入pkg.testing,并且基于该包所在位置来查找并运行测试用例
1.5 修改Python的traceback打印
pytest --sholocals #在tracebacks中显示本地变量
pytest -l #同上
pytest --tb=auto #默认该方式会显示traceback的第一条和最后一条的详细信息,其余信息简略
pytest --tb=long #详细显示所有的tracebacks
pytest --tb=short #简略显示tracebacks
pytest --tb=line #每个错误只显示一行
pytest --tb=native #以python标准库模式输出
pytest --tn=no #不输出任何tracebacks
#此外,还有比long更加详细输出的参数-full-trace**。在该参数下,ctrl+c在终端tracebacks输出的时候同时打印堆栈信息。
#这在测试耗时过长的时候使用,帮助我们找到测试用例挂死在何处
1.6 详尽的测试报告

参数**-r**可以用来在测试结束后展示一份“测试概要”,可以在大型测试集合中获取清楚的测试结果。

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("skiping this test")

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

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

输出结果为:

================================================================ test session starts ================================================================
platform win32 -- Python 3.9.1, pytest-6.2.3, py-1.10.0, pluggy-0.13.1
rootdir: D:\Users\Administrator\PycharmProjects\pytestLearn\learn
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:5: AssertionError
===================================================================== FAILURES ======================================================================
_____________________________________________________________________ test_fail _____________________________________________________________________

    def test_fail():
>       assert 0
E       assert 0

test_example.py:11: AssertionError
============================================================== short test summary info ==============================================================
SKIPPED [1] test_example.py:17: skiping this test
XFAIL test_example.py::test_xfail
  reason: xfailing this test
XPASS test_example.py::test_xpass always xfail
ERROR test_example.py::test_error - assert 0
FAILED test_example.py::test_fail - assert 0
======================================= 1 failed, 1 passed, 1 skipped, 1 xfailed, 1 xpassed, 1 error in 0.07s =======================================

-r后可以追加一些参数,a表示的是“除了pass之外的所有信息”
可以追加的参数列表:

f -failed
E -error
s -skipped
x -xfailed
X -xpassed
p -passed
P -passed with output
a -all except p

pytest -rfs 只看failed和skipped的测试结果

pytest -rfs

输出结果:

================================================================ test session starts ================================================================
platform win32 -- Python 3.9.1, pytest-6.2.3, py-1.10.0, pluggy-0.13.1
rootdir: D:\Users\Administrator\PycharmProjects\pytestLearn\learn
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:5: AssertionError
===================================================================== FAILURES ======================================================================
_____________________________________________________________________ test_fail _____________________________________________________________________

    def test_fail():
>       assert 0
E       assert 0

test_example.py:11: AssertionError
============================================================== short test summary info ==============================================================
FAILED test_example.py::test_fail - assert 0
SKIPPED [1] test_example.py:17: skiping this test
======================================= 1 failed, 1 passed, 1 skipped, 1 xfailed, 1 xpassed, 1 error in 0.06s =======================================

p可以用来显示pass的测试用例,P会在测试报告中增加一段“PASSES”的信息

pytest -rpP
================================================================ test session starts ================================================================
platform win32 -- Python 3.9.1, pytest-6.2.3, py-1.10.0, pluggy-0.13.1
rootdir: D:\Users\Administrator\PycharmProjects\pytestLearn\learn
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:5: AssertionError
===================================================================== FAILURES ======================================================================
_____________________________________________________________________ test_fail _____________________________________________________________________

    def test_fail():
>       assert 0
E       assert 0

test_example.py:11: 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.07s =======================================
1.7 测试失败时自动调用PDB

pytest允许通过命令行在测试失败的时候自动调用python的内置调试工具PDB

pytest --pdb   #每次测试失败的时候都调用pdb
pytest -x --pdb #首次失败的使用调用pdb,然后结束测试进程
pytest --pdb --maxfail=3 #前三次失败的时候调用pdb

所有异常信息都会保存在sys.last_value,sys.last_type和sys.last_tracebak中。在交互使用中,这允许使用任何调试工具进入后期测试。也可以手动访问异常信息如下:
什么意思没看懂

(Pdb)import sys
(Pdb)sys.last_traceback.tb_lineno
1448
(Pdb)sys.last_value
AssertionError('assert 0')
(Pdb)sys.last_type
<class 'AssertionError'>

pytest允许在测试启用时立即调用pdb:

pytest --trace   #在每个测试开始时立即调用python的pdb
1.8 使用内置的断点函数
1.9分析测试时间
pytest --durations=10
#默认情况下,如果测试时间很短(<0.01s)这里不会显示执行时长,如果需要显示,在命令中追加**-vv**参数
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值