In the next step, you will see how to prepare your data for your report. The report in this tutorial shows a summary of all test runs for all test cases of a test suite. Furthermore, it presents a description of all executed test steps per test run, sorted by test case. In the Klaros API documentation. you will find an overview of the available data model.
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 define the KlarosScript interface for the class.
Then click to close the 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. The test suite to generate the report for will 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 match the test suite id passed to the KlarosScript and that are present in the currently active project. The result is returned as a list, yet we will only expect a single entry to be returned.
// Build the query using the parameter String suiteQuery = "select suite from KlarosTestSuite suite where suite.name='" + suiteName + "'"; if (context.getActiveProject() != null) { suiteQuery += " and suite.configuration.name='"; suiteQuery += context.getActiveProject().getName(); suiteQuery += "'"; } List testSuites = context.executeQuery(suiteQuery);
To avoid any errors when executing the KlarosScript, the following code is added for the case that no matching test suite has been found.
if (!testSuites.isEmpty()) {
testSuite = testSuites.get(0);
...
}
Now we add the test-suite to the context so that the template engine can access it later.
testSuite = testSuites.get(0);
context.add("testSuite", testSuite);
The preparations for the pie chart in the report can now 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 suite results belonging to the test suite.
for (KlarosTestSuiteResult testSuiteResult : testSuite.getResults()) { ... }
Within the test-suite results loop, the test results of each test case are being iterated over. The following code is inserted into the for loop created in the previous step.
for (KlarosTestCaseResult testCaseResult : testSuiteResult.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 (testCaseResult.isError()) error.add(testResult); else if (testCaseResult.isFailure()) failure.add(testResult); else if (testCaseResult.isPassed()) success.add(testResult); else if (testCaseResult.isSkipped()) skipped.add(testResult);
We are almost done. The last step is to store the four lists containing the test results in the context to access them later from the template. This code is placed inside the outermost if-clause and outside the two for loops.
context.add("error", error); context.add("failure", failure); context.add("success", success); context.add("skipped", skipped);
The complete Class can be found in the archive here.