Java test reporting

note
Using JUnit 5? If you use the JUnit 5 test framework no code is necessary to integrate, click here to view documentation for the Tesults JUnit 5 extension and ignore the instructions below.
note
Using JUnit 4? If you use the JUnit 4 test framework take a look at documentation for integrating with JUnit 4 and ignore the instructions below.
note
Using TestNG? If you use the TestNG test framework no code is necessary to integrate. Click here to view documentation for the Tesults TestNG Listener and ignore the instructions below.

Installation

The Tesults Java API Library is available from the JCenter and Maven Central repositories or as a JAR.

Option 1: Gradle

If you are using Gradle add this dependency snippet to your build.gradle file:

dependencies {
  compile 'com.tesults:tesults:1.0.3'
}

Also ensure you have the JCenter or Maven Central repository referenced in your build.gradle file.

Option 2: Maven

If you are using Maven add this dependency snippet to your pom.xml file:

<dependency>
  <groupId>com.tesults</groupId>
  <artifactId>tesults</artifactId>
  <version>1.0.3</version>
</dependency>

Option 3: JAR

Download here and reference in your project.

Configuration

Use one of the three methods described above to make the Tesults Java API available as a library and then import the com.tesults.tesults package in your code:

import com.tesults.tesults.*;

Usage

Upload results using the upload method in the Results class. This is the single point of contact between your code and Tesults.

Results.upload(data);

The upload method returns a Map indicating success or failure:

Map<String, Object> response = Results.upload(data);

// Response output:
System.out.println("success: " + response.get("success"));
System.out.println("message: " + response.get("message"));
System.out.println("warnings: " + ((List<String>) response.get("warnings")).size());
System.out.println("errors: " + ((List<String>) response.get("errors")).size());

The upload method returns a response of type Map<String, Object> that you can use to check whether the upload was a success.

  • Value for key "success" is a Boolean: true if results successfully uploaded, false otherwise.
  • Value for key "message" is a String: if success is false, check message to see why upload failed.
  • Value for key "warnings" is a List<String>, if size is not zero there may be issues with file uploads.
  • Value for key "errors" is a List<String>, if "success" is true then this will be empty.

The data param passed to upload is a Map<String, Object> containing your results data. Here is a complete example showing how to populate data with your build and test results and then complete upload to Tesults with a call to Results.upload(data):

Complete example:
// Required imports:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.tesults.tesults.*;  // Tesults Java API

// A list to hold your test cases.
List<Map<String,Object>> testCases = new ArrayList<Map<String, Object>>();

// Each test case is a Map<String,Object>. We use generic types rather than
// concrete helper classes so that if and when the Tesults services adds more fields
// you do not have to update the library.

// You would usually add test cases in a loop taking the results from the data objects
// in your build/test scripts. At a minimum you must add a name and result:

Map<String, Object> testCase1 = new HashMap<String, Object>();
testCase1.put("name", "Test 1");
testCase1.put("desc", "Test 1 description");
testCase1.put("suite", "Suite A");
testCase1.put("result", "pass"); // result value must be pass, fail or unknown

testCases.add(testCase1);

Map<String, Object> testCase2 = new HashMap<String, Object>();
testCase2.put("name", "Test 2");
testCase2.put("desc", "Test 2 description");
testCase2.put("suite", "Suite B");
testCase2.put("result", "pass");

// (Optional) Add start and end time for test:
// In this example, start is offset to 60 seconds earlier
// but it should be set to current time when the test starts
testCase2.put("start", java.lang.System.currentTimeMillis() - 60000);
testCase2.put("end", java.lang.System.currentTimeMillis());

// (Optional) For a paramaterized test case:
Map<String, String> params = new HashMap<String, String>();
params.put("param1", "value1");
params.put("param2", "value2");
testCase2.put("params", params);

// (Optional) Custom fields start with an underscore:
testCase2.put("_CustomField", "Custom field value");

testCases.add(testCase2);

Map<String, Object> testCase3 = new HashMap<String, Object>();
testCase3.put("name", "Test 3");
testCase3.put("desc", "Test 3 description");
testCase3.put("suite", "Suite A");
testCase3.put("result", "fail");
testCase3.put("reason", "Assert fail in line 203 of example.java.");

// (Optional) For uploading files:
List<String> files = new ArrayList<String>();
files.add("/Path/to/file/log.txt");
files.add("/Path/to/file/screencapture.png");
files.add("/Path/to/file/metrics.xls");
testCase3.put("files", files);

// (Optional) For providing test steps for a test case:
List<Map<String,Object>> steps = new ArrayList<Map<String, Object>>();
Map<String, Object> step1 = new HashMap<String, Object>();
step1.put("name", "Step 1");
step1.put("desc", "Step 1 description");
step1.put("result", "pass"); // can be pass, fail or unknown
step1.put("reason", "Failure reason if result is a fail");
steps.add(step1);

Map<String, Object> step2 = new HashMap<String, Object>();
step2.put("name", "Step 2");
step2.put("desc", "Step 2 description");
step2.put("result", "pass"); // pass, fail or unknown
step2.put("reason", "Failure reason if result is a fail");
steps.add(step2);

testCase3.put("steps", steps);

testCases.add(testCase3);

// Map<String, Object> to hold your test results data.
Map<String, Object> data = new HashMap<String, Object>();
data.put("target", "token");

Map<String, Object> results = new HashMap<String, Object>();
results.put("cases", testCases);
data.put("results", results);

// Upload
Map<String, Object> response = Results.upload(data);
System.out.println("success: " + response.get("success"));
System.out.println("message: " + response.get("message"));
System.out.println("warnings: " + ((List<String>) response.get("warnings")).size());
System.out.println("errors: " + ((List<String>) response.get("errors")).size());

The target value, 'token' above should be replaced with your Tesults target token. If you have lost your token you can regenerate one from the config menu.

Test case properties

This is a complete list of test case properties for reporting results. The required fields must have values otherwise upload will fail with an error message about missing fields.

PropertyRequiredDescription
name*Name of the test.
result*Result of the test. Must be one of: pass, fail, unknown. Set to 'pass' for a test that passed, 'fail' for a failure.
suiteSuite the test belongs to. This is a way to group tests.
descDescription of the test
reasonReason for the test failure. Leave this empty or do not include it if the test passed
paramsParameters of the test if it is a parameterized test.
filesFiles that belong to the test case, such as logs, screenshots, metrics and performance data.
stepsA list of test steps that constitute the actions of a test case.
startStart time of the test case in milliseconds from Unix epoch.
endEnd time of the test case in milliseconds from Unix epoch.
durationDuration of the test case running time in milliseconds. There is no need to provide this if start and end are provided, it will be calculated automatically by Tesults." : "Duration of the build time in milliseconds. There is no need to provide this if start and end are provided, it will be calculated automatically by Tesults.
rawResultReport a result to use with the result interpretation feature. This can give you finer control over how to report result status values beyond the three Tesults core result values of pass, fail and unknown.
_customReport any number of custom fields. To report custom fields add a field name starting with an underscore ( _ ) followed by the field name.

Build properties

To report build information simply add another case added to the cases array with suite set to [build]. This is a complete list of build properties for reporting results. The required fields must have values otherwise upload will fail with an error message about missing fields.

PropertyRequiredDescription
name*Name of the build, revision, version, or change list.
result*Result of the build. Must be one of: pass, fail, unknown. Use 'pass' for build success and 'fail' for build failure.
suite*Must be set to value '[build]', otherwise will be registered as a test case instead.
descDescription of the build or changes.
reasonReason for the build failure. Leave this empty or do not include it if the build succeeded.
paramsBuild parameters or inputs if there are any.
filesBuild files and artifacts such as logs.
startStart time of the build in milliseconds from Unix epoch.
endEnd time of the build in milliseconds from Unix epoch.
durationDuration of the build time in milliseconds. There is no need to provide this if start and end are provided, it will be calculated automatically by Tesults.
_customReport any number of custom fields. To report custom fields add a field name starting with an underscore ( _ ) followed by the field name.

Files generated by tests

The example above demonstrates how to upload files for each test case. In practice you would generate the array of file paths for each test case programatically.

To make this process simpler we suggest you write a helper function to extract files for each test case and this can be easily achieved by following a couple of simple conventions when testing.

1. Store all files in a temporary directory as your tests run. After Tesults upload is complete you can delete the temporary directory or overwrite it on the next test run.

2. Within this temporary directory create subdirectories for each test case so that files for each test case are easily mapped to a particular test case.

  • expanded temporary folder
    • expanded Test Suite A
      • expanded Test 1
        • test.log
        • screenshot.png
      • expanded Test 2
        • test.log
        • screenshot.png
    • expanded Test Suite B
      • expanded Test 3
        • metrics.csv
        • error.log
      • expanded Test 4
        • test.log

Then all your helper function needs to do is take the test name and/or suite as parameters and return an array of files for that particular test case.

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

final static String tempDir = "/files-temp-dir";

public static List<String> filesForTest (String suite, String name) {
  List<String> filePaths = new ArrayList<String>();

  Path path = (Path) Paths.get(tempDir, suite, name);
  String fullPath = path.toString();

  File dir = new File(fullPath);

  for (final File file : dir.listFiles()) {
    if (!file.getName().equals((".DS_Store"))) { // Exclude os files
      filePaths.add(file.getPath());
    }
  }

  return filePaths;
}

//testCase.put("files", filesForTest((String) testCase.get("suite"), (String) testCase.get("name")));

Caution: If uploading files the time taken to upload is entirely dependent on your network speed. Typical office upload speeds of 100 - 1000 Mbps should allow upload of even hundreds of files quite quickly, just a few seconds, but if you have slower access it may take hours. We recommend uploading a reasonable number of files for each test case. The upload method blocks at the end of a test run while uploading test results and files. When starting out test without files first to ensure everything is setup correctly.

Consolidating parallel test runs

If you execute multiple test runs in parallel or serially for the same build or release and results are submitted to Tesults within each run, separately, you will find that multiple test runs are generated on Tesults. This is because the default behavior on Tesults is to treat each results submission as a separate test run. This behavior can be changed from the configuration menu. Click 'Results Consolidation By Build' from the Configure Project menu to enable and disable consolidation by target. Enabling consolidation will mean that multiple test runs submitted with the same build name will be consolidated into a single test run.

Dynamically created test cases

If you dynamically create test cases, such as test cases with variable values, we recommend that the test suite and test case names themselves be static. Provide the variable data information in the test case description or other custom fields but try to keep the test suite and test name static. If you change your test suite or test name on every test run you will not benefit from a range of features Tesults has to offer including test case failure assignment and historical results analysis. You need not make your tests any less dynamic, variable values can still be reported within test case details.

Proxy servers

Does your corporate/office network run behind a proxy server? Contact us and we will supply you with a custom API Library for this case. Without this results will fail to upload to Tesults.

Have questions or need help? Contact us