|
|
|
|
Tutorial: Accessing a datapool from a TPTP JUnit test
Objectives:
To create and access a datapool from an example test application using the Eclipse Test and
Performance Tools Platform (TPTP).
Time required
1 hour
Before you start this tutorial,
you need to
-
Install Eclipse and the Eclipse Test and Performance Tools
Platform (TPTP).
- Have a basic understanding of JUnit testing. For more information about JUnit testing, see
www.junit.org.
- Configure and run the Agent Controller which corresponds to your TPTP version. For more information, see
Getting started with the Agent Controller.
Description
In this tutorial,
you create and test an application called MyShoppingCart. Using the Eclipse Test and Performance Tools Platform (TPTP), you develop JUnit tests and create a datapool to provide data to the test environment.
This tutorial guides you through the following procedures:
-
Creating a sample user application
-
Setting up a TPTP test
suite
-
Creating a datapool and initializing test
data
-
Modifying test cases to reference a
datapool
In this procedure, you develop the MyShoppingCart class. In subsequent procedures, you will use the Eclipse Test and Performance Tools Platform to develop a corresponding test environment.
- Create a new Eclipse Java Project.
- From the File menu choose File > New > Project... The
New Project dialog appears.
- In the Wizards list, select Java Project and click Next.
The Create a Java project page appears.
- Type datapoolExample for the Project name and click Finish.
The datapoolExample project appears in the Navigator view.
- Create the MyShoppingCart class.
- In the Navigator view, right-click the datapoolExample
project and choose New > Class. The New Java Class dialog
appears.
- Type MyShoppingCart in the Name field.
- Clear the option to create the main method public static void
main(String[] args).
- Click Finish. The MyShoppingCart.java file appears in the Java
editor.
- Type the following for the MyShoppingCart source:
import java.util.Hashtable;
public class MyShoppingCart {
public Hashtable myFlowers;
public MyShoppingCart() {
myFlowers = new Hashtable();
myFlowers.put("Orchid", new Double(5.99));
myFlowers.put("Tulip", new Double(1.99));
myFlowers.put("Yellow Carnation", new Double(6.99));
myFlowers.put("White Rose", new Double(9.99));
myFlowers.put("Geraniums", new Double(4.99));
}
public double itemCost(String itemName, int itemQuantity)
{
Double price = (Double)myFlowers.get(itemName);
if (price != null) {
return price.doubleValue()*itemQuantity;
}
return -1;
}
}
- Save MyShoppingCart.java. From the File menu choose File >
Save.
In this procedure, you develop a test suite for the the MyShoppingCart class. Using the Eclipse Test and Performance Tools Platform, you develop a JUnit
test for the itemCost method.
- Open the Test Perspective.
- From the File menu, choose Window > Open Perspective >
Other.
- Select Test and click OK. The
Test Navigator of the
Test Perspective appears.
- Create a new TPTP JUnit Test.
- In the
Test Navigator of the
Test Perspective, right-click the datapoolExample project and choose New
> Test Element...
- In the Test Element dialog, select TPTP JUnit test and click Next. The New Test Case dialog appears, prompting you to add JUnit libraries. Click Yes to add JUnit libraries.
- In the New JUnit Test Source Code page, type MyShoppingCartTest in the Name field. In the Select how the test behavior is edited section, choose the edit In
the test editor option (the default).
- Click Finish. The TPTP JUnit Test editor appears, showing the
MyShoppingCartTest test suite. The Overview tab includes a test description, Source Information, and a Test Methods listing. Currently, no test methods are defined. For this tutorial, the TPTP JUnit Test editor generates method stubs so the Implement
Test Behavior as code option in the Source Information section should be cleared. For more information, see
JUnit Test Suite Editor.
- Add the testItemCost and testMyShoppingCartConstructor methods.
- In the Test Methods tab, click Add. A default name appears for your test.
- Add the testItemCost method. In the Name field, type testItemCost for the new test name. In the Description field, type Test
for the MyShoppingCart.itemCost(String, int) method.
- Add the testMyShoppingCartConstructor method. In the Test Methods tab, click Add. In the Name field, type testMyShoppingCartConstructor for the new test name. In the Description field, type Test
for the MyShoppingCart constructor.
- Configure the test execution behavior.
- Add a test execution loop. In the Behavior tab, click Add..
> Loop. In the Name field, type Loop_MyShoppingCart. In
the Number of Iterations field, type 1 (the default value).
- Add a testMyShoppingCartConstructor invocation. Select
Loop_MyShoppingCart and click Add... > invocation. The Test Invocation
dialog appears. Select Select testMyShoppingCartConstructor and
click OK.
- Add a testItemCost invocation. Select
Loop_MyShoppingCart and click Add... > invocation. The Test
Invocation dialog appears. Select testItemCost and click OK.
- Save the test suite. From the File menu, choose File > Save.
Note: the Save command causes the TPTP JUnit Test editor to create
test method stubs in MyShoppingCartTest.java.
-
Enter code for the generated JUnit test methods testMyShoppingCartConstructor and testItemCost.
- Open the Java Perspective. From the file menu choose Window
> Open Perspective > Other... > Java.
- Open MyShoppingCartTest.java. In the Navigator, open the datapoolExample project folder and double-click MyShoppingCartTest.java. The MyShoppingCartTest.java contents appear in the Java editor, including code to setup and execute the test suite, and stub methods for testMyShoppingCartConstructor and testItemCost.
- Type the following code for the testMyShoppingCartConstructor method.
public void testMyShoppingCartConstructor()
throws Exception
{
MyShoppingCart cart = new MyShoppingCart();
Double priceOrchid = (Double)cart.myFlowers.get("Orchid");
Double priceTulip = (Double)cart.myFlowers.get("Tulip");
assertTrue(priceOrchid.doubleValue() == 5.99);
assertTrue(priceTulip.doubleValue() == 1.99);
}
- Type the following code for the testItemCost method.
public void testItemCost()
throws Exception
{
MyShoppingCart cart = new MyShoppingCart();
double priceForTwo = cart.itemCost("Orchid", 2);
assertTrue(priceForTwo == 11.98);
}
- Save MyShoppingCartTest.java. From the file menu, choose File
> Save.
- Open the Test Perspective and create a test deployment. For an example, see
Creating a Test Deployment.
-
Run the test using your custom deployment.
- In the
Test Navigator of the
Test Perspective, right-click the MyShoppingCartTest test suite and choose Run
As > Run.... The Run configuration dialog appears.
- In the Configurations pane, select Test and then click New.
- In the left pane of the Run configuration (Select test to run), expand datapoolExample and choose the MyShoppingCartTest test suite.
- In the right pane of the Run configuration, select the deployment you created section.
- In the Test Logs tab, clear the Use defaults option and
select the datapoolExample project folder for the location.
- Click Run to launch the test.
- Double-click the MyShoppingCartTest test log, which appears in the
Test Navigator of the
Test Perspective. The MyShoppingCartTest test log appears. Select the Events tab to view the test details. You should see the following events: start of test suite, start of Loop_MyShoppingCart, test start, test verdict, test stop, second test start, second test verdict, second test stop, loop stop, test suite verdict, and test suite stop.
In this procedure, you create a simple datapool to store test data. Using the datapool editor, you define a datapool's structural
elements, including variables (columns), records (rows), and
equivalence classes (groups of related records).
- Create a CSV (comma delimited) file including your test data. Typically, you can export data in a spreadsheet application or database to CSV format. In this tutorial, however, you type the data in a text editor.
- Type the following in a text editor.
,ModelNumber::String,Description::String,Price::Double
flowers,F0001,Orchid,5.99
flowers,F0002,Tulip,1.99
flowers,F0003,Yellow Carnation,6.99
flowers,F0004,White Rose,9.99
flowers,F0005,Geraniums,4.99
- Save the file as flowerData.csv in a temporary external location.
-
In Eclipse, open the Test perspective.
- From the File menu, choose Window > Open Perspective >
Other.
- Select Test and click OK. The
Test Navigator of the
Test Perspective
appears.
-
In the
Test Navigator of the
Test Perspective, right-click a
project and select New > Test Element
...
The New Test Element dialog appears.
-
In the Wizards list box, expand the Test Assets folder and select Datapool.
-
Click Next. The New Datapool dialog appears.
-
Choose the datapool project folder and
datapool name. In the list of existing projects, select the
datapoolExample project. In the Name
field, type shoppingCartDatapool. Click Next to
continue and then Next again to open the CSV (comma delimited) file import page.
- Import the
CSV file flowerData.csv.
-
In the CSV File field, click Browse
and navigate to flowerData.csv.
-
Check the First row
contains variable names and suggested types
option. The first row of flowerData.csv contains column headings and types.
- Check the
First column contains
equivalence class names
option. Equivalence classes group related data. The first column of flowerData.csv specifies a single equivalence class called flowers.
-
Click Finish. If you specified initial datapool dimensions, which may conflict with the CSV file dimensions, a dialog appears. Click Yes to use the dimensions of the CSV file. Click No to use the specified initial dimensions (and possibly truncate the CSV file data).
The Datapool editor appears, showing the data contained in the flowers equivalence class.
For detailed information about creating a datapool, see
Creating a datapool.
In this procedure, you use the datapool API to replace hard-coded test values with
references to datapool columns.
Adding libraries required by the datapool APIThe datapool API requires various TPTP and Eclipse Modeling Framework (EMF) libraries. In this procedure, you add the libraries to your Java build path.
- Open the Java Perspective. From the file menu choose Window >
Open Perspective > Java.
- Open the datapoolExample project properties. In the Navigator or Package Explorer window, right-click the datapoolExample project folder and choose Properties. The Properties for datapoolExample dialog appears.
- Add the following library JARs to the Java build path of the datapoolExample project:
-
org.eclipse.tptp.platform.models_<version>/tptp-models.jar
-
org.eclipse.tptp.platform.models.hierarchy_<version>/tptp-models-hierarchy.jar
-
org.eclipse.emf.common_<version>.jar
-
org.eclipse.emf.ecore_<version>.jar
-
org.eclipse.emf.ecore.xmi_<version>.jar
-
org.eclipse.equinox.common_<version>.jar
where
<version>
is the current version of the library JAR.
Steps:
- In the left pane, select Java Build Path.
- In the right pane, select the Libraries tab.
- For each of the library JARs, complete the following steps:
- If the library JAR is on the Java build path of the datapoolExample project, continue to the next library JAR. Otherwise, continue to the next step.
- Click Add External JARs....
- Navigate to the directory containing the library JAR and select the JAR file. The library JAR is contained in the same directory as the dependencies under the TPTP Libraries or JUnit 3 classpath container for the datapoolExample project.
- Click Open.
- Click OK to close the Properties dialog.
Using the datapool APIIn this procedure, you modify the MyShoppingCartTest class and testMyShoppingCartConstructor method to utilize the
shoppingCartDatapool.
- If required, open the Java Perspective. From the file menu choose Window >
Open Perspective > Java.
- Open MyShoppingCartTest.java. In the Navigator, open the
datapoolExample project folder and double-click
MyShoppingCartTest.java. The MyShoppingCartTest.java contents
appear in the Java editor.
- Add the following import statements to the MyShoppingCartTest class.
import org.eclipse.hyades.execution.runtime.datapool.IDatapool;
import org.eclipse.hyades.execution.runtime.datapool.IDatapoolFactory;
import org.eclipse.hyades.execution.runtime.datapool.IDatapoolIterator;
import org.eclipse.hyades.models.common.datapool.impl.Common_DatapoolFactoryImpl;
- Declare an IDatapoolIterator class instance. This class instance will be initialized in your set up code and used in test methods.
public class MyShoppingCartTest extends HyadesTestCase {
IDatapoolIterator dpIterator;
//...
- Modify the setUp method to initialize your datapool. In a TPTP JUnit test environment, the setUp method provides a common fixture. You can use setUp to initialize common test variables. Note: specify your fully qualified workspace path in place of <workspace> in the java.io.File constructor.
protected void setUp() throws Exception {
// Initialize the datapool factory
IDatapoolFactory dpFactory = new Common_DatapoolFactoryImpl();
// Load the shoppingCartDatapool datapool
IDatapool datapool = dpFactory.load(
new java.io.File("<workspace>\\datapoolExample\\shoppingCartDatapool.datapool"),
false);
// Create an iterator to traverse the datapool
dpIterator = dpFactory.open(datapool,"org.eclipse.hyades.datapool.iterator.DatapoolIteratorSequentialPrivate");
// Initialize the datapool to traverse the first equivalence class.
dpIterator.dpInitialize(datapool,0);
}
Notes:
- The second parameter in the IDatapoolFactory load method indicates if the datapool instance is shared. If true the datapool cache is checked for an existing copy of the datapool. If false, the datapool is a private instance. Modifying a shared datapool instance may affect other users, and is only recommended for confined environments.
- If you want to programmatically edit a datapool import org.eclipse.hyades.edit.datapool.
- The second parameter in the IDatapoolFactory open method represents the type of iterator. For shared datapools use DatapoolIteratorSequentialPrivate.
- Modify the testMyShoppingCartConstructor method to utilize the
shoppingCartDatapool.
public void testMyShoppingCartConstructor()
throws Exception
{
MyShoppingCart cart = new MyShoppingCart();
// Traverse through datapool...
// Test if constructor initializes each flower record appropriately
while(!dpIterator.dpDone())
{
String Description = dpIterator.dpCurrent().getCell("Description").getStringValue();
double datapoolPrice = dpIterator.dpCurrent().getCell("Price").getDoubleValue();
// Test if the flower is accounted for in the application
Double applicationPrice;
assertNotNull(applicationPrice = (Double)cart.myFlowers.get(Description));
// Test that the application price equals the price in the datapool
assertTrue(applicationPrice.doubleValue() == datapoolPrice);
dpIterator.dpNext();
}
}
- Save MyShoppingCartTest.java. From the file menu, choose File
> Save.
- Add the datapool to your testArtifact.
- Open the Test Perspective. In the
Test Navigator of the
Test Perspective, double-click testDeployment to open it in the editor.
- In the Pairs tab, select testAsset and click Open...
- In the Test Assets tab, click Add.... Select the datapool and click OK. Save the asset.
- Run the test using your custom deployment.
- Open the Test Perspective. In the
Test Navigator of the
Test Perspective, right-click the MyShoppingCartTest test suite and choose Run
As > Run.... The Run configuration dialog appears.
- In the left pane of the Run configuration, choose the MyShoppingCartTest test suite.
- In the right pane of the Run configuration, select the deployment you created in the
Before You Begin section.
- In the Test Logs tab, clear the Use defaults option and
select the datapoolExample project folder for the location.
- Click Run to launch the test.
Notes:
- If the deployment you select contains any encrypted datapools, a dialog will be shown after you have launched the test to require the password for each encrypted datapool that contained in the deployment.
- Double-click the new MyShoppingCartTest test log, which appears in the
Test Navigator of the
Test Perspective. The MyShoppingCartTest test log appears. Select the Events tab to view the test result details.
Related tasks
Testing with JUnit
Providing tests with variable data
Creating a test deployment
|
|
|