Skip to main content

Unit Testing

Unit tests are typically written and run by software developers to ensure that code meets its design and behaves as intended. It is important to write the unit test code early and update it as the application code changes. Unit testing is very important in all phases of the development cycle.


Unit Testing: Benefits
  • Finds Problems easily 
            In test-driven development, unit tests are created before the code itself
            is written. When the tests pass, that code is considered complete.
  • Facilitates change
            Unit testing method allows the program to change in the future and
            make sure that it works properly.
  • Simplifies integration
            By testing the parts of a program first and then testing the sum of its 
            parts, integration testing becomes much easier. But an elaborate
            hierarchy of unit tests does not equal integration testing.
  • Documentation
            Unit testing provides a documentation to the system. Unit test cases 
            contain certain characteristics that are critical to the success of the 
            unit. These characteristics can indicate use of a unit.
  • Design
            Each unit test can be seen as a design element specifying classes,
            methods, and observable behaviour. The following Java example will help
            illustrate this point.
            
            
              class SanityCheck(unittest.TestCase):        
              def testSanity(self):                    
                  """fromRoman(toRoman(n))==n for all n"""
                  for integer in range(1, 4000):       
                      numeral = roman.toRoman(integer) 
                      result = roman.fromRoman(numeral)
                      self.assertEqual(integer, result)
 
  
            The above given code is a test class that checks the correctness of a
            program that converts the integer given to roman numeral and vice
            versa. Here the test class checks whether the given integer falls in the
            range (0,3999).(The conversion program works only for integers
            inside that range). The complete conversion and the corresponding unit
            test program is available here.


            The above given code segment is just an example to illustrate the form of
            a test class. There are certain points to be noted here.
            
            * To write a test case, first subclass the TestCase class of the unittest 
               module. This class provides many useful methods which you can use in
               your test case to test specific conditions. 
            * The TestCase class of the unittest provides the methods like       
      assertEqual(),assertRaises() etc. assertEqual() compares its arguments
               and if not same, will raise an exception and the test will immediately be considered 
               failed.


Unit testing: Limitations
  • Testing cannot be expected to catch every error in the program. It is impossible to evaluate every execution path in most trivial programs.
  • Requires a number of lines of codes in unit test code to test a single line of code.
  • Challenge of setting relevant initial condition so that the part of the code being tested behaves as the part of the whole system.
  • Must keep rigorous discipline and must maintain a version control system.
  • It is also essential to implement a sustainable process for ensuring that test case failures are reviewed daily and addressed immediately.


The above points explains some basics of unit testing. Please refer more details.
Unit testing is well explained with the help of above said conversion example here.

Comments

Popular posts from this blog

Backbone.js - An Introduction

Backbone.js is a lightweight JavaScript library that adds structure to your client-side code.  Developers commonly use libraries like Backbone.js to create single-page applications (SPAs).  What is a Single-Page-Application? A single page Web App is a website that attempts to recreate the experience of a native desktop or mobile application. On a Single Page Web App you are less likely to see page refreshes, the browser navigation within the app is often overridden and  there will usually be some offline capabilities. To a user their interaction with a Single Page Web App will be almost identical to how they interact with a full native application. Backbone has many advantages over other js frameworks. Backbone is mature, popular, and has a number  of plugins and extensions available that build upon it. It has been used to create non-trivial applications by companies such as Disqus, Walmart, SoundCloud and LinkedIn. Backbone organizes its whole code under t...

Elements of Programming in Scheme

A powerful programming language provides a framework to interpret our ideas of processes. It generally combines small procedures to complex ones. Usually there are three steps to accomplish this. primitive expressions , which represent the simplest entities the language is concerned with, means of combination , by which compound elements are built from simpler ones, and means of abstraction , by which compound elements can be named and manipulated as units. Some very basic ideas in manipulating with the data are given below. Expressions   A very simple expression is a number. Simple expressions are combined to form complex ones. For example:                   (+ 2  3  4) is an expression where + is the operation and 2,3,4 are the oparents. The expression can be made more complex by combining simple expressions as given below:              ...

Abstraction Using Higher Order Procedures

Procedures are, in effect, abstractions that describe compound operations on numbers independent of the particular numbers. Yet even in numerical processing we will be severely limited in our ability to create abstractions if we are restricted to procedures whose parameters must be numbers. Often the same programming pattern will be used with a number of different procedures. To express such patterns as concepts, we will need to construct procedures that can accept procedures as arguments or return procedures as values. Procedures as arguments Procedures are often used as the arguments of an another procedure. This provides a higher level of abstraction in programming. For example consider a procedure to find out the sum of squares of numbers between a range. The procedures inc and square are used as the arguments of the procedure sum, which is generalised as follows.                 (define (inc...