For using AludraTest, each test class must
A minimal test case would be:
import org.aludratest.AludraTestCase; import org.aludratest.testcase.Test; public class MyTestClass extends AludraTestCase { @Test public void myTestMethod() { logInfo("myTestMethod was called"); } }
Often, certain functionality has to be invoked before (set-up) or after (tear-down) test execution. This can be achieved by putting relevant code into particular methods. Such methods have the following requirements:
Any test class may have several methods marked with @Before or @After, also inherited methods are executed.
If a test class has several test methods, each @Before method is called before each test method and each @After method after each test method.
Example:
import org.aludratest.testcase.After; import org.aludratest.testcase.AludraTestCase; import org.aludratest.testcase.Before; import org.aludratest.testcase.Test; public class MyTestClass extends AludraTestCase { @Before public void setUp() { System.out.println("setUp()"); } @After public void tearDown() { System.out.println("tearDown()"); } @Test public void myTestMethod() { logInfo("myTestMethod was called"); } }
Test methods can have parameters to receive test data from files, e.g. an Excel sheet. For this purpose the annotation org.aludratest.testcase.data.Source can be used.
There are two different ways to apply the annotation: To a method or to method parameters.
When applied to a method, each column of the data source is mapped to the primitive-typed method parameter of the same index:
import org.aludratest.testcase.AludraTestCase; import org.aludratest.testcase.data.Source; import org.aludratest.testcase.Test; @Parallel public class MyTestClass extends AludraTestCase { @Test @Source(uri="testSource.xls", segment="sheet1") public void testActivityLogic(Date date, int value) { logInfo("test data: " + date + ", " + value); } }
Alternatively a data dource can be applied to a method parameter of JavaBean type. Then, data source column names are mapped to JavaBean properties. Even recursive JavaBean graphs can be constructed from a data source like an Excel sheet:
import org.aludratest.testcase.AludraTestCase; import org.aludratest.testcase.data.Source; import org.aludratest.testcase.Test; @Parallel public class MyTestClass extends AludraTestCase { @Test public void testActivityLogic(@Source(uri="testSource.xls", segment="sheet1") TestData data) { logInfo("test data: " + data); } }
A sample Excel sheet content:
name child.name parent child1 parent child2
The TestData class may be defined like this:
public class TestData { private String name; private Child child; public String getName() { return this.name; } public void setName(String name) { this.name = name; } public Child getChild() { return this.child; } public void setChild(Child child) { this.child = child; } }
with the Child class:
public class Child { private String name; public String getName() { return this.name; } public void setName(String name) { this.name = name; } }
Remember that the data classes must comply to JavaBeans conventions:
On the lowest level, a test method may be annotated with the annotation org.aludratest.testcase.Parallel to allow for concurrent test execution or with org.aludratest.testcase.Sequential to forbid it. If no such annotation is applied, concurrency characteristics are inherited from the test class or an outer test suite (or suite hierarchy). Each of the elements, suite, class and test method support these concurrency annotations. If no annotation was specified at all, it is assumed that concurrent execution of all tests is allowed.
Example:
import org.aludratest.testcase.AludraTestCase; import org.aludratest.testcase.Parallel; import org.aludratest.testcase.data.Source; import org.aludratest.testcase.Test; @Parallel public class MyTestClass extends AludraTestCase { @Test @Source(uri="testSource.xls", segment="sheet1") public void testActivityLogic(TestData data) { logInfo("test data: " + data); } }
A test suite is defined as an empty Java classes with a org.aludratest.Suite annotation which lists the suite components. Each suite component may be a simple test class or a suite itself. Thus, suite structures can be nested arbitrarily deeply.
Example:
import org.aludratest.junit.AludraTestJUnitSuite; @AludraTestJUnitSuite({ MyTestClass.class, MyOtherTestClass.class }) public class MyTestSuite { }
As mentioned before, suite classes can be annotated with org.aludratest.testcase.Parallel or org.aludratest.testcase.Sequential to control execution concurrency of their components:
import org.aludratest.Parallel; import org.aludratest.Suite; @Suite({ MyTestClass.class, ActivityLogic2.class }) @Parallel public class MyTestSuite { }
In order to start tests in Eclipse, a generic 'Eclipse run configuration' needs to be defined. This is a one-time-effort, after which you can execute any test with this generic configuration.
org.aludratest.app.AludraSuiteRunner
${resource_path}
Now the test class is executed. Please note that the test class must be the active editor in Eclipse when you click on "Run".
You can reuse this single run configuration to run any individual test: Select the test class in the Package Explorer, then click the triangle on the right of the "Run" button and click "AludraTest" in the appearing list.
Testcases can be executed locally with the use of JUnit functions in Eclipse. Follow the guideline below:
package com.acme.mytests.junit; import org.aludratest.junit.AludraTestJUnitSuite; import org.junit.runner.RunWith; @RunWith(AludraTestJUnitSuite.class) public class AludraJUnitTest { }
-Dsuite=${resource_path}
Now you can reuse this single run configuration to run any individual test: Select the test class in the Package Explorer, then click the triangle on the right of the "Run" button and click "AludraTest" in the appearing list.
TODO
AludraTest has a complex configuration engine. Most settings have a default value, so you do not have to configure anything to run your first tests. But if you plan to e.g. run web application tests using Selenium, you will definitely have to configure something.
For general information about the configuation engine, see Service Configuration.
For specific information about the configuration options for AludraTest or components of AludraTest, see AludraTest Services.
Imagine a DataClass model with the classes Country, State and City, interrelated with Java collections:
The model above is implemented with the following classes:
public class CountryData extends Data { private String id; private String name; private List<StateData> states = new ArrayList<StateData>(); public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public List<StateData> getStates() { return states; } public void setStates(List<StateData> states) { this.states = states; } }
public class StateData extends Data { private String id; private String name; private List<CityData > cities = new ArrayList<CityData >(); public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public List<CityData > getCities() { return cities; } public void setCities(List<CityData > cities) { this.cities = cities; } }
public class CityData extends Data { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
Data of such a recursive collection mapping can be represented in Excel documents in the following manner:
As an example one can map the following hierarchy
Germany (Country) +- Bayern (State) | +- München (City) | +- Regensburg (City) +- Hessen (State) +- Kassel (City) +- Wiesbaden (City) Italy (Country) +- Veneto (State) | +- Verona (City) | +- Venezia (City) +- Lomabardia (State) +- Milano (City) +- Bergamo (City)
using this Excel document structure (having all the tabs in the same Excel document):
When consuming this data in an AludraTest method like this:
@Test public void testInbound(@Source(uri = "country.ent.xls", segment = "countries") CountryData country) { System.out.println(country.getName()); }
The test method is executed twice, once with country Germany, once with country Italy having them wired with all their states and cities.
When certain data sets of an Excel sheet require further examination, one can temporarily configure the test method to skip the first n rows, applying the annotation org.aludratest.annotations.test.Offset to the test method, for example for skipping the first 5 data rows:
@Test @Offset(5) public void test(@Source(uri = "myfile.ent.xls", segment = "mytab") MyData data) { ... }
AludraTest provides a feature to generate Excel documents based on a test method's signature and annotations. There is a GUI and a command line version that allows you to create an Eclipse run configuration for generating Excel documents:
The Excel Generation GUI is tarted by launching the main class org.aludratest.app.excelwizard.ExcelWizard. It scans the current project folder for (already compiled) AludraTest test cases and displays them in a list.
Using a filter text, the user can filter the test cases by substring:
When selecting a test case from the list, the 'Test Method' dropdown box is updated to display all test methods that have @Source annotations.
The user can then select one and click 'Create Excel Sheet'. The wizard will then create an empty Excel sheets that contains a 'config' tab and data tabs that reflect the structure of the annotated method parameters:
For generating Excel documents in Eclipse, first create a "Java Application" run configuration that uses the project which contains the test class and set the main class to org.aludratest.app.excelwizard.CLIExcelCreator.
On second tab "Arguments", in the field 'Program Arguments' enter ${java_type_name}.
In order to execute the generator, select the test class in the Eclipse package explorer and start the new Run Configuration.