Pytest框架 | 运行参数实战案例剖析

b0e1e45f5aa36127070f8019fc33d9f7.png

上期回顾

  1. Pytest的配置:AutoTest-Pytest环境安装配置

  2. Pytest运行参数说明:Pytest常用参数剖析 | 案例演示

本期介绍

  • 今天给大家再进行讲解下Pytest运行时另外几个份量级参数 -n、-reruns

    • -n NUM: pytest-xdist多线程运行(需要先安装pyest-xdist)

    • --reruns NUM:重试运行测试用例(需要先安装pytest-rerunfailures)

快速安装模块包

  • 在Pycharm新建requirements.txt文件,在文件里录入你这边需要安装的模块信息

    • pytest

    • pytest-html

    • pytest-xdist

    • pytest-ordering

    • pytest-rerunfailures

    • allure-pytest

  1. 在pycahrm | Termianl命令行输入pip install -r requirements.txt进行快速安装

  2. 安装完毕即可正常使用文本里的模块

进入主题运行参数

-n参数

// FileName: HelloWorld.python
# -*- coding:utf-8 -*-
# auth:shichao 
# Email:695214599@qq.com

import pytest
import time


class TestLogin:
    def test_01_shichao(self):
        time.sleep(2)
        print('这是第1条测试用例')
        assert 1 < 2

    def test_02_shichao(self):
        time.sleep(2)
        print('这是第2条测试用例')
        a = "测试"
        assert "测" in a

    def test_03_shichao(self):
        time.sleep(2)
        print('这是第3条测试用例')

    def test_04_shichao(self):
        time.sleep(2)
        print('这是第4条测试用例')

    def test_05_shichao(self):
        time.sleep(2)
        print('这是第5条测试用例')

    def test_06_shichao(self):
        time.sleep(2)
        print('这是第6条测试用例')


if __name__ == '__main__':
    pytest.main()
  • pytest -vs test_one_case.py 运行参数正常运行,未加多线程执行参数测试用例运行总耗时12s+,

    7aec882458101b82cb617b137f431555.png

  • 所以当这种方式面临着几百条测试用例的时候那么我们的耗时是不是更长,此时就需要 -n 参数来解决问题

  • pytest -vs test_one_case.py -n 2 运行方式加-n参数进行运行,n=2意味着代表2个线程,看看效果

    6d7959392ae4862d2abac9c0c2d83a64.png

  • 发现没有当我们加了-n参数后 n=2 启动2个线程时,耗时减半了,大家还可以试试n=3 n=4的时候效果

-reruns参数

这里我们将第三条测试用例写一个错误的断言,先进行运行看是否报错,再看看我们运用重试参数-reruns的效果

// FileName: HelloWorld.python
# -*- coding:utf-8 -*-
# auth:shichao 
# Email:695214599@qq.com

import pytest
import time


class TestLogin:
    def test_01_shichao(self):
        time.sleep(2)
        print('这是第1条测试用例')
        assert 1 < 2

    def test_02_shichao(self):
        time.sleep(2)
        print('这是第2条测试用例')
        a = "测试"
        assert "测" in a

    def test_03_shichao(self):
        time.sleep(2)
        print('这是第3条测试用例')
        assert 1 == 2
        print('这里有个错误的断言,来试试我们的重试机制')

    def test_04_shichao(self):
        time.sleep(2)
        print('这是第4条测试用例')

    def test_05_shichao(self):
        time.sleep(2)
        print('这是第5条测试用例')

    def test_06_shichao(self):
        time.sleep(2)
        print('这是第6条测试用例')


if __name__ == '__main__':
    pytest.main()
  • pytest -vs test_one_case.py 运行参数正常运行,未加-reruns重试参数,正常运行到第三条测试用例进行报错了

    93858fa4881c0fc47ada6098f43c4cb1.png

  • 我们来试试进行加上--reruns的效果,注意哈当我们在实际命令编写时,是使用的--reruns 2 后面接上重新运行的次数,后面接2就代表重新运行2次

  • pytest -vs test_one_case.py --reruns 2 当我们加了--reruns 2 参数后我们发现第三条错误的用例,按照预期进行重试了2次

    d2657258f5acc50ead8a2aa77bb2f386.png

划重点:--reruns参数的作用,

  1. 做过UI自动化的同学都知道,我们很多测试用例都是基于前端页面元素加载完毕后,使用selenium的内置方法模拟人工进行UI自动化测试

  2. 如果当某次执行时页面元素因某些原因未成功加载完毕,此时我们的测试用例运行时捕捉不到页面元素,则会进行报错

  3. 所以如果我们运用到--reruns参数进行重试的目的,就是为了重试这类运行错误的测试用例二次校验是不是真的失败。

  4. 以上就是针对于-n、-reruns 运行参数的剖析,后期大家如果使用到pytest框架则会明白其中的好处,希望对大家带来帮助

支持小编持续输出好文,请点赞、分享朋友圈一下吧!

加小编微信:695214599,一起共事,进步,沉淀!

上一篇:接口测试系列 | 实战演练

8307068f58502aaa2a7c72f9906f0349.png

点击阅读原文,查看更多精彩文章

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值