Unit Test

Unit Test performs white-box testing on source code modules -- usually at the class level. The unit tests are implemented using the jUnit unit testing framework. The unit test cases are required to measure the functional quality of the coding as far upstream as possible. The unit tests are completely automated. The unit tests are generated by developers concurrently with their production code.

More Information

Tools

The ant build script can be used to run the unit tests. It runs the umich.cac.test.AllUnitTests unit test suite. To run the tests type


         ant runtests

        
in the top level directory where the build.xml file is located

Test Case Location

The unit test cases reside in a test sub-package of the package the unit test refers to. For example, unit tests for the umich.cac.util package (located in the .../umich/cac/util directory) will reside in the umich.cac.util.test package (located in the .../umich/cac/util/test directory.) The unit test classes have to belong to a different package than their production class counterparts so that they better mimic client code and so that the unit test classes are not included in the jar files.

When to Create a Test

  1. New unit test case for each new Java class (except abstract classes and interfaces.)

  2. New unit test case for each existing Java class that is to be modified/enhanced. The new test case will baseline the existing code to ensure that you do not unintentionally break anything. The new test case will verify the added/modified functionality.

  3. New "AllTests" unit test case suite for each unit test package. The "AllTests" suite has to be manually updated when further unit test cases are added to its package.

  4. "AllUnitTests" unit test case suite in umich.cac.test that runs all of the "AllTests" suites located in the other test packages. The "AllUnitTests" suite has to be manually updated when further "AllTests" classes are added to new test packages.

How to Create a Test

Create the Java class for the unit test:

  1. Create a class called XxxTest inherited from junit.framework.TestCase where Xxx is the name of the production class this test case is testing

  2. Place the class in the test sub-package of package the production class resides in

  3. Add constructor taking "String name" parameter

  4. Add "public static Test suite()" method

  5. Add test methods named "public void testX()" where X is a public method in the production class

Add the test case to the suites:

  1. Add XxxTest.suite() to the local AllTests class if it is not already present

  2. Add AllTests.suite() to umich.cac.test.AllUnitTests class if it is not already present

The unit test case will:

  1. demonstrate capability of the production class (all public methods)

  2. demonstrate intentional failures in the production class (expected exceptions, expected error codes, etc.)

The unit test case will not:

  1. be dependent on other tests

  2. be required to run in a certain order

  3. rely on databases or other network services being live (this is beyond unit testing scope and is handled by other tools)

Caveats

Note that the unit tests were developed to run on linux, so the testcases contain linefeeds for Unix, if you run the tests on a NT platform the line feeds are different and may result in unit tests saying that there are failures since the code generates line feeds for the platform.