Pytest如何忽略自动使用测试夹具中的设置类



以下代码在包含setup类的每个函数之后运行。我不会在实际测试之前创建实例,所以我不希望它在setup_class方法之后运行。你能建议我是否可以在设置类后将签名更改为不运行吗

@pytest.fixture(autouse=True)
def teardown_module(self):
Log.test_teardown("Deleting instance")
Utils.compute_utils().delete_instance_and_wait_for_state(
TestAutoValidateCpuAlignment.instance_id, teardown=True)

使用yield可以更有效地实现这一点。将teardown_modulecreate_module固定装置合并为一个在操作之间同时执行yield的固定装置。通过这种方式,它将创建您的实例,执行测试,然后将其拆下。

@pytest.fixture(autouse=True)
def instance_module(self):
try:
Log.test_teardown("Creating instance")
Utils.compute_utils().create_instance_and_wait_for_state(
TestAutoValidateCpuAlignment.instance_id, teardown=True)

yield
except Exception as e:
# do something with the exception
finally:
Log.test_teardown("Deleting instance")
Utils.compute_utils().delete_instance_and_wait_for_state(
TestAutoValidateCpuAlignment.instance_id, teardown=True)

最新更新