Python测试框架pytest系列可以查看下列
Python测试框架pytest(01)简介、安装、快速入门_编程简单学的博客-CSDN博客

Python测试框架pytest(02)PyCharm设置运行pytest、pytest.main()_编程简单学的博客-CSDN博客
软件测试资料领取方式 (#1) · Issue · 可可爱爱的程序员 / 软件测试资料合集 · GIT CODE
————————————————

1、在测试用例里面断言失败,结果为failed。

创建test_fixture_failed.py文件

脚本代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公众号:AllTests软件测试
"""import pytest@pytest.fixture()
def user():print("用户名")name = "AllTests软件测试"return namedef test_case(user):assert user == "软件测试"  # 测试用例失败结果为failedif __name__ == "__main__":pytest.main(["-s", "test_fixture_failed.py"])
复制代码

运行结果:

测试用例失败结果为failed。

2、在fixture里面断言失败,结果为error。

创建test_fixture_error.py文件

脚本代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公众号:AllTests软件测试
"""import pytest@pytest.fixture()
def user():print("用户名")name = "AllTests软件测试"assert name == "软件测试"  # fixture失败结果为errorreturn namedef test_case(user):assert user == "AllTests软件测试"if __name__ == "__main__":pytest.main(["-s", "test_fixture_error.py"])
复制代码

运行结果:

fixture失败结果为error。

2、fixture的实例化顺序

  • fixture 的 scope 实例化优先级:session > package > module > class > function。即较高 scope 范围的 fixture(session)在较低 scope 范围的 fixture( function 、 class )之前实例化。
  • 具有相同作用域的 fixture 遵循测试函数中声明的顺序,并遵循 fixture 之间的依赖关系。在 fixture_A 里面依赖的 fixture_B 优先实例化,然后到 fixture_A 实例化。
  • 自动使用(autouse=True)的 fixture 将在显式使用(传参或装饰器)的 fixture 之前实例化。

1、创建test_fixture2.py文件

脚本代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公众号:AllTests软件测试
"""
import pytestorder = []@pytest.fixture(scope="session")
def s1():order.append("s1")@pytest.fixture(scope="package")
def p1():order.append("p1")@pytest.fixture(scope="module")
def m1():order.append("m1")@pytest.fixture(scope="class")
def c1():order.append("c1")@pytest.fixture(scope="function")
def f0():order.append("f0")@pytest.fixture
def f1(f3, a1):# 先实例化f3, 再实例化a1, 最后实例化f1order.append("f1")assert f3 == 123@pytest.fixture
def f3():order.append("f3")a = 123yield a@pytest.fixture
def a1():order.append("a1")@pytest.fixture
def f2():order.append("f2")def test_order(f1, c1, m1, f0, f2, s1, p1):# 按scope的优先级,按顺序执行s1,p1,m1,c1,f1(优先执行f3,之后a1,最后f1),f0,f2assert order == ["s1", "p1", "m1", "c1", "f3", "a1", "f1", "f0", "f2"]
复制代码

2、运行结果:断言成功

按 scope 的优先级,按顺序执行 s1,p1,m1,c1,f1(优先执行f3,之后a1,最后f1),f0,f2

3、使用多个fixture

1、创建test_fixture_2.py文件

脚本代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公众号:AllTests软件测试
"""
import pytest@pytest.fixture()
def fixture_one():print("====fixture_one====")@pytest.fixture()
def fixture_two():print("====fixture_two====")def test_case(fixture_two, fixture_one):print("====执行用例====")
复制代码

2、运行结果:

test_case函数执行前按顺序先执行fixture_two,之后执行fixture_one。

Python测试框架pytest(05)fixture - error和failed、fixture实例化、多个fixture相关推荐

  1. Python测试框架pytest(04)fixture - 测试用例调用fixture、fixture传递测试数据

    Python测试框架pytest系列可以查看下列 Python测试框架pytest(01)简介.安装.快速入门_编程简单学的博客-CSDN博客 Python测试框架pytest(02)PyCharm设 ...

  2. Python测试框架pytest(03)setup和teardown

    Python测试框架pytest系列可以查看下列 Python测试框架pytest(01)简介.安装.快速入门_编程简单学的博客-CSDN博客 ​​​​​​Python测试框架pytest(02)Py ...

  3. Python测试框架Pytest的基础入门

    Pytest简介 Pytest is a mature full-featured Python testing tool that helps you write better programs.T ...

  4. Python测试框架pytest(01)简介、安装、快速入门

    1.简介 pytest 是成熟的功能齐全的 Python 测试工具,可帮助你编写更好的程序. pytest 是一个使构建简单和可伸缩的测试变得容易的框架.测试具有表达性和可读性,不需要样板代码.几分钟 ...

  5. Python 测试框架pytest

    1.简介 pytest 是一个成熟的.功能齐全的 Python 测试工具,帮助你编写更好的程序. 2.特点 1)容易上手,入门简单,文档丰富,文档中有很多demo学习 2)可以支持简单的单元测试.复杂 ...

  6. Python测试框架pytest(23)插件 - pytest-picked、pytest-lazy-fixture

    目录 1.pytest-picked(运行未提交的git用例) 1.1.安装 1.2.参数 1.3.用法 1.4.示例 2.pytest-lazy-fixture(在pytest.mark.param ...

  7. Python测试框架pytest(22)插件 - pytest-xdist(分布式执行)

    目录 1.安装 2.示例 3.原理和流程 4.解决:多进程运行次数 当测试用例非常多的时候,一条条按顺序执行测试用例,是很浪费测试时间的.这时候就可以用到 pytest-xdist,让自动化测试用例可 ...

  8. Python测试框架pytest(02)PyCharm设置运行pytest、pytest.main()

    1.PyCharm设置运行pytest 打开PyCharm,依次打开Preferences--->Tools--->Python Integrated Tools,将Testing里的De ...

  9. python测试用例管理_Python测试框架Pytest的常用插件测试报告

    原标题:Python测试框架Pytest的常用插件测试报告 一.pytest-html 生成 html 测试报告 要求:Python 3.6+ 安装:pip install pytest-html 文 ...

最新文章

  1. MDaemon 10.1.2 通过Webclient发邮件时,提示“发邮件时发生错误
  2. 软件工程第一周开课博客
  3. Systemd 入门及常用命令
  4. Android --- SDK 和 API 是什么意思
  5. java基础1--继承
  6. oracle 9i从入门到精通读书笔记2
  7. 《5》CentOS7.0+OpenStack+kvm云平台的部署—组态Horizon
  8. kafka Centos7.2 单机集群搭建
  9. 巨人网络辟谣史玉柱被警方带走:下午一直在上海总部开会
  10. 6.Hibernate多对多关系建立
  11. 2021年低压电工考试题及低压电工模拟考试
  12. Windows10应用磁贴如何显示
  13. 16MHz贴片晶振智能电子产品的好帮手
  14. 集成安装光盘制作教程(http://bbs.deepin.org/viewthread.php?tid=1170144)
  15. 股票交易接口-Java封装
  16. keil软件下载安装与新建工程
  17. H5本地调试微信静默授权、授权获取用户信息
  18. 24GHz多通道毫米波雷达传感器开发-前言
  19. 如何批量修改云服务器BCC实例名称
  20. scons 自定义命令行参数传递

热门文章

  1. R语言ggplot2可视化箱图(boxplot)并使用ggsignif添加分组显著性(significance)标签
  2. R语言使用fs包的dir_delete函数删除指定的文件目录(remove the directory)、举一反三、file_delete函数、link_delete函数可以用来删除文件和文件夹
  3. R语言ggplot2可视化:ggplot2可视化直方图(histogram)并在直方图的顶部外侧(top upper)或者直方图内部添加数值标签
  4. Error: Aesthetics must be either length 1 or the same as the data (5): fill
  5. 基于网格的聚类算法STING
  6. R假设检验之Breusch-Pagan检验(Breusch-Pagan Test)
  7. 计算机组成原理实验报告名,计算机组成原理的实验报告
  8. 无损卡尔曼滤波UKF与多传感器融合
  9. 三代测序数据分析之文献推荐
  10. 树莓派开发3-wiring库控制继电器和继电器组