Sunday, 12 August 2012

UNIT TESTING


  • In computer programming, unit testing is a method by which individual units of source code, sets of one or more computer program modules together with associated control data, usage procedures, and operating procedures, are tested to determine if they are fit for use.
  • The goal of unit testing is to isolate each part of the program and show that the individual parts are correct.
Advantages 

  Find problems early
  • Unit tests find problems early in the development cycle. Unit tests are created before the code itself is written. When the tests pass, that code is considered complete.
  • The unit tests alert the development team of the problem before handing the code off to testers or clients, it is still early in the development process.

   Facilitates change
  • Unit testing allows the programmer to write test cases for all functions and methods so that whenever a change causes a fault, it can be quickly identified and fixed.
  • Readily available unit tests make it easy for the programmer to check whether a piece of code is still working properly.

   Simplifies integration
  • By testing the parts of a program first and then testing the sum of its parts, integration testing becomes much easier.
   Documentation
  • Unit testing provides a sort of living documentation of the system.
  • Unit tests provides a basic understanding of what functionality is provided by a unit and how to use it.
   Design
  • The basic building blocks of unit testing are 'test cases' -- single scenarios that must be set up and checked for correctness.
  • The testing code of a Test Case instance should be entirely self contained, such that it can be run either in isolation or in arbitrary combination with any number of other test cases.
  • An example of unit testing :
       import unittest
 
        class SimpleWidgetTestCase(unittest.TestCase):
            def setUp(self):
                self.widget = Widget("The widget")

        class DefaultWidgetSizeTestCase(SimpleWidgetTestCase):
            def runTest(self):
                assert self.widget.size() == (50,50), 
                            'incorrect default size'

        class WidgetResizeTestCase(SimpleWidgetTestCase):
            def runTest(self):
                self.widget.resize(100,150)
                assert self.widget.size() == (100,150), \
                       'wrong size after resize'
    
  • In this, if the 'setUp' method raises an exception while the test is running, the framework will consider the test to have suffered an error, and the 'runTest' method will not be executed.
  • The TestCase class of the unittest compares its arguments and if not same, will raise an exception and the test will immediately be considered failed.
  • Place the following code at the bottom of your test module:
        if __name__ == "__main__":
            unittest.main()
    
    Then we could run the code in the command line.
    Eg: % python unittest.py widgettests.WidgetTestSuite
  • The design document (the unit-test itself) can be used to verify that the implementation adheres to the design.
  • With the unit-test design method, the tests will never pass if the developer does not implement the solution according to the design.
  • A detailed example of unit testing is given here

Limitations 
  • Testing cannot be expected to catch every error in the program: it is impossible to evaluate every execution path in all but the most trivial programs. 
  • Unit testing by definition only tests the functionality of the units themselves. Therefore, it will not catch integration errors or broader system-level errors.
  • Like all forms of software testing, unit tests can only show the presence of errors; they cannot show the absence of errors.
  • Another challenge related to writing the unit tests is the difficulty of setting up realistic and useful tests.
  •  If these initial conditions are not set correctly, the test will not be exercising the code in a realistic context, which diminishes the value and accuracy of unit test results. 
  • Use of a version control system is essential in unit testing.
  • It is also essential to implement a sustainable process for ensuring that test case failures are reviewed daily and addressed immediately.
 
  • These are some basic ideas regarding unit testing. You could refer here for more details.

No comments:

Post a Comment