Question

I have a test database with few user accounts. I don't want to break any tests. If I am writing a function that updates password for user in db and one that reads the password.

Should I ideally reset the password to the previous one, once I am done testing update function ?

I have never written unit tests before, so I am little confused.

Was it helpful?

Solution

An important rule when doing repeatable tests (automated or not) is to make sure that the test data is always in the same state before the tests start. This should be ideally assured for every single test of your test suite, so you can interchange the order of any tests at any time.

How you achieve this is up to you. For your scenario, it may make sense to reset the password to the original state after the test is finished (note that you have to do this indepently of the outcome of the test). A different approach is to have a complete db backup in place which is restored after each test (something which be feasible for small single-file databases like SQL lite or MS Access, but typically not for big C/S dbs like Oracle or MS SQL server). A third alternative is to have a script which initializes all relevant test data in your DB (including passwords) to a defined state, and to run this script automatically before any of your tests is executed.

Which of these alternatives is best depends on the kind of database you are using, the kind of tests you have, the dependencies among them, the total amount of test data, and some performance and safety considerations.

Licensed under: CC-BY-SA with attribution
scroll top