简介:

  fixture区别于unnitest的传统单元测试(setup/teardown)有显著改进:

  1.有独立的命名,并通过声明它们从测试函数、模块、类或整个项目中的使用来激活。

  2.按模块化的方式实现,每个fixture都可以互相调用。

  3.fixture的范围从简单的单元测试到复杂的功能测试,可以对fixture配置参数,或者跨函数function,类class,模块module或整个测试session范围。

fixture可以当做参数传入

定义fixture跟定义普通函数差不多,唯一区别就是在函数上加个装饰器@pytest.fixture(),fixture命名不要以test开头,跟用例区分开。fixture是有返回值得,没有返回值默认为None。用例调用fixture的返回值,直接就是把fixture的函数名称当做变量名称。

# 作者         :admin
import pytest
@pytest.fixture()
def test_01():
a = 5
return a def test_02(test_01):
assert test_01 == 5
print("断言成功") ============================= test session starts =============================
platform win32 -- Python 3.7.2, pytest-4.0.2, py-1.8.0, pluggy-0.12.0
rootdir: E:\daima\Project_test\test_demo, inifile:
plugins: allure-adaptor-1.7.10, html-1.22.0, metadata-1.8.0, rerunfailures-7.0
collected 1 item test_demo_pytest_fixture.py 断言成功
. ========================== 1 passed in 0.17 seconds ===========================

使用多个fixture

如果用例需要用到多个fixture的返回数据,fixture也可以返回一个元祖,list或字典,然后从里面取出对应数据。

# 作者         :admin
import pytest
@pytest.fixture()
def test_01():
a = 5
b = 6
return (a, b) def test_02(test_01):
a = test_01[0]
b = test_01[1]
assert a < b
print("断言成功") Launching py.test with arguments E:/daima/Project_test/test_demo/test_demo_pytest_fixture.py in E:\daima\Project_test\test_demo ============================= test session starts =============================
platform win32 -- Python 3.7.2, pytest-4.0.2, py-1.8.0, pluggy-0.12.0
rootdir: E:\daima\Project_test\test_demo, inifile:
plugins: allure-adaptor-1.7.10, html-1.22.0, metadata-1.8.0, rerunfailures-7.0collected 1 item test_demo_pytest_fixture.py .断言成功
[100%] ========================== 1 passed in 0.01 seconds ===========================
Process finished with exit code 0

  

fixture的作用范围(scope)

ixture里面有个scope参数可以控制fixture的作用范围:session>module>class>function

-function:每一个函数或方法都会调用

-class:每一个类调用一次,一个类中可以有多个方法

-module:每一个.py文件调用一次,该文件内又有多个function和class

-session:是多个文件调用一次,可以跨.py文件调用,每个.py文件就是module

代码示例:

# 作者         :admin
import pytest
@pytest.fixture(scope="class")
def test_01():
a = 5
b = 6
return (a, b) class TestNum:
def test_02(self,test_01):
a = test_01[0]
b = test_01[1]
assert a < b
print("断言成功") Launching py.test with arguments E:/daima/Project_test/test_demo/test_demo_pytest_fixture.py in E:\daima\Project_test\test_demo ============================= test session starts =============================
platform win32 -- Python 3.7.2, pytest-4.0.2, py-1.8.0, pluggy-0.12.0
rootdir: E:\daima\Project_test\test_demo, inifile:
plugins: allure-adaptor-1.7.10, html-1.22.0, metadata-1.8.0, rerunfailures-7.0collected 1 item test_demo_pytest_fixture.py .断言成功
[100%] ========================== 1 passed in 0.02 seconds ===========================
Process finished with exit code 0

  

调用fixture的三种方法

1.函数或类里面方法直接传fixture的函数参数名称

注释:代码详见上图

2.使用装饰器@pytest.mark.usefixtures()修饰需要运行的用例

# 作者         :admin
import pytest
@pytest.fixture(scope="class")
def test_01():
a = 5
b = 6
return (a, b) @pytest.mark.usefixtures("test_01")
class TestNum:
def test_02(self,test_01):
a = test_01[0]
b = test_01[1]
assert a < b
print("断言成功") Launching py.test with arguments E:/daima/Project_test/test_demo/test_demo_pytest_fixture.py in E:\daima\Project_test\test_demo ============================= test session starts =============================
platform win32 -- Python 3.7.2, pytest-4.0.2, py-1.8.0, pluggy-0.12.0
rootdir: E:\daima\Project_test\test_demo, inifile:
plugins: allure-adaptor-1.7.10, html-1.22.0, metadata-1.8.0, rerunfailures-7.0collected 1 item test_demo_pytest_fixture.py .断言成功
[100%] ========================== 1 passed in 0.02 seconds ===========================
Process finished with exit code 0

3.叠加usefixtures

如果一个方法或者一个class用例想要同时调用多个fixture,可以使用@pytest.mark.usefixture()进行叠加。注意叠加顺序,先执行的放底层,后执行的放上层。

# 作者         :admin
import pytest
@pytest.fixture(scope="class")
def test_01():
a = 5
b = 6
return (a, b) @pytest.fixture(scope="class")
def test_02():
print("你是第二个执行") @pytest.mark.usefixtures("test_02")
@pytest.mark.usefixtures("test_01")
class TestNum:
def test_03(self,test_01):
a = test_01[0]
b = test_01[1]
assert a < b
print("断言成功") Launching py.test with arguments E:/daima/Project_test/test_demo/test_demo_pytest_fixture.py in E:\daima\Project_test\test_demo ============================= test session starts =============================
platform win32 -- Python 3.7.2, pytest-4.0.2, py-1.8.0, pluggy-0.12.0
rootdir: E:\daima\Project_test\test_demo, inifile:
plugins: allure-adaptor-1.7.10, html-1.22.0, metadata-1.8.0, rerunfailures-7.0collected 1 item test_demo_pytest_fixture.py 你是第二个执行
.断言成功
[100%] ========================== 1 passed in 0.02 seconds ===========================
Process finished with exit code 0

  

pytest之fixture使用详解的更多相关文章

  1. 【pytest系列】- fixture测试夹具详解

    如果想从头学起pytest,可以去看看这个系列的文章! https://www.cnblogs.com/miki-peng/category/1960108.html fixture的优势 ​ pyt ...

  2. Pytest fixture及conftest详解

    前言 fixture是在测试函数运行前后,由pytest执行的外壳函数.fixture中的代码可以定制,满足多变的测试需求,包括定义传入测试中的数据集.配置测试前系统的初始状态.为批量测试提供数据源等 ...

  3. pytest框架fixture的使用

    fixture可以当做参数传入 定义fixture跟定义普通函数差不多,唯一区别就是在函数上加个装饰器@pytest.fixture(),fixture命名不要以test开头,跟用例区分开.fixtu ...

  4. 【单元测试】NUint使用详解及Visual Studio配置

    阅读目录 什么是单元测试? 为什么使用单元测试? NUint使用详解: 示例 属性 断言 简单测试 VS配置: External Tools Visual Nunit 2010 NUnit Test ...

  5. Python安装、配置图文详解(转载)

    Python安装.配置图文详解 目录: 一. Python简介 二. 安装python 1. 在windows下安装 2. 在Linux下安装 三. 在windows下配置python集成开发环境(I ...

  6. 【和我一起学python吧】Python安装、配置图文详解

     Python安装.配置图文详解 目录: 一. Python简介 二. 安装python 1. 在windows下安装 2. 在Linux下安装 三. 在windows下配置python集成开发环境( ...

  7. NUint使用详解及Visual Studio配置

    NUint使用详解及Visual Studio配置 阅读目录 什么是单元测试? 为什么使用单元测试? NUint使用详解: 示例 属性 断言 简单测试 VS配置: External Tools Vis ...

  8. Python2.7字符编码详解

    目录 Python2.7字符编码详解 声明 一. 字符编码基础 1.1 抽象字符清单(ACR) 1.2 已编码字符集(CCS) 1.3 字符编码格式(CEF) 1.3.1 ASCII(初创) 1.3. ...

  9. N​Unit的Attribute​使​用​详​解

    NUNIT使用详解(一) 2008/08/26 11:40 NUnit是一个单元测试框架,专门针对于.NET来写的,它是是xUnit的一员.NUnit完全由C#语言来编写,并且编写时充分利用了许多.N ...

随机推荐

  1. 跟哥一起学python(3)- 理解“变量”

    我们把前面的程序稍微改一下,来了解python中的变量. # file: ./4/4_1.py # 定义变量 hello_str = "hello, world!" # 字符串打印 ...

  2. TestNG测试用例重跑详解及实践优化

    测试用例运行稳定性是自动化质量的一个重要指标,在运行中需要尽可能的剔除非bug造成的测试用例执行失败,对于失败用例进行重跑是常用策略之一.一种重跑策略是所有用例运行结束后对失败用例重跑,另一种重跑策略 ...

  3. turtle库应用实例3-叠加等边三角形绘制(一笔画)

    叠加等边三角形绘制 ‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬ ...

  4. H. Subsequences (hard version) dp

    H. Subsequences (hard version) 这个题目好难啊,根本就不知道怎么dp,看了题解,理解了好一会才会的. 首先dp[i][j] 表示前面 i  个字符,形成长度为 j  的不 ...

  5. P2765 魔术球问题 网络流二十四题重温

    P2765 魔术球问题 知识点::最小点覆盖 这个题目要拆点,这个不是因为每一个球只能用一次,而是因为我们要求最小点覆盖,所以要拆点来写. 思路: 首先拆点,然后就是开始建边,因为建边的条件是要求他们 ...

  6. Scrapy - Request 中的回调函数callback不执行

    回调函数callback不执行 大概率是被过滤了 两种方法: 在 allowed_domains 中加入目标url 在 scrapy.Request() 函数中将参数 dont_filter=True ...

  7. Redis实现分布式锁(设计模式应用实战)

    笔者看过网络上各种各样使用redis实现分布式锁的代码,要么错误,要么片段化,没有一个完整的例子,借这个周末给大家总结一下redis实现分布式锁的两种机制 自旋锁和排他锁 鉴于实现锁的方式不同,那么这 ...

  8. 【Linux基础总结】Shell 基础编程

    Shell 基础编程 重启虚拟机遇到磁盘损坏如何解决 Shell编程中变量的声明.引用及作用域 Shell程序 概述 以文件形式存放批量的Linux命令集合,该文件能够被Shell解释执行,这种文件就 ...

  9. matlab 调用C程序进行simulink仿真

    文章目录 simulink仿真 创建C程序 编译C程序 运行结果 simulink仿真 simulink仿真中需要使用S-Function模块,可以实现调用C程序进行仿真,下面先建立一个简单的仿真: ...

  10. Python语法学习第三天--元组

    元组:元组与列表相似,但是元组不能随意修改 ①创建元组 使用圆括号,用逗号隔开 空元组tuple1=()元组中只包含一个元素时,需要在元素后面添加逗号#逗号是关键tuple1=(1,) 当tuple1 ...