1. 执行标记用例执行次数

首先安装 repeat:

pip install pytest-repeat

@pytest.mark.repeat(n)执行当前用例 n 次 然后再往下执行其他用例

import pytest

class TestCase:

    def test_01(self):
print("\ntest_01") @pytest.mark.repeat(2)
def test_02(self):
print("\ntest_02") def test_03(self):
print("\ntest_03") if __name__ == '__main__':
pytest.main()

  

2. 调整用例的执行顺序

首先安装 ordering:

pip install pytest-ordering

@pytest.mark.last 最后一个执行用例

@pytest.mark.run(order=1) 第一个执行用例

import pytest

class TestCase:

    def test_01(self):
print("\ntest_01") @pytest.mark.last()
def test_02(self):
print("\ntest_02") @pytest.mark.run(order=1)
def test_03(self):
print("\ntest_03") if __name__ == '__main__':
pytest.main()

  

3. 用例之间的依赖关系

  • 这是一个pytest第三方插件,主要解决用例之间的依赖关系。如果依赖的上下文失败后续的用例会被标识为跳过执行,相当于执行了pytest.mark.skip
  • dependency可作用的范围有:sessionpackagemoduleclass
  • 安装 pip install pytest-dependency
    • 首先我们需要在用例开始的位置打上一个装饰器@pytest.mark.dependency(),这是代表这条用例作为主条件,如果这条用例失败,关联它的用例会跳过执行。
    • 在被关联的用例上,也打上带参数的装饰器@pytest.mark.dependency()depends接受的参数是关联的依赖用例名。
    • depends也可以用别名的方式指定用例名

  

# 类实现方式

class TestCase:

    @pytest.mark.dependency()
def test_01(self):
assert 1 ==11 @pytest.mark.dependency(depends=["TestCase::test_01"])
def test_02(self):
assert 2 == 2 if __name__ == '__main__':
pytest.main() # test_01失败 test_02跳过执行

  

  

# 函数实现方式

@pytest.mark.dependency()
def test_01():
assert 1 == 11 @pytest.mark.dependency(depends=["test_01"])
def test_02():
assert 11 == 11 if __name__ == '__main__':
pytest.main()

  

# 通过起别名

@pytest.mark.dependency(name="a")
def test_01():
assert 1 == 11 @pytest.mark.dependency(depends=["a"])
def test_02():
assert 11 == 11 if __name__ == '__main__':
pytest.main()

  

# 定义依赖范围

class TestCase1:
@pytest.mark.dependency()
def test_01(self):
assert True class TestCase2: @pytest.mark.dependency(depends=["TestCase1::test_01"], scope="class")
def test_02(self):
assert 11 == 111 if __name__ == '__main__':
pytest.main()

 

4. 多重校验 pytest-assume

正常情况下一条用例如果有多条断言,一条断言失败了,其他断言就不会执行了,而使用pytest-assume可以继续执行下面的断言

安装:pip install  pytest-assume

import pytest

def test_assume():
print('登录操作')
pytest.assume(1 == 2)
print('搜索操作')
pytest.assume(2 == 2)
print('加购操作')
pytest.assume(3 == 2)

 

5. 分布式测试(pytest-xdist)

功能测试用例非常多时,比如有1千条用例,假设每个用例执行需要1分钟,如果单个测试人员执行需要1000分钟才能跑完
当项目非常紧急时,会需要协调多个测试资源来把任务分成两部分,于是执行时间缩短一半,如果有10个小伙伴,那么执行时间就会变成十分之一,大大节省了测试时间
为了节省项目测试时间,10个测试同时并行测试,这就是一种分布式场景 分布式执行用例的原则:
1.用例之间是独立的,没有依赖关系,完全可以独立运行
2.用例执行没有顺序要求,随机顺序都能正常执行
3.每个用例都能重复运行,运行结果不会影响其他用例 插件安装:
pip3 install pytest-xdist -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com 使用方法:
pytest -n 2 (2代表2个CPU)
pytest -n auto  n auto:可以自动检测到系统的CPU核数;从测试结果来看,检测到的是逻辑处理器的数量,即假12核
 使用auto等于利用了所有CPU来跑用例,此时CPU占用率会特别高

  

6. 用例失败重跑

pip install pytest-rerunfailures

# 使用方法一: 装饰器
@pytest.mark.flaky(reruns=10, reruns_delay=1) # 重跑50次,每次间隔1s
class TestDou:
def test_case1(self):
# assert 1 == 6
assert 1 == random.randint(1, 5) # 只要在多次RERUN中遇到一次成功,即可停止,并最终结果为PASSED # 使用方法二: 命令行
class TestYin:
def test_case2(self):
assert 1 == 6 if __name__ == '__main__':
pytest.main(['--reruns', '3', '--reruns-delay', '2', ])

  

  

Pytest 插件的更多相关文章

  1. Pytest插件pytest-rerunfailures失败重跑

    Pytest插件pytest-rerunfailures失败重跑 安装 pip install pytest-rerunfailures doc https://github.com/pytest-d ...

  2. Pytest插件pytest-repeat重复执行

    Pytest插件pytest-repeat重复执行 安装 pip install pytest-repeat doc https://pypi.org/project/pytest-repeat/ h ...

  3. Pytest插件pytest-assume多重断言

    Pytest插件pytest-assume多重断言 背景 import pytest def test_assume1(): assert 1 == 2 print('hello') assert 2 ...

  4. Pytest插件pytest-order指定用例顺序

    Pytest插件pytest-order指定用例顺序 安装  pip install pytest-order 注意不是pytest-ordering 说起来这里有个故事 关于pytest-order ...

  5. Pytest插件之pytest-base-url切换测试环境

    Pytest插件之pytest-base-url切换测试环境 安装  pip install pytest-base-url 应用场景 利用参数--base-url或者配置(pytest.ini中ba ...

  6. pytest特色与实用插件

    pytest特色 1.fixture的特点 fixture是pytest特有的功能,其特点如下: 必须用pytest.fixture装饰器装饰:fixture有明确的名字,在其他函数(function ...

  7. 【pytest官方文档】解读- 插件开发之hooks 函数(钩子)

    上一节讲到如何安装和使用第三方插件,用法很简单.接下来解读下如何自己开发pytest插件. 但是,由于一个插件包含一个或多个钩子函数开发而来,所以在具体开发插件之前还需要先学习hooks函数. 一.什 ...

  8. unittest和pytest的区别

    一.用例编写规则 1.unittest提供了test cases.test suites.test fixtures.test runner相关的类,让测试更加明确.方便.可控.使用unittest编 ...

  9. Pytest 简明教程

    pytest-learn 通过文章 Python 单元测试框架之 Pytest 剖解入门(第一篇) 学习 Pytest. 有很多的第三方插件可以自定义扩展,并且支持 Allure,生成可视化的测试报告 ...

  10. Pytest+allure生成测试报告

    1.Allure.zip包的下载地址: https://github.com/allure-framework/allure2 在跳转页面选择一个allure.zip包的版本下载 若以上方法无法下载z ...

随机推荐

  1. SSM进行Query

    在查询之前,需要输入数据库字段的名称,s_id需要获取

  2. 扒一扒Bean注入到Spring的那些姿势,你会几种?

    大家好,我是三友~~ 这篇文章我准备来扒一扒Bean注入到Spring的那些姿势. 其实关于Bean注入Spring容器的方式网上也有很多相关文章,但是很多文章可能会存在以下常见的问题 注入方式总结的 ...

  3. java进阶P-2.7

    类函数 函数 用于按指定字符(串)或正则去分割某个字符串,结果以字符串数组形式返回:对某些特殊字符,如果字符(串)正好是正则的一部分,则需要转义才能使用 字符有 | , + , * , ^ , $ , ...

  4. 静态static关机子修饰成员方法-静态static的内存图

    静态static关机子修饰成员方法 静态方法 当 static 修饰成员方法时,该方法称为类方法 .静态方法在声明中有 static ,建议使用类名来调用,而不需要 创建类的对象.调用方式非常简单. ...

  5. 请求的URI过长:414 Request-URI Too Large

    问题:在项目中遇到使用get 请求,发现前端传递的参数超过nginx 服务器的限制.三种解决方法(任选一种): 1.在nginx配置文件里面把这两个缓存加大 文件位置:conf/nginx.conf ...

  6. python线程池等待全部任务结束再继续

    import json import time from concurrent.futures import ThreadPoolExecutor, wait, ALL_COMPLETED impor ...

  7. xshell连接时显示“服务器发送了一个意外的数据包。received:3,expected:20“问题的解决方法

    xshell连接时显示"服务器发送了一个意外的数据包.received:3,expected:20"问题的解决方法 解决方法:在/etc/ssh/sshd_config最后增加以下 ...

  8. python Gui编程工具详解:beeware

    各个gui开发工具对比 Flexx: 可以使用Flexx创建桌面应用程序和web应用程序,同时可以将程序导出到独立的HTML文档中,GitHub推荐 Kivy&BeeWare: 只需编写一套代 ...

  9. 为了安装alien,我更新了yum源,结果还是没装上

    前几天把自己的thinkpad E430C从win7系统装成了centos7,看过<周末折腾了两天,踩了无数个坑,终于把win7装成了centos7>的小伙伴都知道,为了把win7装成ce ...

  10. 为什么不建议使用 @Autowired 注解进行注入

    在Spring中,Bean的注入一般有三种方式:属性注入.set方法注入.构造器注入. 1.Autowired注入的原理 @Autowired属于属性注入,默认按照类型装配,默认情况下要求依赖的对象必 ...