I have two big concerns about UI automation.

  1. If I am creating a data record as part of my test and verifying the creation, should I also delete the data in the same test? If yes what if the creation passes but deletion of data fails due to some reason? If deletion fails then I will end up having lots of test data.

  2. If I have to verify the state of some UI element as a result of some operation, should I reset the precondition in the test itself? The steps are:

    1. perform some a action
    2. verify the state of an element .
    3. undo the action so that next time when the same test runs it is in the correct condition.

My concern is what if resetting the action fails. Should resetting the action also be part of the test? If yes, when the resetting fails it fails the entire test, but my actual test of setting and verifying the state has passed.

How should my tests handle situations where I create or change data and undoing my actions is the failure point?

  • Edited to try to make clearer and highlight the main concern
    – Kate Paulk
    Commented Sep 26, 2019 at 19:57

2 Answers 2


It depends

Now for the longer answer.

If you have full control over your test database, you can have your tests configured in a way that allows them to always start from the same known point. One way to do this is to restore your test database at the start of each test run. Another is to use containers.

If restoring the test database isn't possible, you are better off having designated test setup and test cleanup routines. Exactly how you configure them will depend on your testing framework, but for MSTest it would look a bit like this:

public void RunClassInitialization()
    // do whatever you need to have everything in the correct state to start your test run, for this class 

public void RunSetup()
    // do whatever configuration is common for each test in this class.

// a series of test methods follow - these should all be for a single part of the UI,
// like one page that you're working with.

public void RunCleanup()
    // do whatever cleanup is common to all your tests

public void RunClassCleanup() 
    // do whatever cleanup you need to after running all the tests in this class

The exact annotation style and scope options will depend on what framework and language you use. I may not have correctly remembered the C# and MSTest annotations, but you get the idea.

If before every test you go to the login page and log in, that goes in the before test/test setup method. Your cleanup goes in the cleanup method.

Another option is to use a try-finally block:

    //do your actions and assertions here
    //do your cleanup here

It all comes down to what works best for your situation.

  • Thanks a ton Kate! It helps . Need to put a thought process on this . Commented Sep 27, 2019 at 7:02

Have nightly DB refresh to reset data into base state.

The way we solved it in our project , is by having nightly DB refresh from pre- configured DB copy for automation (one time job).

This way every test starts and ends on home page, just performing test steps , no pre/or post cleanup steps which is already seeded/set in DB.

  • COULD YOU PLEASE EXPLAIN IN DETAILS .. Commented Sep 30, 2019 at 5:20
  • What you did not understand? Commented Sep 30, 2019 at 11:23
  • Just setup the database with right data and keep refreshing it back on nightly basis so tests will always find the data in right state on execution. Commented Sep 30, 2019 at 22:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.