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 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.

 

Thanks

  • 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.

5 Replies

  • Ravik's avatar
    Ravik
    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 .

     

     

     

     

     

     

    • baxatob's avatar
      baxatob
      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.

      • Colin_McCrae's avatar
        Colin_McCrae
        Community Hero

        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 ....

  • NisHera's avatar
    NisHera
    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.