This blog is aimed at those developer who have little development and unit testing experience. Please refrain yourself to proceed if you don't know development.
What is Unit Testing ?
By Definition, In computer programming, unit testing is a method by which individual units of source code are tested to determine if they are fit for use. A unit is the smallest testable part of an application. In procedural programming a unit may be an individual function or procedure. Unit tests are created by programmers or occasionally by white box testers.
Some Benefits of Unit Testing:
Bug Tracking
Code Maintenance
Understandable code
How the above things help in actual scenario ?
Suppose you have developed a code from scratch and it went to maintenance. Now you left the company, there are no unit test written and a bug came via customer. The app went for a hot fix and some other guy fix it, and his fix created another which in turn creates another issue.
So the creation of issue cycle is continuous as nobody knows the complete actual scenarios and the actual (original) dev has left the company and the company went haywire when an issue came.
Now whose fault is this ?
Let me tell you, its original dev fault, as he didn't write the unit test cases.
If we would have written the unit test cases, then:
A wrong scenario would have failed the test case, and we can fix the code.
Code will become more understandable
Code in turn is maintainable.
Our Learning from our case study
Unit testing provides a sort of living documentation of the system. Developers looking to learn what functionality is provided by a unit and how to use it can look at the unit tests to gain a basic understanding of the unit's API.
Unit test cases embody characteristics that are critical to the success of the unit. These characteristics can indicate appropriate/inappropriate use of a unit as well as negative behaviors that are to be trapped by the unit. A unit test case, in and of itself, documents these critical characteristics, although many software development environments do not rely solely upon code to document the product in development.
By contrast, ordinary narrative documentation is more susceptible to drifting from the implementation of the program and will thus become outdated (e.g., design changes, feature creep, relaxed practices in keeping documents up-to-date).
So When a Test Fails Incorrectly please read above two paras or below two points.
Delete the failing test after verifying that it is no longer valid—essentially since the old requirement is either invalid or is tested elsewhere.
Change the old test so you test the new requirement (essentially using a new test), and test the old requirement under new settings (the test logic stays the same, but the initialization function may change).
Correct way to write Unit Tests (What is the biggest mistake developers generally do?)
In one line - Developers do Integration testing instead of unit testing.
Unit testing means we should not test external dependencies.
Simple Example is following.
Suppose we have following pseudo code:
Function loadData(Success, Fail){
if(Success){
helper.callThisWhenItsSuccess();
}
if(Fail){
helper.callThisWhenItsFail();
}
}
In above function if write a unit test case,
We should not worry about helper class and its function, the helper class should not be covered in this particular unit test If it is covered then it become type of integration testing.
How to avoid this in real time scenarios ?
Use mocks, Stubs and use correct assertions.
So ideally, each test case is independent from the others: substitutes like method stubs, mock objects, fakes and test harnesses can be used to assist testing a module in isolation. Unit tests are typically written and run by software developers to ensure that code meets its design and behaves as intended. Its implementation can vary from being very manual (pencil and paper) to being formalized as part of build automation.
Reference: Unit testing Wikibook
What is Unit Testing ?
By Definition, In computer programming, unit testing is a method by which individual units of source code are tested to determine if they are fit for use. A unit is the smallest testable part of an application. In procedural programming a unit may be an individual function or procedure. Unit tests are created by programmers or occasionally by white box testers.
Some Benefits of Unit Testing:
Bug Tracking
Code Maintenance
Understandable code
How the above things help in actual scenario ?
Suppose you have developed a code from scratch and it went to maintenance. Now you left the company, there are no unit test written and a bug came via customer. The app went for a hot fix and some other guy fix it, and his fix created another which in turn creates another issue.
So the creation of issue cycle is continuous as nobody knows the complete actual scenarios and the actual (original) dev has left the company and the company went haywire when an issue came.
Now whose fault is this ?
Let me tell you, its original dev fault, as he didn't write the unit test cases.
If we would have written the unit test cases, then:
A wrong scenario would have failed the test case, and we can fix the code.
Code will become more understandable
Code in turn is maintainable.
Our Learning from our case study
Unit testing provides a sort of living documentation of the system. Developers looking to learn what functionality is provided by a unit and how to use it can look at the unit tests to gain a basic understanding of the unit's API.
Unit test cases embody characteristics that are critical to the success of the unit. These characteristics can indicate appropriate/inappropriate use of a unit as well as negative behaviors that are to be trapped by the unit. A unit test case, in and of itself, documents these critical characteristics, although many software development environments do not rely solely upon code to document the product in development.
By contrast, ordinary narrative documentation is more susceptible to drifting from the implementation of the program and will thus become outdated (e.g., design changes, feature creep, relaxed practices in keeping documents up-to-date).
So When a Test Fails Incorrectly please read above two paras or below two points.
Delete the failing test after verifying that it is no longer valid—essentially since the old requirement is either invalid or is tested elsewhere.
Change the old test so you test the new requirement (essentially using a new test), and test the old requirement under new settings (the test logic stays the same, but the initialization function may change).
Correct way to write Unit Tests (What is the biggest mistake developers generally do?)
In one line - Developers do Integration testing instead of unit testing.
Unit testing means we should not test external dependencies.
Simple Example is following.
Suppose we have following pseudo code:
Function loadData(Success, Fail){
if(Success){
helper.callThisWhenItsSuccess();
}
if(Fail){
helper.callThisWhenItsFail();
}
}
In above function if write a unit test case,
We should not worry about helper class and its function, the helper class should not be covered in this particular unit test If it is covered then it become type of integration testing.
How to avoid this in real time scenarios ?
Use mocks, Stubs and use correct assertions.
So ideally, each test case is independent from the others: substitutes like method stubs, mock objects, fakes and test harnesses can be used to assist testing a module in isolation. Unit tests are typically written and run by software developers to ensure that code meets its design and behaves as intended. Its implementation can vary from being very manual (pencil and paper) to being formalized as part of build automation.
Reference: Unit testing Wikibook
Comments
Post a Comment
Important - Make sure to click the Notify Me check-box below the comment to be notified of follow up comments and replies.