pytest快速入门 - 构造“后置条件”的几种方式

目录 <<上一章:构造“预置条件”的几种方式 下一章:如何确保后置条件完全执行(测试人员必知必会)>>

1 pytest自动化测试 - 构造“后置条件”的几种方式

  每个测试用例执行完后,都需要恢复环境,将用例添加的数据删除,修改的参数恢复等,在pytest中,有如下方式可以用于构造后置条件,用于执行清理工作。

1.1 使用夹具构造后置条件

  在夹具中使用yield返回测试数据,yield语句后的语句都是后置条件

import pytest

@pytest.fixture
def setup_and_teardown():
    print("\n" + "=" * 65)
    print("预置条件")
    resource = "Some resource"
    yield resource
    print("后置条件")

def test_with_fixture_001(setup_and_teardown):
    print("用例1开始")
    assert setup_and_teardown == "Some resource"
    print("用例1结束")

def test_with_fixture_002():
    print("\n用例2开始")
    assert "Some" in "Some resource"
    print("用例2结束")

输出:

============================= test s
### pytest Framework Setup and Teardown Methods In the context of `pytest`, setup and teardown methods are used to define actions that should be executed before or after tests. These mechanisms ensure that each test runs in an isolated environment, preventing side effects from one test affecting others. #### Using Fixtures for Setup and Teardown `pytest` provides fixtures as a powerful mechanism for managing setup and teardown operations. A fixture is defined using the `@pytest.fixture` decorator, and it can perform initialization (setup) when requested by a test function and cleanup (teardown) once the test completes[^1]. Here is an example demonstrating how to use fixtures: ```python import pytest @pytest.fixture def resource_setup(): print("\nSetup called.") yield "Resource" print("\nTeardown called.") def test_example(resource_setup): print(f"\nTest running with {resource_setup}.") assert True ``` The above code defines a fixture named `resource_setup`. The `yield` statement separates the setup phase (before yielding) from the teardown phase (after yielding). When the test concludes, the teardown logic executes automatically[^2]. #### Class-Level Setup and Teardown For scenarios where you need class-level setup and teardown, you can combine fixtures with scope parameters. By setting the `scope="class"` argument within the `@pytest.fixture` decorator, the same fixture instance will persist across all methods inside a single test class[^3]: ```python import pytest @pytest.fixture(scope="class") def class_resource_setup(request): request.cls.resource = "Class Resource" print("\nClass level setup.") yield print("\nClass level teardown.") @pytest.mark.usefixtures("class_resource_setup") class TestExample: def test_one(self): print(f"\nTest One uses {self.resource}.") assert self.resource == "Class Resource" def test_two(self): print(f"\nTest Two also uses {self.resource}.") assert self.resource == "Class Resource" ``` This ensures both `test_one` and `test_two` share the same setup while executing their respective assertions independently[^4]. #### Module and Session Scopes Fixtures support broader scopes such as module (`scope="module"`) or session (`scope="session"`), which execute only once per module or entire test suite respectively. This reduces redundant setups during large-scale testing sessions but requires careful handling due to shared states between multiple tests[^3]. ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值