pytest-运行模式(二)

pytest 的多种运行模式,让测试和调试变得得心应手,运行pytest时会找到当前目录及其子目录中的所有test_*.py*_test.py格式的文件以及以test开头的方法或者class, 不然就会提示找不到可以运行的case了。

  1. 运行后生成测试报告
# 安装pytest-html
pip install -U pytest-html

# 运行模式:
pytest tests.py --html=report.html
  1. 运行指定的case

当写了较多的case时,每次没必要都运行一遍,可以指定case

class TestClassOne(object):
    def test_one(self):
        x = "this"
        assert 't'in x

    def test_two(self):
        x = "hello"
        assert hasattr(x, 'check')


class TestClassTwo(object):
    def test_one(self):
        x = "iphone"
        assert 'p'in x

    def test_two(self):
        x = "apple"
        assert hasattr(x, 'check')

模式1. 运行tests.py文件中的所有case:

pytest tests.py

模式2. 运行tests.py文件中的TestClassOne这个class下的连个cases:

pytest tests.py::TestClassOne

模式3. 运行tests.py文件中的TestClassOne这个class下的test_one:

pytest tests.py::TestClassOne::test_one

注意: 定义class时,需要以Test开头,不然是不会运行该class的

  1. 多进程运行cases

当cases很多时, 运行时间也会比较长,要缩短运行时间,可以使用多进程进行运行

# 安装pytest-xdist:
pip install -U pytest-xdist

# 运行模式
pytest tests.py -n  NUM

其中 NUM 是进程数

  1. 重试运行cases

在做接口测试时,有时会遇到503或者短时的网络波动,导致case运行失败,而这并非是我们期望的结果,此时就可以使用通过重试运行cases的方式来解决

# 安装pytest-rerunfailuers
pip install -U pytest-rerunfailuers

# 运行模式
pytest tests.py --reruns NUM

其中 NUM 是进程数

  1. 显示print内容

在运行测试脚本时,为了调试或打印一些内容,我们会在代码中加一些print内容,但是在运行pytest时,这些内容不会显示出来。如果带上-s, 就可以显示了

运行模式:

pytest tests.py -s

另外,pytest 的多种运行模式可以叠加执行的,比如:运行四个进程,又想打印print内容,可以用:

pytest tests.py -s -n 4

总结自简书

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值