What is unit testing?
Unit testing is the act of writing code (as simplified as possible using clear English to describe it) to test the logic of the software your business uses. The main goal of testing is to simulate real-world conditions of your application and compare to a known expected output.
A simple test would be:
Why we Test
Now we have some code, let’s do some tests to prove the code is working. The tests allow us to keep modifying the code for any improvements or any extra functionality while knowing the business rules are tested and enforced.
The observant eye will notice that the programmer made a mistake in the original code! They paid the charity an amount of 20% instead of 2%. Having known values of what the expected output should be the programmer running the tests will see the values are coming out wrong and fix “charity_amount = amount * 0.02”. They can stop the bugs before they are sent to the you, the client.
Importance of Tests
We saw above, why we test. Now let’s look at why it’s important. We now have documented code that can be repeatedly run each time your program is updated to make sure your business rules are continually enforced. But it doesn’t end there:
Your business is growing, you may have started with a very small application and think these tests could just be done by someone clicking through the program and testing results. Or better yet, the programmer shouldn’t make mistakes But now your business has grown and you need more and more functionality.
Your application is getting really complicated; your business is thriving after all, the systems to maintain it are complex. You make requests for functionality, but ‘Phil’ the original programmer has been working on other projects in between (or he has had a few sleeps and few beers between first implementing the charge method). Anything ‘Phil’ does now is supported by these tests so by making a change somewhere else in your codebase, he doesn’t break these core business rules.
The next scenario is your business is booming; this is great news! But ‘Phil’ can’t keep up, or ‘Phil’ has left. Now ‘Angela’ has been put onto your project to assist in creating all this new functionality. ‘Angela’ can’t know all your business rules straight away, you need this feature! ‘Angela’ understands the business rules for your specific requirements today, but the tests enforce the changes she makes to your system here don’t break existing business rules.
The Benefits of Testing Code
- Quick changes to complex pieces of code with assurance that the expected output, or any other methods reliance on this code isn’t broken.
- Refactor code to make performance improvements and readability without breaking business rules.
- Allow other web developers to truly understand the expected results of a method when they are changing it (or changing something else that this code relied upon).
- Written in a clear concise languages so business analysts who know your business rules can clearly read them and understand the programmer has produced the correct requirements.
- Perfect way to reproduce hidden bugs. They happen, and need to be fixed. Writing a test to reproduce the bug then ensures it is fixed and never happens again.
- Proven way to find bugs, generally the cascading effect of other methods (creating the faulty method) creates a beacon of light onto the real culprit.
- Other developers (or the new coder 6 months from now) can understand and quickly make other changes without breaking your business logic or traversing through endless notes making sure every bit of your business logic is still in tact.
- Minimise risk of changes to enforcing business rules.
As you can see even through a very abstract example of a simple use-case, testing code is really important. It’s rare that your tasks are this simple, and are generally a lot more complex. But it’s important to remember that everyone makes mistakes, even programmers.
Tests insure your code, so from now on it will always be correct regardless of other changes in the system or the developer helping build it.