White Box Testing
Test your code
   Home      UnitTesting
 
With emergence of test driven development and other agile process Unit testing becomes an important part of software development life cycle. Unit testing involves testing of a software application unit as whole. The goal of unit testing is to isolate each part of the program and show that the individual parts are correct. A unit test provides a strict, written contract that the piece of code must satisfy.
 
Generally in procedural programming or structured programming a unit is a individual program, function, procedure, etc., while in object-oriented programming, unit is a method and classes.
 

Why it is important?

For any complex system, it is highly inefficient and ineffective to test the system solely as a “black box”. Unit test gives developer confidence of properly working of code. It works as regression testing at developer ends which stops occurrences of old defects (new defects as well if new tests cases added).
 

Who should do it?

Unit testing is almost always done by the development team, developers are expected to build unit tests for each of their modules (these are typically method, classes, functions, or some other ‘atomic’ unit of code), and to ensure that each module passes its unit tests before the code is included in a build.

What it should cover?

Unit tests should cover only basic blocks of programs method, classes, functions, or some other ‘atomic’ unit of code as compared to the whole functionality. Unit tests should generally include Boundary value analysis, Inverse testing, Error cases and exception testing, performance testing etc.
 
Unit testing make sure the module still works correctly after the code changes and whenever a change causes a fault, it can be quickly identified and fixed. Each test case should be independent from the others; mock objects and test harnesses can be used to assist testing of a module in isolation.
 
As acceptance test gives user confidence that the application has the required features and that they behave correctly, unit test gives developer confidence of properly working of code.
 
 

Unit Testing Guideline 

1) Test at unit level

2) Keep tests independent

3) Make sure all paths in code is covered

4) Write Unit Tests For Each Alternative Path

5) Test each feature once for every class

6) Do regression testing

7) Design code with testing in mind

8) Unit tests should be automated

10) Measure the coverage of tests

11) Test both the positive and negative test scenario

12) Write unit test for each return code and error(exceptions) 

13) While doing Unit testing write test keeping in mind
  • Functional
  • Inputs
  • Outputs
  • Boundaries
  • Termination
  • Algorithms and Logic
  • Integration

14) Use them in that way to aid your development process
 
 
 
xUnit framework helps in implementing test driven development. These framework allow to test various units of program like methods, classes etc. The overall design of xUnit frameworks depends on several components.
 
Test Fixtures
A test fixture is the set of preconditions or state needed for a test to run. Also known as a test context.
 
Test Method
Test method have test for a method.
 
Test Class
Test class have test cases for a class.
 
Test Suites
A test suite is a set of tests or test classes that all share the same fixture.
 
Test Execution
The execution of an individual unit test proceeds as follows.
 
setup();
...
/* Body of test */
...
teardown();
 
The setup() and teardown() methods serve to initialize and clean up test fixtures.
 
Assertions
An assertion is a function or macro that verifies the behavior of the unit under test. Failure of an assertion typically throws an exception, aborting the execution of the current test.
 
 
xUnit framework now available as JUnit, NUnit, CPPUnit etc for almost all the programming language and platform