The next step of this tutorial will show how to gather and prepare data for the report. The report we are going to create will show a summary of all test runs for all test cases in a test suite together with a detailed description of all executed test steps, listed per test case per test run. To provide the data for the report the API described in the Klaros API documentation can be used to retrieve the required data.
The following picture shows the steps to perform to create a KlarosScript class.
First a class implementing the KlarosScript interface must be created. Right click on the src folder (1) in your project and select New -> Class.
In the new dialog enter a name for your script class (2)
Then click on the KlarosScript
in the new dialog to provide the KlarosScript interface for the class.
Then press the button to close the interface dialog.
Finally press the
button (4). After a short while the class should be created and Eclipse should present you the following view.Before entering any code, the import statements must be provided.
import de.verit.klaros.scripting.*; import de.verit.klaros.core.model.*; import java.util.*;
The code to be executed must be entered into the execute method of the KlarosScript class. To provide a certain level of flexibility, the test suite to generate the report for should be passed as a parameter to the KlarosScript class. We will see later in this tutorial how this works in detail. It is achieved by the following line of code:
String suiteName = (String)context.getParameterValue("suiteName");
Next the database is accessed to retrieve the data for the report. The following snippet shows how to access the database. This code selects all entries from the KlarosTestSuite table whose ids matches the test suite id passed to the KlarosScript and that are present in the currently active project. The result is returned as a list, though only one single entry is expected.
String suiteQuery = "select g from KlarosTestSuite g where g.name='" + suiteName + "'"; if (context.getActiveProject() != null) { suiteQuery += " and g.configuration.name='"; suiteQuery += context.getActiveProject().getName(); suiteQuery += "'"; } List testSuites = context.executeQuery(suiteQuery);
To avoid any errors when executing the KlarosScript the following code is contained in an if-clause if no matching test suite was found.
if (!testSuites.isEmpty()) {
testSuite = (IKlarosTestSuite) testSuites.get(0);
...
}
Add the test suite to the context so it can later be retrieved from the template.
testSuite = (IKlarosTestSuite) testSuites.get(0);
context.add("testSuite", testSuite);
Now the preparations for the pie chart in the report can begin. The report should present a pie chart showing the amount of test case results in the states error, skipped, failure and success. Therefore a list of KlarosTestCaseResult is required for each result type. This code is also placed inside the if-clause.
List<KlarosTestCaseResult> error = new ArrayList<>(); List<KlarosTestCaseResult> failure = new ArrayList<>(); List<KlarosTestCaseResult> success = new ArrayList<>(); List<KlarosTestCaseResult> skipped = new ArrayList<>();
Now the lists get filled. First we step through the list of test cases belonging to the test suite.
for (KlarosTestCase klarosTestCase : testSuite.getTestCases()) { ... }
Inside this loop we iterate over the test results for each test case. This can be more than one since a test case can have several test runs. The following code must be placed in the for loop from the step before.
for (KlarosTestCaseResult testResult: klarosTestCase.getResults()) { ... }
We now have the test results at hands and can distribute them to the four lists defined earlier, depending on the state of the test result. This code must be placed inside the second for loop defined one step before.
if (testResult.isError()) error.add(testResult); else if (testResult.isFailure()) failure.add(testResult); else if (testResult.isPassed()) success.add(testResult); else if (testResult.isSkipped()) skipped.add(testResult);
We are almost done. The last step is to store the four lists containing the KlarosTestCaseResults in the context to access it later from the template. This code is placed inside the outermost if-clause and outside of the two for loops.
context.add("error", error); context.add("failure", failure); context.add("success", success); context.add("skipped", skipped);
The complete KlarosScript class can be found in the archive here.