So, be honest, when you hear someone talk about unit testing … what is your first thought?
- Is it: “I just don’t have the time to do unit testing”!
- Or “Our management will never approve the time for unit testing”?
- Or something similar?
Let’s look at what unit testing can do for you:
Save you Time
Yes, that’s right!
Throughout my career as a software developer, I have seen how much time unit tests can save.
Let’s think about this …
- Any developer that has written more than a few lines of code knows that it needs to be run to verify that it operates as expected. Right?
- You need to execute the feature to confirm that it takes appropriate inputs and produces appropriate outputs. You may need to go through this process several times.
- And as you build more features, it requires more effort to manually test it.
- For example, say you write a pedometer application that takes in a step count goal and an actual step count and then calculates the percent of goal you have reached.
- To test the pedometer feature you need to execute the application, navigate to the appropriate feature, enter all of the required data, and then validate the results.
- And if you find a bug you may have to repeat this process again, and again, and again.
- For the feature you are working on now, how many times have you run the application to try it out? 10? 20? more?
- The idea of an automated code test is that you can write code to perform that testing.
- So if you want to test that the pedometer calculation is correct, you write an automated code test that defines appropriate inputs, calls the function under test, and verifies the results.
- Then when the users want a change to that code (and you know that they will), you can make the change and just re-run the tests.
- No need to slog through the UI again for each possible set of inputs.
This can save you LOTS of time.
Help you Find Bugs Faster
You just received a bug report. Something in the code does not work.
Instead of trying to reproduce the problem by wading through a bunch of UI, you can instead use your unit tests to quickly reproduce, find, and fix the error.
Allow you to Refactor Safely
So you are working on some code that is just too painful to deal with.
You’d like to apply some refactoring techniques to make the code cleaner and easier to use.
With no tests, you are running a risk of introducing bugs if you rework the code.
If you have unit tests in place, you can safely perform a refactoring because when you are done with the refactoring, you can rerun the tests to confirm that all is well.
Readily Add Features
You can add new features and rerun all of the existing tests to ensure that the new feature does not adversely impact any existing features.
No more worry that adding feature b will adversely affect feature a.
Minimize Those Annoying Interruptions
So you are in the middle of coding the next feature and you have to drop everything because someone entered something bad somewhere in your application and now it is crashing.
Having a good set of unit tests can minimize those annoying interruptions.
Enhance your Value
Don’t you just hate it when the “QA” person emails you to let you know your code doesn’t work?
This is especially difficult if that “QA” person if your boss or your client.
Having a good set of unit tests can make you look like a coding master!
Or what if the person after you changes something and now it looks like your code doesn’t work.
Unit tests help the developers that come after you to better understand and modify your code. They can re-run the tests to ensure the code still works after their changes.
Having a good set of unit tests verifies that your code works over the lifetime of the application.
Writing unit tests isn’t hard or time consuming once you get into the habit. And these benefits look pretty good!
For a gentle introduction to automated code testing, see my Pluralsight course: “Defensive Coding in C#”.
Check out my Pluralsight courses!