• 【Pytest学习】重复执行用例插件之pytestrepeat的详细使用


    前言

    • 平常在做功能测试的时候,经常会遇到某个模块不稳定,偶然会出现一些bug,对于这种问题我们会针对此用例反复执行多次,最终复现出问题来
    • 自动化运行用例时候,也会出现偶然的bug,可以针对单个用例,或者针对某个模块的用例重复执行多次

    环境前提

    • Python 2.7、3.4+或PyPy
    • py.test 2.8或更高版本

    安装插件

    pip3 install pytest-repeat -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com

    快速入门

    结合之前讲到的失败重跑、输出html报告插件来敲命令行

    两种方式皆可,等号或空格

    • count=2
    • count 2
    pytest --html=report.html --self-contained-html  -s --reruns=5 --count=2 10fixture_request.py

    重复测试直到失败(重点!)

    • 如果需要验证偶现问题,可以一次又一次地运行相同的测试直到失败,这个插件将很有用
    • 可以将pytest的 -x 选项与pytest-repeat结合使用,以强制测试运行程序在第一次失败时停止
    py.test --count=1000 -x test_file.py

    小栗子

    def test_example():
        import random
        flag = random.choice([True, False])
        print(flag)
        assert flag

    执行命令

      pytest -s --count 5 -x 13repeat.py

    执行结果

    @pytest.mark.repeat(count)

    如果要在代码中将某些测试用例标记为执行重复多次,可以使用 @pytest.mark.repeat(count) 

    @pytest.mark.repeat(5)
    def test_repeat():
        print("测试用例执行")

    执行命令

    pytest -s 13repeat.py

    执行结果

    --repeat-scope

    命令行参数

    作用:可以覆盖默认的测试用例执行顺序,类似fixture的scope参数

    • function:默认,范围针对每个用例重复执行,再执行下一个用例
    • class:以class为用例集合单位,重复执行class里面的用例,再执行下一个
    • module:以模块为单位,重复执行模块里面的用例,再执行下一个
    • session:重复整个测试会话,即所有测试用例的执行一次,然后再执行第二次
     

    案例一:class

     
    class Test_repeat:
        def test_repeat3(self):
            print("测试用例执行333")
    
    class Test_repeat2:
        def test_repeat3(self):
            print("测试用例执行444")
     

    执行命令

    pytest -s --count=2 --repeat-scope=class 13repeat.py

    执行结果

    案例二:module

     
    def test_repeat1():
        print("测试用例执行111")
    
    
    def test_repeat2():
        print("测试用例执行222")
    
    
    class Test_repeat:
        def test_repeat3(self):
            print("测试用例执行333")
     

    执行命令

    pytest -s --count=2 --repeat-scope=module 13repeat.py

    执行结果

    兼容性问题

    pytest-repeat不能与unittest.TestCase测试类一起使用。无论--count设置多少,这些测试始终仅运行一次,并显示警告

    转载:https://www.cnblogs.com/poloyy/p/12691240.html

  • 相关阅读:
    『笔记』数学数论(八)
    『笔记』BSGS
    『笔记』组合数学(六)
    01 分数规划
    高斯消元
    拉格朗日插值法
    洛谷网课数论
    [IOI2013]robots 机器人
    P3530 [POI2012]FES-Festival
    NOIP 2015 day1
  • 原文地址:https://www.cnblogs.com/gtea/p/15650426.html
Copyright © 2020-2023  润新知