Friday February 22, 2019
Experience and writing good unit test code do not have any relation whatsoever, Writing good test is strictly based on the intention behind the test.
Unit test is basically done to test how various pieces of a system is working together and how it is integrating with architecture. So the most integral part of unit test to remember why you are performing the test.
In order to help you with the process, Let’s have a look at unit test in detail and what are best practices that have to be followed for the success of it.
What is Unit Testing?
A Unit testing basically covers every small functionality of your software. It verifies the behaviour of one component of your software which is independent from other parts. In Unit Testing there are basically three parts :
Best Practices to be followed while writing Unit Tests:
While you are writing unit tests, it is very important that you write your tests in such a way which should follow best practices. Let’s look at them in detail.
The test cases should be independent of each other. You can arrange the test cases in a way defined by you. You can define the cluster – short or long running test cases. Every test should be orthogonal in such a way that it should be independent of other test cases.
If that would not be the case then any change in behaviour of one test case would affect multiple of tests. You can achieve this by following one simple rule “Don’t try to add unnecessary assertions.”
You should have assertions which match the specific behavior of an application. They should be isolated and can be run in isolation and should not have dependencies on other outside factors.
The scenario checks the addition of zero to a number. Multiplication functionality assertions should not be covered in this test step.
Good Unit Testing Practice would be:
The unit tests are designed by developers so that they can repeatedly run them so as to ensure that no bugs are introduced in the application.
If these tests would be slow then that would add to time of execution of the test cases. One slow test would make a difference in the execution of overall suite of unit test cases.
So, developers have to make use of best coding practices so that the time of execution can be decreased exponentially.
The unit test is a bad test code. The execution will be slow as we are not using stream concept which increases the execution speed of the code exponentially.
Usage of stream in unit test code is a very good practice. It increases the speed of execution.
A unit test should be readable and clear. You should get to know the story of the functionality which that unit test is going to test.
It should properly show that “the scenario it is testing” and if the test is going to fail, then you should clear idea that why is it failing.
You should be able to address the problem in no time. The test cases should not be too complex and they should be well structured so as to increase the readability of the tests.
You should have a logical name for every variable and test case. This name should show the operation and functionality you are testing.
Every test and variable can’t have a fancy name or an irrelevant name. You can’t have a name like “Show Logical Exception” for a variable or a test. This name don’t have a meaning attached to it.
You should treat your tests with the same care as you treat your production tests. You should incorporate good designing techniques in your framework which is used for testing.
For example there should be low coupling between test code and production one. You should remove dead code to increase the test efficiency.
You should manage the memory well along with time efficiency. A good code base will make your re-factoring and maintenance very much easier in the later stage.
While, if you will have a bad suite then that would make your life more worsen when you find your code not adaptable to change.
Unit tests should fail when there is some bug in the system. They should not fail unless there is no bug.
But sometimes, Developers encounter such situations where the tests fail even when there is no bug.
Sometimes when you run a single test then it passed while if you run it in unit test suite then that test case fail. Sometimes, tests are migrated to continuous integration server, the tests starts failing.
This suggest that there is some design flaw in the system. Good unit tests should be independent from external factors which can be environment or machine on which the tests are running.
In this way, if you will follow good unit coding practices, you won’t get any problem in maintainance of code in the later stage of life.
Life would be much more easier for you. Just follow the practices and you will really feel the difference in time of execution. All the best !!