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. Pulsar负载均衡原理及优化

    前言 前段时间我们在升级 Pulsar 版本的时候发现升级后最后一个节点始终没有流量. 虽然对业务使用没有任何影响,但负载不均会导致资源的浪费. 和同事沟通后得知之前的升级也会出现这样的情况,最终还是 ...

  2. 【Django drf】视图层大总结 ViewSetMixin源码分析 路由系统 action装饰器

    目录 九个视图子类 视图集 继承ModelViewSet类写五个接口 继承 ReadOnlyModelView编写2个只读接口 ViewSetMixin源码分析 查找as_view方法 setattr ...

  3. vue学习笔记(一)---- vue指令(浪起来~~~哦耶 的案例)

    案例实现分析: 把第一个字符追加到最后一个字符身上去 基本结构: <body> <div id="app"> <input type="bu ...

  4. pdf转图片加水印压缩

    ''' pip install pymupdf pip install pillow ''' import os import uuid import fitz from PIL import Ima ...

  5. Html音频播放代码

    页面代码: <html> <head> <script src="https://code.jquery.com/jquery-3.1.1.min.js&quo ...

  6. h5与原生app通信的各种功能

    import config from '@/config/index'; import cubeModule from '_public/CubeModule.json'; const _MIDEA_ ...

  7. 以docker方式部署的redis键值查询及清理

    1.首先使用 docker ps 命令来查看正在运行的容器.该命令会列出容器的ID.名称.端口号.状态等信息.也可以使用 docker ps -a 命令来查看所有容器,包括已经停止的容器. docke ...

  8. Mac 创建Python3虚拟环境

    Mac 创建Python3虚拟环境 1.安装virtualenv pip3 install virtualenv 安装virtualenvwrapper pip3 install virtualenv ...

  9. 网关与网络地址(网络号)以及IP地址、广播地址

    转载新浪博客: http://blog.sina.com.cn/s/blog_406127500101i8bp.html

  10. 脊柱关节病外周关节滑膜高表达的RANK/RANKL/OPG系统与炎症呈部分分离

    脊柱关节病外周关节滑膜高表达的RANK/RANKL/OPG系统与炎症呈部分分离Vandooren B, et al. Arthritis Rheum. 2008;58:718-729目的:脊 柱关节病 ...