Vicky Wang
JUnit is a great regression testing framework, while lots of people will hard
code test data in code because its "no-param, no-return" design. You have
to recompile your code when you want to change the test data if you hard code
the test data in your code. There are various ways you can separate test code
and data, for example, you can use resource bundle or properties file to represents
test data, or use some extension such as JTestCase. But the problem is all these
methods have a little difficult to use because they are not supported by JUnit core.
Please refer to JUnit Recipes
for more information about how to separate test data from code with JUnit framework.
Jetif is a regression testing framework was designed to resolve above problem, while not extension to JUnit. It resolve the problem in the core framework, so you will be able to create more straightforward test code with Jetif.
Below table shows the feature difference between Jetif and JUnit + JTestCase.
Feature | Jetif | JUnit + JTestCase |
Sepearate test code and data | Supported by Handler | Supported by JTestCase or other mechanism |
Parameters & Return Value | Supported by Handler | No |
Logging | Yes | Supported by extension |
DataTypes | Limited built-in supported, extendable by user | Limited supported |
Client/Server Testing | Yes | Supported by extension |
You can create more straightforward test code with Jetif, because the parameters and return value in test method are supported by Jetif. For example, a test method can be writtern as below with Jetif:
public boolean testSomething(float param1, String[] param2, Integer param3){ // Test something with the param1, param2, param3 return true; }
While a test method can only be writtern as below with JUnit + JTestCase:
public void testSomething() { // get the test cases from XML Vector testCases = _jtestcase.getNameOfTestCases("testSomething"); // for each test case for (int i=0; i<testCases.size(); i++) { // retrieve name of test case String testCase = (String)testCases.elementAt(i); // get hashed params for this test case Hashtable params = _jtestcase.getTestCaseParams("testCalculate", testCase); int var1 = ((Integer)params.get("var1")).intValue(); String var2 = (String)params.get("var2"); // Now comes to what we need to test. } }
With Jetif, you can define the parameters and expected return value in spec file, and then just use them as parameters and return a value what you want in test code, it's more simple and more clear than JUnit, isn't it?
Jetif will not only make your code more simple, but also make your data organized with more clear structure. Below is a sample method spec for above Jetif test method:
<method name="testSomething"> <param>42.0</param> <param handler="array">value1, value2, value3</param> <param>32</param> <expected>true</expected> </method>
While with JUnit + JTestCase, you should define a more complex data structure for a test method, for example:
<method name="method name" test-case="name your test case here"> <params> <param type="java.util.Hashtable" key-type="java.lang.String"> <param name="Test1" type="java.util.Hashtable" key-type="java.lang.String" value-type="java.lang.String"> <param name="k1">value1</param> <param name="k2">value2</param> <param name="k3">value3</param> </param> <param name="k1" type="java.lang.String">value1</param> <param name="k2" type="java.lang.Integer">42</param> </param> </params> </method>
Both Jetif and JTestCase can support limited number of data types by default. While Jetif provides the Object Handler mechanism that allows user create they own handler to support various data types. When you want a data type to be supported, you can create a handler by yourself instead of waiting support from me.
I guess you have already make your conclusion now, right?
Translations: 简体中文
Copyright © 2004 Vicky Wang, All Rights Reserved.