Why is Unit Testing so important?

Why should we write unit tests? Why is it so important? To realize that we consider a team that is developing a module (say, module A), however, did not consider unit testing that seriously.

What does happen to that module?

1Module A produced lot of bugs in the development phase since it was not tested adequately. And while fixing it– A produced severe impact on other modules too, because of the fact that integration was failing again and again. Hence, the dependent modules had to change as well to stabilize final delivery.

2That’s not the end of misery. In the release testing, huge amount of bugs are identified, which resulted from the uncertain behavior of A, and fixing those become a hell of a job for developers. Just to show you one old classic diagram of software development life cycle–

image

3After relevant modules are delivered, developers could not take pride for that because of the lack of confidence regarding whether they have  delivered a quality product (from a developers perspective, we think that it is  really imperative to deliver something from which we can take pride). Please refer to the done concept of agile development to get more idea on the quality of product that we are referring. 

4 Through the course of time, A would become more susceptible to changes. And developers would become really reluctant and not comfortable to change the codes for any change request. More so, refactoring would become error-prone and troublesome due to the lack of unit and integration tests.

So, after some time, developers would start referring A as “Dead Zone” or Legacy Code, due to difficulties in its maintainability.

Now if we go back to the days when the developers were planning or starting the development process, what would you advise them ?

The situation that we have just described,  its not new to most of the developers developing for a long time. That’s why some intelligent people in agile world sat together and came up with some basic rules and practices that we must follow to make software product more maintainable. One of core principle  is to performing unit testing considering all the test criteria.

1Unit and integration testing verifies semantically correctness of software modules and how they should interact with each other. In addition, impact of changes can be easily determined, and be thoroughly tested if unit testing is done properly. In addition, it becomes easier to do code refactoring, module integration and most importantly, allows quick bug detection. To sum up, according to our experience, unit testing   improves the maintainability of software to a great extent.

2Consider unit tests as the specification of modules or software.  Any developer new to software module could just go through the unit tests to get an overview of what that module semantically entails. Thus, not only units test improves maintainability, it also enhances understanding of complex software systems.

Any comment or suggestion regarding this post is highly appreciated. Thanks!


See More

Revisions

[R-1: 02-04-2013] Porting blog-post from the following URL: http://weblogs.asp.net/adilakhter/archive/2008/04/18/why-unit-testing-is-so-important.aspx .

TestContext – more on mstest

If we are in test driven development, we need to spend significant amount of time in writing unit tests. In VSTS unit testing framework, every test run create a unique folder in our machine and generate a test report ( *.trx format) for every test run-

testresult.jpg

Sometimes it’s needed to access the folder that current test run created. UnitTestAdapterContext class provides a set of properties related to current test run that can be accessed from inside unit tests.

To use the properties provided in UnitTestAdapterContext we need add the following properties in our Test Class –

private TestContext testContextInstance;

///
///Gets or sets the test context which provides
///information about and functionality for the current test run.
///
public TestContext TestContext {
 get
 {
 return testContextInstance;
 }
 set
 {
 testContextInstance = value;
 }
}

The property is automatically provided with a concrete instance of the TestContext type [1]. In essence, it implies that we do not need to instantiate or derive TestContext in our test class.

Take a look at the properties and functionalities offered by TestContext –

testconttextcd.jpg

 If I start debugging a unit test adding a break point and watch the TestContext, we can view different information about and functionality for current test run –   

unittestadaptercontext.jpg  

So, Current TestRun Directory can be accessed by just one line of code- 

 string testDirectory = TestContext.TestDir; 

Therefore , we can use class in our TestContext Unit Tests to get different informations about the current test run and we can use different functioanallity of this class – for example – BeginTimer() and EndTimer() would be verify useful to measure our application’s differenent module’s performance to validate certain non-functional requirements.

More details on TestContext is available here.

References
[1] http://msdn.microsoft.com/en-us/library/ms404699

Design a site like this with WordPress.com
Get started