以下代码在包含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_module
和create_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)