Why automated tests?

31 Mar 2011

  1. Work is more fun when the things you create mostly works.
  2. When fixing bugs, the probability of introducing new bugs is reduced.
  3. Specifications of program behavior expressed as tests are easier to maintain than tutorial code or a word document specification.
  4. In order to be able to write unit tests easier you are forced to simplify your classes.
  5. Switching underlying technology or infrastructure. For example: If you have integration tests on your database repositories or data access layers it is easier to switch the underlying implementation. You have a way of verifying that important behavior is intact.
  6. If you are writing code that:
    • Requires the use of the debugger frequently.
    • After you have written it, you fear to change.

    Do you really consider yourself a professional developer?

Some of it is in relation to having to work late at night and during weekends. Tests can help you with your definition of done, thus alleviating the need for late night coding sessions.

Effective coding means to use the tools available when they are appropriate.

Sometimes it’s about doing something simple:

  1. Is the bug to complex to find using tests? The flow of the application is wrong…

If you’re a beginner. How do you start writing tests?

  1. If you’re writing C# then you want to read “the art of Unit Testing” by Roy Osherove.
  2. If you’re unsure about how to write tests. Try using test lint.
  3. Search posts done by Martin Fowler, Kent BeckRobert C. Martin (Uncle Bob)Michael Feathers .
  4. How do you do when you test your code manually?

    1. You set up the state of the program relevant before the method/procedure to test. In NUnit it’s called SetUp. You use this attribute to mark the method that sets up the state.
    2. You fill in a form and click on a button, browse to a page with the right parameters in the url, et.c. When testing this is the actual test. You mark a method that does this by the Test attribute.
    3. You look at the result from the action. In NUnit you use the static helper class Assert that contains methods to help you write the expected values.
    4. After you’re finished testing, you may need to clean up. In NUnit this is accomplished by marking the a method with the attribute CleanUp.

If you are structured in your testing of your app you may know some of the domain of automatic testing. You can read about this in the above book by Roy Osherove.

Also, writing tests is not a reason not to test what you’ve written manually. It is a way to make sure that you don’t have to do as much manually testing as before.



Do you want to send a comment or give me a hint about any issues with a blog post: Open up an issue on GitHub.

Do you want to fix an error or add a comment published on the blog? You can do a fork of this post and do a pull request on github.