XTest Unit Tests Tutorial
Version:1.0
Authors:- Daniel Pfeffer
- Vitezslav Stejskal
Abstract: This is the tutorial of XTest Unit tests support framework.
- Document History:
- 14 Mar 2001 : Daniel Pfeffer, version 0.1, status: draft, created
- 20 Mar 2001 : Daniel Pfeffer, version 0.2, updated
- 11 May 2001 : Vitezslav Stejskal, version 1.0, updated
- 11 Oct 2001: Martin Brehovsky, version 1.1, updated accomodate
recent changes in XTest
- Contents:
- Introduction
- Prerequisites
- Lessons
Unit tests are are checking code unit (classes)
functionality according system design specification. The test application
can be used for functionality, validation and compatibility tests. This document
describes steps how to build and run unit test using XTest unit tests support
framework. This document contains for each step "Lab" lessons describing
in details steps to get first "hands on" Xtest based unit tests experiences.
The JUnit module
and XTest needs to be installed for developing and running XTest unit
tests. If you intend to use NetBeans extension to JUnit (preffered when running
in XTest harness), you will need also to install this extension (nbjunit.jar,
available with XTest harness).
Assuming some existing module to be tested, the
test development process can be split in following steps:
- Lab
1
- create the code we will test
- Lab
2
- create selected class or package test application skeleton
- Lab
3
- fill in the test code in test application
- Lab
4
- run (debug) the test application within IDE with test results reported
in output window
Lab 1
- Create tested product
Let's create some classes for illustration ...
- Choose the root directory
of your imaginary product source code base (e.g. c:\myproduct).
- Create 'mymodule' sub-directory
of your product code base, where we will place source codes of your module
(the code we will test) and its unit tests.
- Create following structure
of sub-directories in module's directory.
c:\myproduct\mymodule ...
...\src ...\test ...\test\unit ...\test\unit\src
|
- In the IDE explorer
mount directory c:\myproduct\mymodule\src
- Create sample HelloWorld
class in mounted file system
public class HelloWorld extends Object { public HelloWorld(){}
public String greeting() { return "Hello world!"; } }
|
- Create another sample
class in "src/myorg" package
package myorg; public class Hello2 extends Object { public Hello2() {}
public String hello(String name) { return "Hello " + name + "!"; } }
|
Lab 2
- Create test application skeleton
The NetBeans
JUnit Module
can be effectively used to create skeleton of test application. Make
sure you have JUnit module installed before you will proceed this lab.
- In the IDE explorer
mount directory c:\myproduct\mymodule\test\unit\src
- Create test code skeleton
using JUnit Module, right-click node representing mounted module sources
(...\mymodule\src) and follow context menu (Tools -> Tests -> Create).
- In unit test generator
configuration dialog select mounted .../test/unit/src directory to be file
system containing generated tests.
- Press OK, HelloWorldTest
and myorg.Hello2Test classes should be generated.
- Compile generated test
sceletons.
- If you want to use NetBeans extension to JUnit,
which offers extra functionality over plain JUnit, like filtering test methods
or asserting files, you need also to do the following steps (future version
of JUnit module will be capable of generating also tests using NetBeans extension
to JUnit):
- Import NetBeans extension to JUnit classes
(import org.netbeans.junit.*;).
- Change the base class of your test class from
TestCase to NbTestCase.
- In method suite(), instead of creating new
TestSuite() class, create new NbTestSuite class.
Lab 3
- Fill in the test code
The test functions should be filled in skeletons
generated by JUnit module. The JUnit assert functions should be used to check
conditions and report failures when expected results doesn't match results
obtained in test execution.
You may write your test code casually without checking
for exceptional situations, because any exception thrown by your test code
is automatically catched and reported by the JUnit test framework causing
your test to fail.
Examples shown on this page use NetBeans extension
to JUnit.
- Complete the "HelloWorldTest"
class with following code:
import java.io.*; import java.net.URL; import junit.framework.*; import org.netbeans.junit.*;
public class HelloWorldTest extends NbTestCase { public HelloWorldTest(java.lang.String testName) { super(testName); } public static void main(java.lang.String[] args) { junit.textui.TestRunner.run(suite()); } public static Test suite() { TestSuite suite = new NbTestSuite(HelloWorldTest.class); return suite; } /** Test of greeting method, of class HelloWorld. */ public void testGreeting1() { String greeting; System.out.println("testGreeting1"); greeting = testObject.greeting(); assert(null != greeting); }
public void testGreeting2() throws IOException { File test; File pass; FileWriter wr; String greeting;
System.out.println("testGreeting2"); greeting = testObject.greeting();
test = new File(dataDir, "greeting.test"); pass = new File(dataDir, "greeting.pass"); wr = new FileWriter(test); wr.write(greeting); wr.close(); assertFile(test, pass, dataDir); } protected HelloWorld testObject; protected File dataDir; protected void setUp() { dataDir = new File(getClass().getResource("data").getFile()); testObject = new HelloWorld(); } }
|
- Complete the "Hello2Test"
class with following code:
package myorg;
import junit.framework.*; import org.netbeans.junit.*;
public class Hello2Test extends NbTestCase { public Hello2Test(java.lang.String testName) { super(testName); } public static void main(java.lang.String[] args) { junit.textui.TestRunner.run(suite()); } public static Test suite() { TestSuite suite = new NbTestSuite(Hello2Test.class); return suite; } /** Test of hello method, of class myorg.Hello2. */ public void testHello() { System.out.println("testHello"); String greeting = testObject.hello("Joe"); assertEquals(greeting, "Hello Joe!"); } protected Hello2 testObject; protected void setUp() { testObject = new Hello2(); } }
|
Lab 4
- Run test application
The test can be simply started from IDE and in case
of any defects easily "debugged" using test results reported in IDE output
window. JUnit module supports three types of test execution - external, internal
and debug, use menu Project -> Settings -> Unit tests to change active
type of execution (see JUnit module documentation for details).
- Right-click the RootSuite
class and follow context menu Tools -> Tests -> Run
- Test classes should
be recompiled and executed, test results are reported in IDE Output window.
Note, that reported failures are linked with test source codes where they
originated (double-click red lines in Output Window).
- There should be one
failure reported in your tests results. This is caused by the missing test
data.
- During the tests execution
there should be created .../test/unit/src/data/greeting.test file, rename
this file to greeting.pass.
- Run tests again, all
tests should pass now.
- Change content of the
greeting.pass file and run tests again. You should see the failure caused
by AssertionFileFailedException, check the .../data/greeting.diff file,
which should contain differences between the 'golden' file greeting.pass
and greeting.test file generated during the test execution.
Hint: You can execute tests form any of your
test classes by right-clicking the Tools -> Tests -> Run context menu.