当pytest要执行一个测试函数,这个测试函数还请求了fixture函数,那么这时候pytest就要先确定fixture的执行顺序了。

影响因素有三:

  • scope,就是fixture函数的作用范围,比如scope='class'
  • dependencies,可能会存在fixture请求了别的fixture,所以产生了依赖关系,也要考虑进去。
  • autouse,如果autouse=True,那么在作用范围内,这个fixture是最先调用的。

所以,像fixture函数或测试函数的名称、定义的位置、定义的顺序以及请求fixture的顺序,除了巧合之外,对执行顺序没有任何影响。

对于这些巧合情况,虽然pytest会尽力保持每次运行的顺序都一样,但是也难免会有意外。所以,如果我们想控制好顺序,最安全的方法还是

依赖上述三点,并且要弄清依赖关系。

一、使用范围更大的fixture函数优先执行

更大范围(比如session)的fixture会在小范围(比如函数或类)之前执行。

代码示例:

import pytest

@pytest.fixture(scope="session")
def order():
return [] @pytest.fixture
def func(order):
order.append("function") @pytest.fixture(scope="class")
def cls(order):
order.append("class") @pytest.fixture(scope="module")
def mod(order):
order.append("module") @pytest.fixture(scope="package")
def pack(order):
order.append("package") @pytest.fixture(scope="session")
def sess(order):
order.append("session") class TestClass:
def test_order(self, func, cls, mod, pack, sess, order):
assert order == ["session", "package", "module", "class", "function"]

运行结果:

test_module1.py .                                                        [100%]

============================== 1 passed in 0.01s ==============================
Process finished with exit code 0

既然运行通过,那么这些fixture函数的运行顺序就是列表里的顺序["session", "package", "module", "class", "function"]

二、相同顺序的fixture基于依赖项执行

当一个fixture函数请另一个fixture函数,另一个会先执行。

比如,fixturea请求fixtureb,需要用b返回的结果。那么b先执行,因为a依赖于b,必须得让b先执行,否则a就没法干活。

另外,即使a不需要用b返回的结果,只要a需要确保在b之后执行,a仍然可以通过请求b来控制顺序。

1.请求依赖呈线性情况下

代码示例:

import pytest

@pytest.fixture
def order():
return [] @pytest.fixture
def a(order):
order.append("a") @pytest.fixture
def b(a, order):
order.append("b") @pytest.fixture
def c(a, b, order):
order.append("c") @pytest.fixture
def d(c, b, order):
order.append("d") @pytest.fixture
def e(d, b, order):
order.append("e") @pytest.fixture
def f(e, order):
order.append("f") @pytest.fixture
def g(f, c, order):
order.append("g") def test_order(g, order):
assert order == ["a", "b", "c", "d", "e", "f", "g"]

官方给出了上述代码的依赖关系图(左)和执行顺序图(右)。



不要方,只要从测试函数test_order开始,一层一层跟着fixture的依赖一层一层梳理下去就对上了。

到这里,其实也就能更进一步理解了,如果想控制好执行顺序,就要给这些请求依赖提供足够的信息。

这样pytest能够找出一个清晰的线性依赖链,最终给调用它们的测试函数一个确定的操作顺序。

2.请求依赖不呈线性的情况,会影响操作执行

此外,如果存在歧义,出现多种执行顺序,那pytest可以在多种顺序里任选。

基于上述的请求依赖关系图(左),假设d没有请求c,那么此时的依赖关系就变成了右图所示:

可以看出:

  • c此时只被一个g请求。
  • g既请求了c,还请求了f。

因为c现在除了一个g,其他没有别的依赖关系,所以现在pytest不知道c应该是在f,e之前执行,还是应该在d之后执行。

这时候,pytest就会认定,c可以在g和b之间的任何位置点执行。也就是说,c必须在b之后和g之前执行。

如果这种情况出现,那么你预期的测试行为或者测试结果可能会受到影响。可以修改下代码,让d中没有请求c,并且我加了print,方便

看fixture的运行顺序。

运行下代码:

test_module1.py
运行order
运行a
运行b
运行d
运行e
运行f
运行c
运行g
F
demo\test_module1.py:51 (test_order)
['a', 'b', 'd...'f', 'c', ...] != ['a', 'b', 'c...'e', 'f', ...] Expected :['a', 'b', 'c...'e', 'f', ...]
Actual :['a', 'b', 'd...'f', 'c', ...]

会看到测试失败了,因为fixture的执行顺序变了,导致添加到order列表的元素顺序也变了,实际与预期结果不相等,测试失败。

不过可以从打印出的fixture运行顺序看出,c确实在b之后和g之前运行了。

官方描述这些想要表达的什么呢?

我觉得应该是这个,如果你希望精确控制执行顺序,避免顺序不对而造成执行操作或测试结果有误,那么就要给足请求依赖,好让pytest

线性制定执行顺序。

三、Autouse的fixtures,会优先执行

1. autouse的妙用

如果请求了一个autouse=True的fixture函数,那么这个autouse的fixture函数会比请求的其他fixture都要先执行。

另外,如果fixture a是autouse的,而fixture b不是。而fixture a又请求fixture b,那么fixture b也将变成autouse的fixture ,但仅适用于请求了a的测试。

其实这点也很好理解,既然a是要先执行的,a又请求了b,说明a依赖于b,那么b自然也是要先于a执行的。

在上一个例子中,由于d没有去请求c,导致依赖关系模糊,最后影响了执行结果。

但是如果c是autouse,那么b和a也就自动变成了autouse,因为c依赖于b和a。所以,这时候,c,b,a都会在其他非autouse的fixture函数之前执行。

修改下代码,在c上加上autouse:

import pytest

@pytest.fixture
def order():
print("\n运行order")
return [] @pytest.fixture
def a(order):
print("运行a")
order.append("a") @pytest.fixture
def b(a, order):
print("运行b")
order.append("b") @pytest.fixture(autouse=True)
def c(a, b, order):
print("运行c")
order.append("c") @pytest.fixture
def d(b, order):
print("运行d")
order.append("d") @pytest.fixture
def e(d, b, order):
print("运行e")
order.append("e") @pytest.fixture
def f(e, order):
print("运行f")
order.append("f") @pytest.fixture
def g(f, c, order):
print("运行g")
order.append("g") def test_order(g, order):
assert order == ["a", "b", "c", "d", "e", "f", "g"]

运行结果:

test_module1.py
运行order
运行a
运行b
运行c
运行d
运行e
运行f
运行g
. [100%] ============================== 1 passed in 0.01s ==============================
Process finished with exit code 0

执行顺序正常了,从a到g。

它们的依赖关系图变成了这样:

因为c变成了autouse,所以在图里处于d之上的位置,这时候pytest又可以将执行顺序线性化了。而且,c也让b和a都变成了autouse的fixture。

2. autouse的慎用

在使用autouse的时候也要小心。

因为一个测试函数即使没有直接请求一个autouse fixture,但是只要这个测试函数在这个autouse的作用范围内,那么这个autouse就会自动执行。

看代码示例:

import pytest

@pytest.fixture(scope="class")
def order():
return [] @pytest.fixture(scope="class", autouse=True)
def c1(order):
order.append("c1") @pytest.fixture(scope="class")
def c2(order):
order.append("c2") @pytest.fixture(scope="class")
def c3(order, c1):
order.append("c3") class TestClassWithC1Request:
def test_order(self, order, c1, c3):
assert order == ["c1", "c3"] class TestClassWithoutC1Request:
def test_order(self, order, c2):
assert order == ["c1", "c2"]

执行代码,运行case是通过的,说明order == ["c1", "c2"]

可以看到,虽然类TestClassWithoutC1Request(官方写的是TestClassWithC1Request,应该是错了)没有请求c1,但是c1还是在这个类里运行了。

但是,仅仅是一个autouse的fixture请求了一个非autouse的话,其实这并不能说这个非autouse的fixture也成为了一个可以应用到上下文的fixture函数。

仅仅是适用于请求它的那个autouse fixture的作用范围。

例如,看下面代码:

import pytest

@pytest.fixture
def order():
return [] @pytest.fixture
def c1(order):
order.append("c1") @pytest.fixture
def c2(order):
order.append("c2") class TestClassWithAutouse:
@pytest.fixture(autouse=True)
def c3(self, order, c2):
order.append("c3") def test_req(self, order, c1):
assert order == ["c2", "c3", "c1"] def test_no_req(self, order):
assert order == ["c2", "c3"] class TestClassWithoutAutouse:
def test_req(self, order, c1):
assert order == ["c1"] def test_no_req(self, order):
assert order == [] if __name__ == '__main__':
pytest.main(['-s', 'test_module2.py'])

运行都是可以通过的,这里的依赖关系图是这样的。

在类TestClassWithAutouse中:

  • test_reqtest_no_req是2个测试方法。
  • c3是autouse,并且请求了c2,所以c2也成了autouse。虽然2个测试并没去请求c2c3,但是都执行了,而且在c1之前执行。

在这里,c3请求了还order,同样地,在c3的作用域内,order也扮演了autouse的存在。但是在TestClassWithoutAutouse,order就不是

autouse了,所以类TestClassWithoutAutouse中的test_no_req可以运行成功,因为order=[]

【pytest官方文档】解读fixtures - 11. fixture的执行顺序,3要素详解(长文预警)的更多相关文章

  1. 【pytest官方文档】解读fixtures - 3. fixtures调用别的fixtures、以及fixture的复用性

    pytest最大的优点之一就是它非常灵活. 它可以将复杂的测试需求简化为更简单和有组织的函数,然后这些函数可以根据自身的需求去依赖别的函数. fixtures可以调用别的fixtures正是灵活性的体 ...

  2. 【pytest官方文档】解读fixtures - 1.什么是fixtures

    在深入了解fixture之前,让我们先看看什么是测试. 一.测试的构成 其实说白了,测试就是在特定的环境.特定的场景下.执行特定的行为,然后确认结果与期望的是否一致. 就拿最常见的登录来说,完成一次正 ...

  3. 【pytest官方文档】解读fixtures - 2. fixtures的调用方式

    既然fixtures是给执行测试做准备工作的,那么pytest如何知道哪些测试函数 或者 fixtures要用到哪一个fixtures呢? 说白了,就是fixtures的调用. 一.测试函数声明传参请 ...

  4. Cuda 9.2 CuDnn7.0 官方文档解读

    目录 Cuda 9.2 CuDnn7.0 官方文档解读 准备工作(下载) 显卡驱动重装 CUDA安装 系统要求 处理之前安装的cuda文件 下载的deb安装过程 下载的runfile的安装过程 安装完 ...

  5. 【pytest官方文档】解读fixtures - 10. fixture有效性、跨文件共享fixtures

    一.fixture有效性 fixture有效性,说白了就是fixture函数只有在它定义的使用范围内,才可以被请求到.比如,在类里面定义了一个fixture, 那么就只能是这个类中的测试函数才可以请求 ...

  6. 【pytest官方文档】解读fixtures - 7. Teardown处理,yield和addfinalizer

    当我们运行测试函数时,我们希望确保测试函数在运行结束后,可以自己清理掉对环境的影响. 这样的话,它们就不会干扰任何其他的测试函数,更不会日积月累的留下越来越多的测试数据. 用过unittest的朋友相 ...

  7. 【pytest官方文档】解读fixtures - 8. yield和addfinalizer的区别(填坑)

    在上一章中,文末留下了一个坑待填补,疑问是这样的: 目前从官方文档中看到的是 We have to be careful though, because pytest will run that fi ...

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

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

  9. 【pytest官方文档】解读- 开发可pip安装的第三方插件

    在上一篇的 hooks 函数分享中,开发了一个本地插件示例,其实已经算是在编写插件了.今天继续跟着官方文档学习更多知识点. 一个插件包含一个或多个钩子函数,pytest 正是通过调用各种钩子组成的插件 ...

随机推荐

  1. 微信小程序引入ECharts组件

    首先打开ECharts网页 https://echarts.apache.org/zh/tutorial.html#%E5%9C%A8%E5%BE%AE%E4%BF%A1%E5%B0%8F%E7%A8 ...

  2. 微信小程序:利用map方法方便获得对象数组中的特定属性值们

  3. (嘎吧)--微软的 C# , IL,CLR, Cup 之间关系以及扩展联想

    还是啰嗦下:文章短并不代表文章质量不高.我最喜欢用干货性的以及总结性的语言 让大家明白文章要表达的内容.这一切,都是来自多年对.NET 的一些领悟以及一些理解. 不长篇大论,一本人也没时间,二本人也不 ...

  4. Spring IoC - 循环依赖

    Spring 复习 3.循环依赖 3.1 定义 循环依赖指多个对象的创建过程中均需要注入对方对象,如下所示 class A{ B b; public A(){ } public A(B b){ thi ...

  5. while(1)和system("pause")区别

    我们在调试时,有时候会用到这两个语句. 1.显而易见,第一个是一个循环函数,占cpu.占内存: 2.system("pause")是一个系统调用,占内存,不占cpu;这个开销还是有 ...

  6. SpringBoot(四): SpringBoot web开发 SpringBoot使用jsp

    1.在SpringBoot中使用jsp,需要在pom.xml文件中添加依赖 <!--引入Spring Boot内嵌的Tomcat对JSP的解析包--> <dependency> ...

  7. AndroidStudio 中 gradle.properties 的中文值获取乱码问题

    0x01 现象 在gradle.properties中定义了全局变量,然后从 build.gradle 中设置 app_name: resValue "string", " ...

  8. 人脸检测数据源制作与基于caffe构架的ALEXNET神经网络训练

    本篇文章主要记录的是人脸检测数据源制作与ALEXNET网络训练实现检测到人脸(基于caffe). 1.数据获取 数据获取: ① benchmark是一个行业的基准(数据库.论文.源码.结果),例如WI ...

  9. 剑指 Offer 66. 构建乘积数组 + 思维

    剑指 Offer 66. 构建乘积数组 Offer_66 题目描述 题解分析 java代码 package com.walegarrett.offer; /** * @Author WaleGarre ...

  10. Java I/O流 04

    I/O流·其他流 序列流 * A:什么是序列流 * 序列流可以把多个字节输入流整合成一个,从序列流中读取数据时,将从被整合的第一个流开始,读完后再读下一个 * B:使用方式 * 整合两个:Sequen ...