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.

1 Answer 1


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.

  • I would also emphasize that you can't always count on your cleanup code running. If your tests crash hard and take down the test runner process, your reset function might never get called. Jan 26, 2014 at 14:17
  • @akatakritos: indeed, that's why one should always initialize the complete test data in a reliable way before running the test suite, and undo the effects of a single test right after its done.
    – Doc Brown
    Jan 26, 2014 at 14:24

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.