Ask a Question

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


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



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 previous test fails, my app stays open... But I start all my tests from running the app, so I'll get an Error and a failure if the app is already running.


I create script tests in Python.



Valued Contributor

Your questing seems bit broader..


Usually in test automation what you do is 

1) start with known condition

2) run the test

3) Clean up 


So it seems you have missed last step in previous test.

So my point is better to clean-up in previous test.




Super Contributor

You can try -  GeneralEvents_OnStartTest(sender) event in TC.


In OnStartTest you can define your steps what you want to perform before your test start -


like - Close app, Kill Existing browsers sessions, excel and all .







Community Hero

I use my own script start_application(*args) which run all required procedures:
- check for configuration files and get all necessary data from there;
- kill all redundant processes in the system;
- check all required services;
- run the application etc.


If something goes wrong on this step, test will not be started.

Yep. I use similar startup and teardown scripts before all my tests.


Usually to kill/remove/clean everything before it starts. Actually starting the application (by running the executable in my case) is usually a test in itself, so is separate.


So, much like the others ....


Kill all executables.

Kill all services & processes.

Restore the DB.

Etc etc ....

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.

Robert Martin
[Hall of Fame]
Please consider giving a Kudo if I write good stuff

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Vegas Thrill Rider
Extensions available
Showing results for 
Search instead for 
Did you mean: