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