Automated Unit Tests: the Pros and Cons
There are two sides to the coin when it comes to automated unit tests. I’ve been using the testing on a few projects recently, so I thought it would be helpful to recap some of the pros and cons here.
The Pros
There are so many reasons why it’s a good idea to use automated unit tests:
- They eliminate human error: When you just manually test the site, you have to remember to run each test and record the results. An automated unit test runs – as the name implies – automatically, and the results are recorded automatically. And the benefit is that it’s run the exact same way every time.
- You gain an ever-growing suite of test cases: If you test and find a new a problem, you just add a new test case and fix the code so that the test passes. Now you’ve increased your test case suite by one. The idea is it that it continually grows with you throughout the testing phase of the project.
- They encourage good coding practices: It’s not designed to test the user interface, so it encourages the decoupling of your business logic from your UI.
- More test cases means more confidence in stability of code: As your unit test suite grows, you will know that with each passing test, your code is increasingly stable. The more tests in your suite, the more confidence you can have in your code and any changes in your code.
- They allow you to refactor source code with more confidence. Refactoring code is something programmers do from time to time. Basically, as we develop the code, some parts at times get a little big and bloated. Refactoring is going back over and rewriting sections of your code to make it more streamlined. One good practice is to make sure that you don’t try to have a method do more than one thing, or have an object do more than one thing. When we go through and refactor big blocks of code, we can re-run our unit tests and know that even though we rewrote some of the code, the output is still the same.
- They make the adding of features easier: Automated unit testing allows us to thoroughly test everything and make sure when we add features, we do not negatively impact the already-verified code.
the cons
- Increased overhead of amount of code to write: Every method in your classes should be tested by your unit tests. Most of the time, you’ll have one or more unit tests to write, depending on complexity. It’s really simple code, but it’s still additional code you have to write.
- You can’t write unit tests for your user interface: These tests are great for testing business logic implementation – not so great for testing your UI. How are you going to write a C# method that tells you that a screen looks good? There are some 3rd party suites that will record key strokes and mouse clicks and execute tests that way, but these are usually pretty expensive.
If you’re looking for a good resource and up for a bit of reading on the topic, try The Pragmatic Programmer: From Journeyman to Master by David Thomas and Andrew Hunt.