4

I am using Selenium GUI tests in a Java Web Application. Since these tests are actually client, how can we rollback database after running a test?

jd466
  • 547
  • 1
  • 6
  • 20

4 Answers4

3

What you're after is called Fixture Teardown Patterns. Since you need to keep track of all resources that are created in a test and automatically destroy/free them during Teardown. I obviously don't know your framework, but for this case

rollback database after running a test

a good candidate is the Inline Teardown. It includes teardown logic at the end of the Test Method immediately after the result verification. Like so:

enter image description here

ekostadinov
  • 6,880
  • 3
  • 29
  • 47
  • Thanks, what is these test_methods depend on each other? For example in case of an Integration Test. – jd466 Nov 03 '15 at 13:06
  • In this case the `test_method`s are the steps in single test, but you can modify it to your needs and consider them as individual tests in one suite. So the concept is applicable to you. Like I said - think of them as separate tests. – ekostadinov Nov 03 '15 at 13:19
2

My guess is that you can't 'roll back' the database since web applications typically commit transactions between requests.

You'll need to implement your own custom rollback. Perhaps you could create a new user for each test and remove any changes made by this user after the test. Or maybe you want to implement the command pattern.

You might also find a cascading delete helpful

lance-java
  • 25,497
  • 4
  • 59
  • 101
2

Lately I attended a talk about docker. The speaker was creating a docker container with a mysql database for demonstration purposes. I was immediately thinking about how to use this for integration testing as you can create a clean database instance with very little effort.

I was searching if there are already some best practices and found those to websites

I´m in the phase of evaluating on how to integrate this but I´m confident this is what I (and hopefully you) was looking for. Workflow would be:

  1. Test execution
  2. Start docker container from image with empty
  3. Fill database with master data (if necessary)
  4. Run test
  5. Throw docker container away
Kai
  • 38,985
  • 14
  • 88
  • 103
0

Thank you for your suggestions. I decided to use mysqldump for this purpose. Within Ant, Backup and restore the test-database before and after each batch-test.

jd466
  • 547
  • 1
  • 6
  • 20