There are number of different unit testing methodologies - such as test driven development and behaviour driven development. These methodologies are applied to software systems in order to carry out unit testing. Unit testing is one of the many software testing methodologies available and it has been praised for the number of defects that can be identified when it is used correctly.
We are going to take a look at what unit testing is and why it is worth doing.
What is Unit Testing?
Unit testing is a way of testing a software that developers create. It can be achieved manually but it is mainly automated.
Unit testing generally tests certain functions or procedures within code and gives the ability to verify that it works as expected. For any function and given a set of inputs, we can determine if the function is returning the correct values and will gracefully handle failures during the execution process if invalid input is provided.
Unit testing helps us to identify failures in our business logic and improves quality of what the developer writes for that particular piece of code.
Why Unit test?
Unit tests help us to identify failures in our business logic to help improve the quality of the code that is written for certain functions. It also ensures that code is behaving the way it should by isolating the smallest piece of testable software in the application from the remainder of the code.
As you write more and more tests, you end up creating a suite of tests that you can run at any time during development to continually verify the quality of your work.
Development from a unit testing perspective is writing code that is easy to test. Unit testing requires that your code be easily testable and it must support this particular type of evaluation. You are more likely to have a higher number of smaller and more focused functions that provide a single operation on a set of data rather than large functions performing a number of different operations.
Writing unit tests that are well-tested can prevent future changes from breaking functionality. Since you're testing your code as you introduce new functionality, you're going to begin developing a suite of test cases that can run each time you work on your business logic. When a failure happens, you know that you have something to address.
Benefit of unit testing includes finding software bugs early, facilitating change, simplifying integration, providing a source of documentation, and many others, which we will look at now.
Benefits of Unit Testing
If unit testing is written and performed properly and consistently, software projects are a lot more effective at delivering the correct solution in a predictable and managed way.
One of the main benefits of unit testing is that it makes the coding process more Agile. When you add more and more features to a software, you sometimes need to change old design and code. However, changing already tested code is both risky and costly. If we have unit tests in place, then we can proceed with refactoring confidently.
Some of the unit testing benefits are:
- Improves the quality of the code.
- Identifies every defect that may have come up before code is sent further for integration testing.
- Helps you to realise when to stop coding. Your tests give you confidence that you've done enough for now and can stop tweaking and move on to the next task.
- Helps with coding when faced with a large piece of work, writing the tests will get you moving quickly.
- Helps you to really understand the design of the code you are working on. Instead of writing code to do something, you are starting by outlining all the conditions you are subjecting the code to and what outputs you would expect from that.
- Reduces defects in the newly developed features.
- Reduces bugs when changing the existing functionality.
- Allows the programmer to refactor code at a later date and make sure the module still works correctly.
- Testing is carried out by developers who test code before integration, issues can be found very early and can be resolved there and then without impacting the other pieces of the code.
- Provides documentation of the system by looking at the unit tests, the developer can gain a basic understanding of the unit’s interface.
- Simplifies the debugging process. If a test fails, then only the latest changes made in the code need to be debugged.
- Unit testing helps reduce the cost of bug fixes.
- Writing the test first (before actual code) forces you to think harder about your problem and it exposes the edge cases and keeps you focused to write better code with better design.
- Forces you to define what code is responsible for. If you can do this easily, that means the code’s responsibility is well-defined and therefore that it has high cohesion.
The IBM System Sciences Institute have done an in-depth study on bug fixing and their relative costs. They conclude that fixing a production bug costs 100x more than fixing a bug at design time, and over 15x more costly than fixing a bug during integration testing.
Unit tests and test-driven development (TDD) have so many hidden and personal benefits as well as the obvious ones that you just can't really explain to somebody until you are writing tests yourself.
Drawbacks of unit testing?
As with anything, there are some drawbacks, but these are outweighed by the benefits we get. Here are some of the main points:
- Unit testing cannot catch each and every bug in an application.
- Unit testing is hard to set up for realistic, useful tests.
- Unit testing is time consuming. You will spend little more time. Sometimes writing testable code might be frustrating, but you get used to it and get better at it as you practice.
- It is impossible to evaluate every execution path in every software application. The same is the case with unit testing.
- Integration errors or system errors can be missed.
- Test program cannot be run in actual deployment environment.
- Test code is likely to be at least as buggy as the code it is testing.
- Value and accuracy of unit tests can be diminished if initial conditions are not set correctly.
- Software testing is important and it should be embedded into software development processes.
- Unit testing offers many benefits with limited drawbacks (that are outweighed by the benefits).
- Every testing tool is valuable and it can reveal its benefits only when it is used correctly.
So, you must start looking into unit testing your code and you will start to reap the fruits of your efforts.