前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Pytest框架 | 运行参数实战案例剖析

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

作者头像
测试小兵
发布2022-06-07 20:46:13
4460
发布2022-06-07 20:46:13
举报
文章被收录于专栏:猪圈子猪圈子

上期回顾

  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参数

代码语言:javascript
复制
// 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+,
  • 所以当这种方式面临着几百条测试用例的时候那么我们的耗时是不是更长,此时就需要 -n 参数来解决问题
  • pytest -vs test_one_case.py -n 2 运行方式加-n参数进行运行,n=2意味着代表2个线程,看看效果
  • 发现没有当我们加了-n参数后 n=2 启动2个线程时,耗时减半了,大家还可以试试n=3 n=4的时候效果

-reruns参数

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

代码语言:javascript
复制
// 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重试参数,正常运行到第三条测试用例进行报错了
  • 我们来试试进行加上--reruns的效果,注意哈当我们在实际命令编写时,是使用的--reruns 2 后面接上重新运行的次数,后面接2就代表重新运行2次
  • pytest -vs test_one_case.py --reruns 2 当我们加了--reruns 2 参数后我们发现第三条错误的用例,按照预期进行重试了2次

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

  1. 做过UI自动化的同学都知道,我们很多测试用例都是基于前端页面元素加载完毕后,使用selenium的内置方法模拟人工进行UI自动化测试
  2. 如果当某次执行时页面元素因某些原因未成功加载完毕,此时我们的测试用例运行时捕捉不到页面元素,则会进行报错
  3. 所以如果我们运用到--reruns参数进行重试的目的,就是为了重试这类运行错误的测试用例二次校验是不是真的失败。
  4. 以上就是针对于-n、-reruns 运行参数的剖析,后期大家如果使用到pytest框架则会明白其中的好处,希望对大家带来帮助
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2022-04-15,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Python测试社区 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 上期回顾
  • 本期介绍
  • 快速安装模块包
  • 进入主题运行参数
    • -n参数
      • -reruns参数
        • 划重点:--reruns参数的作用,
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档