Python 6.pytest 强大的 fixture (上)

Maple · 2020年12月27日 · 1097 次阅读

pytest 强大的 fixture(上)

测试 fixture 的目的是提供一个测试的基线,在此基线基础上,可以更可靠的进行重复测试。Pytest 的 fixture 相对于传统的 xUnit 的 setup/teardown 函数做了显著的改进:

  • 测试 fixture 有明确的名称,通过在函数/模块/类或者整个项目中激活来使用 。

  • 测试 fixture 是模块化的实现,使用 fixture 名即可触发特定的 fixture,fixture 可以在其他 fixture 中 进行使用 。

  • 测试 fixture 不仅可以进行简单的单元测试,也可以进行复杂的功能测试。可以根据配置和组件的 选项进行参数化定制测试,或者跨函数/类/模块或者整个测试过程进行测试。

作为函数入参的 fixture

测试函数可以通过接受一个已经命名的 fixture 对象来使用他们。对于每个参数名,如果 fixture 已经声明定义,会自动创建一个实例并传入该测试函数。fixture 函数通过装饰器标志@pytest.fixture来注册。下面是一个简单的独立的测试模块,包含一个 fixture 及使用它的测试函数

# test_fixture.py
import pytest


@pytest.fixture
def smtp_connection():
    import smtplib
    return smtplib.SMTP("smtp.qq.com", 587, timeout=5)

def test_ehlo(smtp_connection):
    response, msg = smtp_connection.ehlo()
    assert response == 250
    assert 0 # 用于调试

这里,test_ehlo 需要 smtp_connection 这个 fixture 的返回。pytest 会在@pytest.fixture的 fixture 中查找并调用名为 smtp_connection 的 fixture。运行这个测试结果如下:

(pytest) D:\study\auto-pytest>pytest test_fixture.py
=============================== test session starts ===============================
platform win32 -- Python 3.7.1, pytest-6.0.2, py-1.9.0, pluggy-0.13.1
rootdir: D:\study\auto-pytest
collected 1 item  
test_fixture.py F              [100%]

=============================== FAILURES ===============================
_______________________________ test_ehlo _______________________________

smtp_connection = <smtplib.SMTP object at 0x0000014FA26653C8>

    def test_ehlo(smtp_connection):
        response, msg = smtp_connection.ehlo()
        assert response == 250
>       assert 0 # 用于调试
E    assert 0

test_fixture.py:11: AssertionError
=============================== short test summary info ===============================
FAILED test_fixture.py::test_ehlo - assert 0
=============================== 1 failed in 0.31s ===============================

测试的回显中可以看出测试函数调用了 smtp_connection,这是由 fixture 函数创建的 smtplib.SMTP() 的一个实例。该函数在我们故意添加的 assert 0 处失败。以下是 pytest 的在调用该函数的时候的详细规则:

  • pytest 找到以 test_作为前缀的测试用例 test_ehlo。该测试函数有一个名为 smtp_connection 的 入参。而在 fixture 函数中存在一个名为 smtp_connection 的 fixture。
  • smtp_connection() 被调用来创建一个实例。
  • test_ehlo() 被调用并在最后一行因为断言失败。注意,如果拼错了函数参数,或者使用了一个不可用的参数,你会看到一个包含可用函数参数列表的错误信息

注意:你可以使用 pytest --fixtures test_fixture.py 来查看可用的 fixture(如果想查看以_开头的 fixture,请添加-v 参数)

conftest.py:共享 fixture 函数

实现测试用例的过程中,当你发现需要使用来自多个文件的 fixture 函数的时候,可以将这些 fixture 函数放到 conftest.py 中。

你不需要导入这些 fixture 函数,它会由 pytest 自动检索。

fixture 函数的检索顺序是从测试类开始,然后测试的模块,然后就是 conftest.py 文件,最后是内置的插件和第三方插件。

你还可以使用 conftest.py 来为本地每个目录实现插件 。

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
暂无回复。
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册