Forum Discussion

Kate's avatar
Kate
Contributor
8 years ago
Solved

The best way to add a specific step before each test (Python)

Hi,   What is the best way to add a step, that will be performed before each test automatically?   For ex., I want to check if my app is running and close it if it is. I need it because if my pre...
  • tristaanogre's avatar
    tristaanogre
    8 years ago

    All good suggestions in this thread.  It depends a lot, I guess, on how you have your project structured to execute the tests.  Guys like me, Colin_McCrae, and baxatob have large, complex frameworks that execute our tests and so, built into the framework are these setup/teardown/cleanup procedures that are executed as part of every test. 

     

    If you don't have something that complex, as NisHera mentioned, you could build this kind of thing into each test itself as part of your procedures for creating and deploying new test cases.  

    Your question included the word "automatically" so none of these are actually "automatic" as they require you, the developer, to make sure you write the appropriate stuff as part of your test execution, whether it's in framework or in the tests themselves.  For the purest "automatic", that's what OnStartTest and OnStopTest as Ravik mentioned were created for. Whenever a new test starts, OnStartTest fires and does whatever is defined. When the test stops, OnStopTest fires and does whatever is defined there. This is the "easiest" way to do this kind of automatic thing... but be aware that the events themselves are rather "dumb" and cannot determine what happened in the previous test nor what is supposed to happen in the next test. So, you'll need to be "smart" and build your event handlers to reset your environment in such a way that it will work for any of your tests.