-1

The below code runs after every function including the setup class. I dont create an instance before the actual tests so i don't want it to run after the setup_class method. Can you advise if i can change the signature to not run after 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)
Ray
  • 615
  • 7
  • 22

1 Answers1

0

This can be done more efficiently using yield. Combine the teardown_module and the create_module fixtures into one that does both and yield between the operations. This way it will create your instance, execute the tests and then tear it down.

@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)
Ilya
  • 730
  • 4
  • 16
  • The tests do more than just create the instance. They also make modifications etc. If the modifications fail, will it still call the delete? It looks like the answer is no as the yield has to have been reached – Ray Nov 23 '22 at 12:12
  • If there is more constrains to the question please update them in. Regardless, I updated the code to execute the teardown even if the create is failed – Ilya Nov 23 '22 at 12:35