Unit Testing: Examples, Concepts and Frameworks

9 July, 2008 at 21:44 11 comments

“Testing is the process of executing a program with the intent of finding errors” (Myers)
“Testing can show the presence of bugs, but not the absence” (Dijkstra)

A good test case is one that has a high probability of detecting an as yet undiscovered error or a successful test case is one that detects an as yet undiscovered error (Myers).

Basic notions

Defining Terms

  • Error – mistake made by human during implementation of a software system.
  • Failure – incorrect behaviour of a program.
  • Fault – incorrect code that caused a failure.
  • Incident – symptoms associated with a failure.
  • Bug – an error or fault.
  • Fault Directed testing – finding bugs through failure
  • Test Case – A test described by test data, environment and expected result.
  • Test Suite – a collection of test cases.
  • Test Plan – document describing testing approach, test suites and test cases.
  • Test Strategy – a way to identify test cases from a specification or implementation.
  • Test Effectiveness – relative ability of test strategy to find bugs.
  • Test Efficiency – relative cost of finding a bug.
  • Test Coverage – the percentage of testable elements that have been tested.

Granularity Levels

  • Unit – verification of single classes.
  • Module – testing interaction of groups of classes (package).
  • Integration – verification of interactions between modules/components.
  • Functional – verification of external behaviours of modules, components and system.
  • System – testing of the system against objectives.
  • Acceptance – validation of application against user requirements (use cases).
  • Regression – re-running all tests on system when it is changed.

Unit testing

Unit testing is a test (often automated) that validates that individual units of source code are working properly. A unit is the smallest testable part of an application. In procedural programming a unit may be an individual program, function, procedure, etc., while in object-oriented programming, the smallest unit is a method, which may belong to a base/super class, abstract class or derived/child class.

Ideally, each test case is independent from the others; Double objects like stubs, mock or fake objects[1] as well as test harnesses can be used to assist testing a module in isolation. Unit testing is typically done by software developers to ensure that the code they have written meets software requirements and behaves as the developer intended.

Test is not a unit test if:

  • It talks to the database
  • It communicates across the network
  • It touches the file system
  • It can’t run at the same time as any of your other unit tests
  • You have to do special things to your environment (such as editing config files) to run it.

Unit testing frameworks

Unit testing frameworks, which help simplify the process of unit testing, have been developed for a wide variety of languages. It is generally possible to perform unit testing without the support of specific framework by writing client code that exercises the units under test and uses assertion, exception, or early exit mechanisms to signal failure. This approach is valuable in that there is a non-negligible barrier to the adoption of unit testing. However, it is also limited in that many advanced features of a proper framework are missing or must be hand-coded.

JUnit framework

JUnit is a unit testing framework for the Java programming language. Created by Kent Beck and Erich Gamma, JUnit is one of the xUnit family of frameworks that originated with Kent Beck’s SUnit. JUnit has spawned its own ecosystem of JUnit extensions.

Experience gained with JUnit has been important in the development of test-driven development, and as a result, some knowledge of JUnit is often presumed in discussions of test-driven development.

JUnit has been ported to other languages, including PHP (PHPUnit), C# (NUnit), Python (PyUnit), Fortran (fUnit), Perl (Test::Class and Test::Unit) and C++ (CPPUnit). This family of unit testing frameworks is referred to collectively as xUnit. TestNG has many of the same goals as JUnit.

And more recently, owing to the development of rich client frameworks such as AJAX, a port of JUnit has been developed for JavaScript (JSUnit).

JUnit Example

Here is a simple class with public methods we want to test using Junit :

  • getHelloWorld, return a String
  • getTruth, return a boolean
  • getElement and setElement, which throw exceptions

public class Foo {
	private int[] array;
        public Foo(int size) {
	    array= new int[size];
	public String getHelloWorld() {
		return  "Hello World";
	public boolean getTruth() {
	    return true;
	public void setElement(int position, int value) throws ArrayIndexOutOfBoundsException {
		array[position] = value;
	public int getElement(int position) throws ArrayIndexOutOfBoundsException {
	    return array[position];

How do you used to write testing code?

The simplest way is as an expression in a debugger. You can change debug expressions without recompiling, and you can wait to decide what to write until you have seen the running objects. You can also write test expressions as statements which print to the standard output stream. Both styles of tests are limited because they require human judgment to analyze their results. Also, they don’t compose nicely- you can only execute one debug expression at a time and a program with too many print statements causes the dreaded “Scroll Blindness”.

Using Junit, you won’t “pollute” the source code because test code is separated from Business classes.

JUnit tests do not require human judgment to interpret, and it is easy to run many of them at the same time. When you need to test something, you create an instance of TestCase…. (more)

TestNG framework

TestNG is a testing framework created by Cedric Beust for the Java programming language inspired by JUnit and NUnit but introducing some new functionalities that purport to make it more powerful and easier to use.

TestNG features include:

  • JDK 5 Annotations (JDK 1.4 is also supported with JavaDoc annotations).
  • Flexible test configuration.
  • Support for data-driven testing (with @DataProvider).
  • Support for parameters.
  • Allows distribution of tests on slave machines.
  • Powerful execution model (no more TestSuite).
  • Supported by a variety of tools and plug-ins (such as Eclipse, IDEA, Maven).
  • Embeds BeanShell for further flexibility.
  • Default JDK functions for runtime and logging (no dependencies).
  • Dependent methods for application server testing.

TestNG is designed to cover all categories of tests, including unit, functional, and integration tests.

TestNG Example

Here is a very simple test:

package example1;

import org.testng.annotations.*;

public class SimpleTest {

 public void setUp() {
   // code that will be invoked when this test is instantiated

 @Test(groups = { "fast" })
 public void aFastTest() {
   System.out.println("Fast test");

 @Test(groups = { "slow" })
 public void aSlowTest() {
    System.out.println("Slow test");


The method setUp() will be invoked after the test class has been built and before any test method is run. In this example, we will be running the group fast, so aFastTest() will be invoked while aSlowTest() will be skipped.

Things to note:

  • No need to extend a class or implement an interface.
  • Even though the example above uses the JUnit conventions, our methods can be called any name you like, it’s the annotations that tell TestNG what they are.
  • A test method can belong to one or several groups. These groups will be used at runtime to determine what test methods should be invoked..

Once you have compiled your test class into the build directory, you can invoke your test with the command line, an ant task (shown below) or an XML file:

<project default="test">

 <path id="cp">
   <pathelement location="lib/testng-testng-4.4-jdk15.jar"/>
   <pathelement location="build"/>

 <taskdef name="testng" classpathref="cp"
          classname="org.testng.TestNGAntTask" />

 <target name="test">
   <testng classpathref="cp" groups="fast">
     <classfileset dir="build" includes="example1/*.class"/>


Use ant to invoke it:

c:> ant
Buildfile: build.xml

[testng] Fast test
[testng] ===============================================
[testng] Suite for Command line test
[testng] Total tests run: 1, Failures: 0, Skips: 0
[testng] ===============================================

Total time: 4 seconds

Then you can browse the result of your tests:

start test-output\index.html

[Rober Graham UCL London 2007]

Bookmark and Share


Entry filed under: TDD, Test, Testing, Unit testing. Tags: , , .

Dan Kaminsky: bug in DNS Java Annotations: Concepts, Methods and Examples

11 Comments Add your own

  • 1. 83g1tf  |  15 May, 2009 at 01:19

    test test test

  • 2. DIPESH KUMAR IT PROFF DBRAIT  |  16 October, 2010 at 14:44

    goood wellllllllllllllll…………..

  • 3. rajeshdey  |  11 November, 2010 at 10:41

    could someone give step by step with examples from each type .

    • 4. houga moudarres  |  14 May, 2012 at 14:38

      need if it s possible some examples for a testing exame question and answard for driving license thanks

  • 5. eswaran k  |  24 August, 2011 at 07:01

    pleases unit testing example send me

  • 6. Yogesh  |  16 June, 2012 at 09:12

    I want know that who does unit Testing ??? Software Tester or developer? Please replay……

  • 7. H.syama manasa  |  2 September, 2012 at 16:20

    plz send theory example

  • 8. sivam  |  3 September, 2012 at 07:26


  • 9. heavy periods during pregnancy  |  29 November, 2012 at 12:30

    Thank you, I have recently been looking for info about this subject for
    a while and yours is the best I have discovered till now.
    However, what concerning the bottom line? Are you positive concerning the

  • 10. juvy  |  6 January, 2013 at 09:05

    gud day to all plz help me with my assignment about the examples of principles of testing……..thx.

  • 11. Chastity  |  3 February, 2013 at 00:26

    You may not have considered driving or taking a cruise ship,
    either. Rather than driving forward or in reverse– or sitting in park– I will
    allow more time in neutral. For those of us with
    less than stellar credit, often we do find ourselves stuck in an
    auto loan with a high interest rate.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed

IT Passion’s Store



Get the Source
OSGi supporter
JUG Milano

Upcoming Events


Blog Stats

  • 371,507 hits

My PageRank

What's My Google PageRank?

%d bloggers like this: