【pytest官方文档】解读fixtures - 4. 一次请求多个fixtures、fixtures被多次请求

简介: 【pytest官方文档】解读fixtures - 4. 一次请求多个fixtures、fixtures被多次请求

跟着节奏继续来探索fixtures的灵活性。


一、一个测试函数/fixture一次请求多个fixture


在测试函数和fixture函数中,每一次并不局限于请求一个fixture。他们想要多少就可以要多少。


下面是另一个简单的例子:


import pytest
# Arrange
@pytest.fixture
def first_entry():
    return "a"
# Arrange
@pytest.fixture
def second_entry():
    return 2
# Arrange
@pytest.fixture
def order(first_entry, second_entry):
    # 这是一个fixture函数,请求了2个其他的fixture函数
    return [first_entry, second_entry]
# Arrange
@pytest.fixture
def expected_list():
    return ["a", 2, 3.0]
def test_string(order, expected_list):
    # 这是一个测试函数,请求了2个不同的fixture函数
    # Act
    order.append(3.0)
    # Assert
    assert order == expected_list


可以看出,在fixture函数order中,请求了2个其他的fixture函数,分别是:first_entrysecond_entry


在测试函数test_string中,请求了2个不同的fixture函数,分别是:orderexpected_list


二、每个测试函数可以多次请求fixtures(返回值被缓存)


在同一个测试函数中,fixture也可以被请求多次。但是在这个测试函数中,pytest在第一次执行fixture函数之后,不会再次执行它们。


如果第一次执行fixture函数有返回值,那么返回值会被缓存起来。


import pytest
# Arrange
@pytest.fixture
def first_entry():
    return "a"
# Arrange
@pytest.fixture
def order():
    return []
# Act
@pytest.fixture
def append_first(order, first_entry):
    # 在这里order第一次被请求,返回一个列表[]
    # 接着,order空列表增加了first_entry的返回值,此时的order变成了["a"],被缓存起来
    return order.append(first_entry)
def test_string_only(append_first, order, first_entry):
    # 在测试函数里,order第二次被请求,但是并不会拿到空列表[],而且拿到了被缓存起来的["a"]
    # 所以断言order == [first_entry],其实就是 ["a"] == ["a"],测试通过
    # Assert
    assert order == [first_entry]


从示例中可以看出:


  1. 在fixture函数append_first中,order第一次被请求,返回一个列表[],被缓存起来。
  2. 接着,order.append(first_entry)[]中增加了first_entry的返回值,所以,此时的order变成了["a"]
  3. 最后,在测试函数test_string_only中,order第二次被请求,但是并不会拿到空列表[],而且拿到了被缓存起来的["a"]
    这样的话,最后的断言assert order == [first_entry]就会成功。


反过来,如果同一个fixture在一个测试函数中每次都去请求一次,那上面的测试函数必然失败。


因为,这样一来,虽然在append_first中的返回值仍然是["a"],但是在test_string_only中,


又去重新请求了一次order,拿到的其实是空列表[],所以最后断言会失败。

相关文章
|
3月前
|
存储 设计模式 测试技术
怎么基于Pytest+Requests+Allure实现接口自动化测试?
该文介绍了一个基于Python的自动化测试框架,主要由pytest、requests和allure构成,采用关键字驱动模式。项目结构分为六层:工具层(api_keyword)封装了如get、post的请求;参数层(params)存储公共参数;用例层(case)包含测试用例;数据驱动层(data_driver)处理数据;数据层(data)提供数据;逻辑层(logic)实现用例逻辑。代码示例展示了如何使用allure装饰器增强测试报告,以及如何使用yaml文件进行数据驱动。
105 0
|
缓存 测试技术
31-pytest-内置fixture之cache使用
31-pytest-内置fixture之cache使用
31-pytest-内置fixture之cache使用
|
测试技术 Python
|
测试技术 Python
|
测试技术
pytest conftest.py和fixture的配合使用
pytest conftest.py和fixture的配合使用
|
自然语言处理 Java 测试技术
pytest学习和使用21-测试报告插件allure-pytest如何使用?
pytest学习和使用21-测试报告插件allure-pytest如何使用?
155 0
pytest学习和使用21-测试报告插件allure-pytest如何使用?
|
测试技术
pytest学习和使用9-fixture中conftest.py如何使用?
pytest学习和使用9-fixture中conftest.py如何使用?
125 0
pytest学习和使用9-fixture中conftest.py如何使用?
|
测试技术 API
【pytest官方文档】解读fixtures - 9. 什么样的fixture结构,用起来最可靠?
【pytest官方文档】解读fixtures - 9. 什么样的fixture结构,用起来最可靠?
【pytest官方文档】解读fixtures - 9. 什么样的fixture结构,用起来最可靠?
|
测试技术
pytest(12)-Allure常用特性allure.attach、allure.step、fixture、environment、categories
上一篇文章pytest Allure生成测试报告我们学习了Allure中的一些特性,接下来继续学习其他常用的特性。
pytest(12)-Allure常用特性allure.attach、allure.step、fixture、environment、categories