Skip to main content

Unit Testing, In a right way.

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

Comments

Popular posts from this blog

Convert your datatable into generic poco object in c# using linq, ado and reflections.

Follow @harshit_parshii The most common problem that we face these days is to create a common class and method that can be used across all the projects and codes. So today I will be sharing my code where you can see how to make and create a generic function without using entity framework for ado. net. The scenario is like you have an old software that uses stored procedure to return set of entities as a data-table, you do not want to re-write the back-end code as you are creating a web API in c# which needs to be delivered asap. You need to map these data tables to models as you might be using MV* pattern. So here we will be doing one to one mapping of model to data- table, and in similar fashion insert or update can also be done. So basically we are converting a data-table to list of strongly typed object model to do CRUD operations. So we have following things before hand. A helper class is referenced as the database(dbFactory) which executes ado. ne...

Send a Fax in windows using faxcomexlib and TAPI in VB code .Net

An application that provides sending fax from faxmodem, connected to the computer, will be explained in the following post.  We can use Telephony Application Programming Interface (TAPI) and the Fax Service Extended Component Object Model (COM) API to send fax. The fax service is a Telephony Application Programming Interface (TAPI)-compliant system service that allows users on a network to send and receive faxes from their desktop applications. The service is available on computers that are running Windows 2000 and later. The fax service provides the following features: Transmitting faxes Receiving faxes Flexible routing of inbound faxes Outbound routing Outgoing fax priorities Archiving sent and received faxes Server and device configuration management Client use of server devices for sending and receiving faxes Event logging Activity logging Delivery receipts Security permissions The following Microsoft Visual Basic code example sends a fax. Note that...

DTMF (Mobile) based speed control of AC motor.

Introduction: With the advancement of ages from prehistoric to present day scenario our life has become more sophisticated and busy, so to ease the schedule of this busy life, technology has play the vital role in it and for its proper running of machine the technology has gone further by providing digitization of analog machinery and its use is enhanced day by day. This project is based on the same concept by wireless controlling the machinery through mobile system anywhere from the world. This project aims at Speed Control of AC motor using DTMF method; DTMF stands for dual tone multiple frequencies . The main idea of this project is to control the speed of an AC motor by wireless communication using DTMF decoder technique aiming at the fine use of mobile technology in our day to day use of automated products. Mobile phones have different frequencies for each number printed on it. These numbers when pressed during call duration produces a tone of certain frequency. This frequ...