• Pytest教程:Fixture详解


    什么是 Fixture?

    Fixture 是 Pytest 中用于提供测试环境的一种机制。它可以被用来模拟资源,例如数据库连接、临时文件、网络连接等,以及执行一系列的设置和清理操作,从而使测试用例能够在可控的环境下运行。

    Fixture 的基本用法

    在 Pytest 中,我们可以通过 @pytest.fixture 装饰器定义 fixture。fixture 可以在测试函数中作为参数传递,并在需要时自动执行。以下是一个简单的例子:

    1. import pytest
    2. @pytest.fixture
    3. def setup():
    4. print("Performing setup")
    5. yield
    6. print("Performing cleanup")
    7. def test_example(setup):
    8. print("Executing test")
    9. assert 1 + 1 == 2

    在上面的例子中,setup 函数是一个 fixture。在 test_example 测试用例执行前,setup 函数会被调用。当测试用例执行完成后,setup 函数会执行清理操作。

    Fixture 的作用域

    Fixture 可以具有不同的作用域,以控制其生命周期和共享程度。Pytest 支持四种作用域:functionclassmodulesession

    • function:默认的作用域,每个测试函数执行前后调用一次 fixture。
    • class:在测试类中所有方法执行前后调用一次 fixture。
    • module:在每个模块(.py 文件)中的所有测试函数执行前后调用一次 fixture。
    • session:在整个测试会话中只调用一次 fixture。
    1. import pytest
    2. @pytest.fixture(scope="module")
    3. def setup_module():
    4. print("Module setup")
    5. yield
    6. print("Module cleanup")
    7. def test_example1(setup_module):
    8. print("Executing test 1")
    9. assert True
    10. def test_example2(setup_module):
    11. print("Executing test 2")
    12. assert False

    在上面的例子中,setup_module fixture 的作用域被设置为 module,因此在整个模块中的测试函数执行前后只会调用一次。

    参数化 Fixture

    有时候,我们希望 fixture 能够根据不同的参数化条件提供不同的行为。Pytest 允许 fixture 接受参数,并在测试函数中使用 pytest.mark.parametrize 进行参数化。

    1. import pytest
    2. @pytest.fixture
    3. def setup(request):
    4. value = request.param
    5. print(f"Setup with parameter: {value}")
    6. yield value * 2
    7. print("Cleanup")
    8. @pytest.mark.parametrize("setup", [1, 2], indirect=True)
    9. def test_example(setup):
    10. assert setup == 2 or setup == 4

    在上面的例子中,setup fixture 被参数化为 1 和 2,并在测试函数中使用间接参数化 (indirect=True) 进行引用。这样,测试函数会分别以 2 和 4 作为 setup 的值运行。

    Fixture 的 Fixture

    有时候,我们可能需要在 fixture 中使用其他 fixture。Pytest 允许在 fixture 中注入其他 fixture。

    1. import pytest
    2. @pytest.fixture
    3. def setup_data():
    4. return [1, 2, 3]
    5. @pytest.fixture
    6. def setup_complex(setup_data):
    7. return {"data": setup_data, "count": len(setup_data)}
    8. def test_example(setup_complex):
    9. assert setup_complex["count"] == 3

    在上面的例子中,setup_complex fixture 依赖于 setup_data fixture。Pytest 会自动解析依赖关系,确保在调用 setup_complex 之前,setup_data 先被调用。

    Fixture 的延迟加载

    有时候,我们可能需要在测试函数中动态地请求 fixture。Pytest 允许在测试函数参数中使用字符串来引用 fixture,从而实现延迟加载。

    1. import pytest
    2. @pytest.fixture
    3. def setup():
    4. print("Setup")
    5. yield 42
    6. print("Cleanup")
    7. def test_example(setup):
    8. assert setup == 42
    9. def test_example_with_fixture_string(setup):
    10. assert setup == 42

    在上面的例子中,test_example_with_fixture_string 测试函数直接使用了 setup 字符串来引用 setup fixture。Pytest 会自动解析这种引用并加载相应的 fixture。

    Fixture 的 Fixture Finalizer

    有时候,我们可能需要在 fixture 的生命周期结束时执行一些额外的清理操作。Pytest 允许使用 request.addfinalizer() 来注册这样的清理函数。

    1. import pytest
    2. @pytest.fixture
    3. def setup(request):
    4. print("Setup")
    5. def teardown():
    6. print("Teardown")
    7. request.addfinalizer(teardown)
    8. yield 42
    9. def test_example(setup):
    10. assert setup == 42

    Fixture实战

    1. # content of conftest.py
    2. import pytest
    3. from myapp import MyApp # 假设有一个名为 myapp 的应用,需要进行测试
    4. @pytest.fixture
    5. def app():
    6. # 在这里创建并返回应用的实例
    7. return MyApp()
    8. @pytest.fixture
    9. def logged_in_user(app):
    10. # 模拟已登录的用户,并返回用户对象
    11. user = app.login("testuser", "password")
    12. return user

    上述示例中,我们创建了两个 Fixture:applogged_in_user

    • app Fixture 创建了一个 MyApp 的实例,它代表我们的应用。在测试中,可以通过传递 app 参数来获取应用实例,以便进行测试。

    • logged_in_user Fixture 则利用了 app Fixture,模拟了一个已登录的用户,并返回用户对象。这个 Fixture 可以在需要已登录用户的测试中使用。

    接下来,我们可以编写测试来使用这些 Fixture:

    1. # content of test_myapp.py
    2. def test_app_creation(app):
    3. assert app is not None
    4. assert app.is_running() # 假设 MyApp 类有一个检查应用是否正在运行的方法
    5. def test_user_login(logged_in_user):
    6. assert logged_in_user is not None
    7. assert logged_in_user.is_logged_in() # 假设用户对象有一个检查是否已登录的方法

    在这个示例中,我们编写了两个测试函数,它们使用了上面定义的 Fixture。通过传递 applogged_in_user 参数,测试函数可以访问预先设置好的资源,使得测试更加简洁和可维护。

  • 相关阅读:
    SN65HVD3082、SN65HVD3088、SN75HVD3082、SN65HVD3085低功耗RS-485收发器
    Kotlin - 协程调度器 CoroutineDispatcher
    ArmV8架构
    反序列化漏洞(1), 原理, 实验, 魔术方法
    鼠标移入与离开事件及页面开关
    SpringCloud-NacosFoundation
    [RootersCTF2019]I_<3_Flask-1|SSTI注入
    设计模式:工厂模式和抽象工厂模式的区别
    MayApps平台为政府机关后勤管理添“智慧”
    Linux信号
  • 原文地址:https://blog.csdn.net/weixin_40025666/article/details/136297760