Talk:CSC/ECE 517 Fall 2009/wiki1a 1 JunitTest: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
'''Writing Effective JUnit Test Cases'''<br>
'''----------------------------------------------------'''<br><br>
JUnit is a testing framework aimed at making unit testing easy by simplifying the writing of test cases and by providing automated running of tests and test suites.
The main aim of this document is to provide a repository of rules that can be used in-order to write an effective Junit test.
This doc also discusses various sites and suggest which are the best sites to read for an overview of the topic and to understand the different philosophies of test-case design.
==Introduction==
===Unit Testing Overview===
The primary goal of unit testing is to take the smallest piece of testable software in the application, isolate it from the remainder of the code, and determine whether it behaves exactly as you expect.
''Test Case''<br>
It describes a scenario that can be exercised through the application.<br>
Each test case is divided into two parts: input and expected output. <br>
The input part lists all the test case statements that create variables or assign values to variables.<br>
The expected output part indicates the expected results; it shows either assertions or the message 'no exception' (when no assertions exist). <br><br>
The main issue in constructing a unit test is deciding the scope of test cases.<br>
If the scope is too narrow, then the tests will be trivial and the objects might pass the tests, but there will be no design of their interactions and interactions of objects are important for object oriented design.<br>
On the other hand if the scope is too broad, then there is a high chance that not every component of the new code will get tested.<br>
The programmer is then reduced to testing-by-poking-around, which is not an effective test strategy.<br>
===Junit Overview===
Non-automated testing is usually like this:<br>
* An expression is used 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.<br>
* Expressions are tested as statements which print to the standard output stream.<br>
<br>
But its not preferred because:<br>
* They require human judgment to analyze their results.<br>
* They are cumbersome, non repeatable - you can only execute one debug expression at a time and a program with too many print statements causes the dreaded "Scroll Blindness".
<br><br>
On the upper hand, JUnit tests, which uses the automated framework does not require human judgment to interpret, and it is easy to run many of them at the same time.
<br>
When you need to test something, here is what you do:<br>
* Annotate a method with @org.junit.Test<br>
* When you want to check a value, import org.junit.Assert.* statically, call assertTrue() and pass a boolean that is true if the test succeeds<br>
<br><br>
''So what exactly is JUnit ?? ''<br>
It is a open-source unit testing framework for Java and provides a way to organize test data and perform repeatable  test execution.<br>
In JUnit, one has to write Java code, called a test class, that describes test data, invokes the methods to be tested, and determines test results.<br>
<br>
JUnit was originally written by Erich Gamma and Kent Beck.<br>
The official JUnit home page is http://junit.org.<br>
JUnit is Open Source Software, released under IBM's Common Public License Version 0.5 and hosted on SourceForge.<br>
<br>
It is an instance of the xUnit architecture for unit testing frameworks. JUnit features include:<br>
* Assertions for testing expected results<br>
* Test fixtures for sharing common test data<br>
* Test runners for running tests<br>
<br>
JUnit mailing lists and forums<br>
[http://groups.yahoo.com/group/junit/ JUnit user list]<br>
[http://lists.sourceforge.net/lists/listinfo/junit-announce JUnit announcements]<br>
[http://lists.sourceforge.net/lists/listinfo/junit-devel JUnit developer list]<br>
<br>
JUnit official documentation<br>
[http://junit.sourceforge.net/doc/testinfected/testing.htm JUnit Test Infected: Programmers Love Writing Tests]<br>
[http://junit.sourceforge.net/doc/cookbook/cookbook.htm JUnit Cookbook]<br>
[http://junit.sourceforge.net/doc/cookstour/cookstour.htm JUnit - A Cook's Tour]<br>
[http://junit.sourceforge.net/doc/faq/faq.htm JUnit FAQ]<br>
<br>
Although JUnit testing costs some time and money, but unit testing provides some crucial advantages, viz:<br>
Automation of the testing process<br>
Reduces difficulties of discovering errors contained in more complex pieces of the application<br>
Test coverage is often enhanced because attention is given to each unit.<br>
<br>
For example, <br>
if you have two units and decide it would be more cost effective to glue them together and initially test them as an integrated unit, an error could occur in a variety of places, now:<br><br>
* Is the error due to a defect in unit 1?<br>
* Is the error due to a defect in unit 2?<br>
* Is the error due to defects in both units?<br>
* Is the error due to a defect in the interface between the units?<br>
* Is the error due to a defect in the test?<br>
<br>
Finding the error (or errors) in the integrated module is much more complicated than first isolating the units, testing each, then integrating them and testing the whole.<br>
<br>
==Websites to lookout==
These are some of the best webpages which will help you in getting more description as well as details about Junit.
<br>Also it will help you understand the test case design philosophy.<br><br>
===For Overview===
* Junit testing basics:<br>
  http://articles.techrepublic.com.com/5100-10878_11-1027676.html       
* Basics of Junit:<br>
  http://junit.sourceforge.net/doc/cookbook/cookbook.htm
  http://junit.sourceforge.net/doc/faq/faq.htm#overview
===For Ideas on test case design===
* For successful test automation:<br>
  http://www.io.com/~wazmo/papers/seven_steps.html
* Unit testing principles:<br>
  http://www.acm.org/ubiquity/views/t_burns_1.html
* For test suite effectiveness:<br>
  http://java.sys-con.com/node/299945
==How to get started==
===Setup===
===Simple testing example===
==Rules for writing effective Junit test cases==
==Conclusion==
==References==
-------------------------------------------------------
== Step 1:  Installing JUnit ==
== Step 1:  Installing JUnit ==



Revision as of 23:11, 7 September 2009

Writing Effective JUnit Test Cases
----------------------------------------------------

JUnit is a testing framework aimed at making unit testing easy by simplifying the writing of test cases and by providing automated running of tests and test suites.

The main aim of this document is to provide a repository of rules that can be used in-order to write an effective Junit test. This doc also discusses various sites and suggest which are the best sites to read for an overview of the topic and to understand the different philosophies of test-case design.

Introduction

Unit Testing Overview

The primary goal of unit testing is to take the smallest piece of testable software in the application, isolate it from the remainder of the code, and determine whether it behaves exactly as you expect.

Test Case
It describes a scenario that can be exercised through the application.
Each test case is divided into two parts: input and expected output.
The input part lists all the test case statements that create variables or assign values to variables.
The expected output part indicates the expected results; it shows either assertions or the message 'no exception' (when no assertions exist).

The main issue in constructing a unit test is deciding the scope of test cases.
If the scope is too narrow, then the tests will be trivial and the objects might pass the tests, but there will be no design of their interactions and interactions of objects are important for object oriented design.
On the other hand if the scope is too broad, then there is a high chance that not every component of the new code will get tested.
The programmer is then reduced to testing-by-poking-around, which is not an effective test strategy.

Junit Overview

Non-automated testing is usually like this:

  • An expression is used 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.
  • Expressions are tested as statements which print to the standard output stream.


But its not preferred because:

  • They require human judgment to analyze their results.
  • They are cumbersome, non repeatable - you can only execute one debug expression at a time and a program with too many print statements causes the dreaded "Scroll Blindness".



On the upper hand, JUnit tests, which uses the automated framework does 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, here is what you do:

  • Annotate a method with @org.junit.Test
  • When you want to check a value, import org.junit.Assert.* statically, call assertTrue() and pass a boolean that is true if the test succeeds



So what exactly is JUnit ??
It is a open-source unit testing framework for Java and provides a way to organize test data and perform repeatable test execution.
In JUnit, one has to write Java code, called a test class, that describes test data, invokes the methods to be tested, and determines test results.

JUnit was originally written by Erich Gamma and Kent Beck.
The official JUnit home page is http://junit.org.
JUnit is Open Source Software, released under IBM's Common Public License Version 0.5 and hosted on SourceForge.

It is an instance of the xUnit architecture for unit testing frameworks. JUnit features include:

  • Assertions for testing expected results
  • Test fixtures for sharing common test data
  • Test runners for running tests


JUnit mailing lists and forums
JUnit user list
JUnit announcements
JUnit developer list


JUnit official documentation
JUnit Test Infected: Programmers Love Writing Tests
JUnit Cookbook
JUnit - A Cook's Tour
JUnit FAQ


Although JUnit testing costs some time and money, but unit testing provides some crucial advantages, viz:
Automation of the testing process
Reduces difficulties of discovering errors contained in more complex pieces of the application
Test coverage is often enhanced because attention is given to each unit.

For example,
if you have two units and decide it would be more cost effective to glue them together and initially test them as an integrated unit, an error could occur in a variety of places, now:

  • Is the error due to a defect in unit 1?
  • Is the error due to a defect in unit 2?
  • Is the error due to defects in both units?
  • Is the error due to a defect in the interface between the units?
  • Is the error due to a defect in the test?


Finding the error (or errors) in the integrated module is much more complicated than first isolating the units, testing each, then integrating them and testing the whole.

Websites to lookout

These are some of the best webpages which will help you in getting more description as well as details about Junit.
Also it will help you understand the test case design philosophy.

For Overview

  • Junit testing basics:
 http://articles.techrepublic.com.com/5100-10878_11-1027676.html        
  • Basics of Junit:
 http://junit.sourceforge.net/doc/cookbook/cookbook.htm
 http://junit.sourceforge.net/doc/faq/faq.htm#overview

For Ideas on test case design

  • For successful test automation:
 http://www.io.com/~wazmo/papers/seven_steps.html
  • Unit testing principles:
 http://www.acm.org/ubiquity/views/t_burns_1.html
  • For test suite effectiveness:
 http://java.sys-con.com/node/299945

How to get started

Setup

Simple testing example

Rules for writing effective Junit test cases

Conclusion

References


Step 1: Installing JUnit

Windows
To install JUnit on Windows, follow these steps:

  • Unzip the junit.zip distribution file to a directory referred to as %JUNIT_HOME%.
  • Add JUnit to the classpath:

set CLASSPATH=%JUNIT_HOME%\junit.jar
Unix (bash)
To install JUnit on Unix, follow these steps:

  • Unzip the junit.zip distribution file to a directory referred to as $JUNIT_HOME.
  • Add JUnit to the classpath:

export CLASSPATH=$JUNIT_HOME/junit.jar

Step 2: Writing a Test Case

To write a test case, follow these steps: 1. Implement a class derived from TestCase, a base class provided by JUnit.

   public class ShoppingCartTest extends TestCase

2. Be sure the test class belongs to the same package as that of the unit you wish to test 3. Override the setUp() method to initialize object(s) under test. 4. Optionally override the tearDown() method to release object(s) under test. 5. Define one or more public testXXX() methods that exercise the object(s) under test and assert expected results. The following is an example test case:

import junit.framework.TestCase;

public class ShoppingCartTest extends TestCase {

    private ShoppingCart cart1;
    private ShoppingCart cart2;

    /**
     * Sets up the test fixture.
     *
     * Called before every test case method.
     */
    protected void setUp() {

        cart1 = new ShoppingCart(6,8);
        cart2 = new ShoppingCart(6,8);
    }

    /**
     * Tears down the test fixture.
     *
     * Called after every test case method.
     */
    protected void tearDown() {
        // release objects under test here, if necessary
    }

    /**
     * Tests emptying the cart.
     */
    public void testEmpty() {

        cart1.empty();
        cart2.empty();
        assertEquals(cart1, new ShoppingCart(3, 9));
        assertTrue(!cart2.equals(cart1));
    }
   }

Methods used in JUnit:
1. An assert method is a JUnit method that performs a test, and throws an AssertionFailedError if the test fails
2. static void assertTrue(boolean test)
static void assertTrue(String message, boolean test)

  • Throws an AssertionFailedError if the test fails
  • The optional message is included in the Error

3. static void assertFalse(boolean test)
static void assertFalse(String message, boolean test)

  • Throws an AssertionFailedError if the test fails

Step 3: Writing a Test Suite

Next, we'll write a test suite that includes several test cases. The test suite will allow us to run all of its test cases in one fell swoop.
To write a test suite, follow these steps:
1. Write a Java class that defines a static suite() factory method that creates a TestSuite containing all the tests.
2. Optionally define a main() method that runs the TestSuite in batch mode.

The following is an example test suite:

import junit.framework.Test;
import junit.framework.TestSuite;

public class ShoppingCartTestSuite {
  
    public static Test suite() {

        TestSuite suite = new TestSuite();
  
        //
        // The ShoppingCartTest we created above.
        //
        suite.addTestSuite(ShoppingCartTest.class);

        //
        // Another example test suite of tests.
        // 
        suite.addTest(CreditCardTestSuite.suite());

        //
        // Add more tests here
        //

        return suite;
    }

    /**
     * Runs the test suite using the textual runner.
     */
    public static void main(String[] args) {
        junit.textui.TestRunner.run(suite());
    }
}

Step 4: Running the Test

Now that we've written a test suite containing a collection of test cases and other test suites, we can run either the test suite or any of its test cases individually. Running a TestSuite will automatically run all of its subordinate TestCase instances and TestSuite instances. Running a TestCase will automatically invoke all of its public testXXX() methods. JUnit provides both a textual and a graphical user interface. Both user interfaces indicate how many tests were run, any errors or failures, and a simple completion status. The simplicity of the user interfaces is the key to running tests quickly. You should be able to run your tests and know the test status with a glance, much like you do with a compiler.

To run our test case using the textual user interface, use:

java junit.textui.TestRunner ShoppingCartTest 

The textual user interface displays "OK" if all the tests passed and failure messages if any of the tests failed.
To run the test case using the graphical user interface, use:

java junit.swingui.TestRunner ShoppingCartTest 


The graphical user interface displays a Swing window with a green progress bar if all the tests passed or a red progress bar if any of the tests failed.
The ShoppingCartTest Suite can be run similarly:

java junit.swingui.TestRunner ShoppingCartTestSuite