pytest官方文档解读fixtures调用fixtures及fixture复用性
作者:把苹果咬哭的测试笔记 时间:2021-08-14 23:01:02
fixtures调用其他fixtures及fixture复用性
pytest最大的优点之一就是它非常灵活。
它可以将复杂的测试需求简化为更简单和有组织的函数,然后这些函数可以根据自身的需求去依赖别的函数。
fixtures可以调用别的fixtures正是灵活性的体现之一。
一、Fixtures调用别的Fixtures
直接看一个简单示例:
import pytest
# Arrange
@pytest.fixture
def first_entry():
# 这是一个fixture函数,返回值:"a"
return "a"
# Arrange
@pytest.fixture
def order(first_entry):
# 这是另一个fixture函数,请求了上一个fixture函数first_entry(),
# 并且把first_entry()的返回值,放进了列表[]里,最后返回
return [first_entry]
def test_string(order):
# Act
# 测试函数中请求了第二个fixture函数order,可以拿到返回的[]
order.append("b")
# Assert
assert order == ["a", "b"]
可以看到,pytest中的某个fixture请求别的fixture,就像测试函数请求fixture一样,所有的请求规则都适用。
同样,如果这些事情换我们自己来做的话,应该是下面这样子:
def first_entry():
return "a"
def order(first_entry):
return [first_entry]
def test_string(order):
# Act
order.append("b")
# Assert
assert order == ["a", "b"]
entry = first_entry()
the_list = order(first_entry=entry)
test_string(order=the_list)
二、Fixtures的复用性
pytest中的fixtures还可以让我们像使用普通函数一样,能够定义反复重用的通用setup步骤。
两个不同的测试函数可以请求相同的fixture,每个测试函数都会获得该fixture的各自结果。
这样的优点就是,确保不同的测试函数之间不会相互影响。
我们可以使用这种机制来确保每个测试函数都获得各自新的、干净的、一致的数据。
import pytest
# Arrange
@pytest.fixture
def first_entry():
return "a"
# Arrange
@pytest.fixture
def order(first_entry):
return [first_entry]
def test_string(order):
# Act
order.append("b")
# Assert
assert order == ["a", "b"]
def test_int(order):
# Act
order.append(2)
# Assert
assert order == ["a", 2]
从代码可以看出,fixture函数order
虽然先后被两个测试函数调用,但是每次被调用给出的结果都是一样的。并不会因为在测试函数test_string
中,进行了order.append("b")
后,就影响了order
在测试函数test_int
中的返回值。
同样,这些事情换成我们自己来做,那就是这样的:
def first_entry():
return "a"
def order(first_entry):
return [first_entry]
def test_string(order):
# Act
order.append("b")
# Assert
assert order == ["a", "b"]
def test_int(order):
# Act
order.append(2)
# Assert
assert order == ["a", 2]
entry = first_entry()
the_list = order(first_entry=entry)
test_string(order=the_list)
entry = first_entry()
the_list = order(first_entry=entry)
test_int(order=the_list)
接下来,继续跟着官方文档解读fixtures的特点:一次请求多个fixtures、fixtures被多次请求。
来源:https://www.cnblogs.com/pingguo-softwaretesting/p/14473173.html
标签:pytest,fixtures,fixture,调用,复用性
0
投稿
猜你喜欢
coreseek 搜索英文的问题详解
2023-11-20 14:12:10
IE8的一些CSS hack
2008-03-17 13:08:00
Python内建类型bytes深入理解
2022-11-13 08:35:54
python抓取某汽车网数据解析html存入excel示例
2023-11-02 16:46:35
Python中的ceil()方法使用教程
2022-02-01 05:15:11
python制作抽奖程序代码详解
2022-05-04 08:51:18
pandas中按行或列的值对数据排序的实现
2023-09-23 02:32:18
python读取dicom图像示例(SimpleITK和dicom包实现)
2023-07-06 14:08:10
python爬虫实战之制作属于自己的一个IP代理模块
2021-12-23 21:42:20
Python利用ORM控制MongoDB(MongoEngine)的步骤全纪录
2023-05-08 14:22:05
Python List列表对象内置方法实例详解
2023-08-21 12:49:58
python中的json总结
2022-03-07 15:51:30
PHP file_get_contents设置超时处理方法
2023-10-18 05:56:46
php实现pdo数据库操作类过程详解
2023-05-25 11:15:05
CSS Position
2009-05-17 14:27:00
oracle 在一个存储过程中调用另一个返回游标的存储过程
2009-09-26 18:54:00
Python嵌套函数,作用域与偏函数用法实例分析
2022-12-03 08:46:15
MYSQL教程:保证安全的备份MYSQL数据库的方法
2010-03-03 17:12:00
你是真正的用户体验设计者吗? Ⅲ
2008-03-27 09:04:00
Python中关于浮点数的冷知识
2023-11-13 14:01:57