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+,
Pytest框架 | 运行参数实战案例剖析

  • 所以当这种方式面临着几百条测试用例的时候那么我们的耗时是不是更长,此时就需要 -n 参数来解决问题
  • pytest -vs test_one_case.py -n 2 运行方式加-n参数进行运行,n=2意味着代表2个线程,看看效果
Pytest框架 | 运行参数实战案例剖析

  • 发现没有当我们加了-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重试参数,正常运行到第三条测试用例进行报错了
Pytest框架 | 运行参数实战案例剖析

  • 我们来试试进行加上--reruns的效果,注意哈当我们在实际命令编写时,是使用的--reruns 2 后面接上重新运行的次数,后面接2就代表重新运行2次
  • pytest -vs test_one_case.py --reruns 2 当我们加了--reruns 2 参数后我们发现第三条错误的用例,按照预期进行重试了2次
Pytest框架 | 运行参数实战案例剖析

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

  1. 做过UI自动化的同学都知道,我们很多测试用例都是基于前端页面元素加载完毕后,使用selenium的内置方法模拟人工进行UI自动化测试
  2. 如果当某次执行时页面元素因某些原因未成功加载完毕,此时我们的测试用例运行时捕捉不到页面元素,则会进行报错
  3. 所以如果我们运用到--reruns参数进行重试的目的,就是为了重试这类运行错误的测试用例二次校验是不是真的失败。
  4. 以上就是针对于-n、-reruns 运行参数的剖析,后期大家如果使用到pytest框架则会明白其中的好处,希望对大家带来帮助

最后我也整理了一些软件测试学习资料,对于学软件测试的小伙伴来说应该会很有帮助,为了更好地整理每个模块

需要的私信我关键字【555】免费获取哦 注意关键字是:555

全套软件测试自动化测试教学视频

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

300G教程资料下载【视频教程+PPT+项目源码】

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

全套软件测试自动化测试大厂面经

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




发表评论
留言与评论(共有 0 条评论) “”
   
验证码:

相关文章

推荐文章