Class CoverageApiClient

java.lang.Object
com.parasoft.coverage.integration.api.CoverageApiClient

public final class CoverageApiClient extends Object
User-facing client for directly reporting coverage session and test lifecycle events.

Most users should prefer the JUnit integration modules. Use this client only when a test is launched manually, such as from a main method or a custom test harness.

Typical standalone usage:


 CoverageApiClient client = CoverageApiClient.createFromSettings();
 String sessionId = client.startSession();
 CoverageTestContext context = client.startTest("ExampleTest", "runsFromMain");

 try {
     // Execute the code under test here. Send context.getCurrentTestOperatorIdHeader()
     // as HTTP headers when calling an application under test.
     client.stopTest("ExampleTest", "runsFromMain", context, CoverageTestResult.PASS, null);
 } catch (RuntimeException e) {
     client.stopTest("ExampleTest", "runsFromMain", context, CoverageTestResult.FAIL, e.getMessage());
     throw e;
 } finally {
     client.stopSession();
     client.publishResults(sessionId, null, null, null);
 }
 
  • Method Details

    • createFromSettings

      public static CoverageApiClient createFromSettings()
      Creates a client from coverage-integration.properties and system properties.
      Returns:
      a coverage API client
    • create

      public static CoverageApiClient create(String ctpBaseUrl, Long environmentId, String userId, String sessionTag)
      Creates a client for directly reporting coverage events.
      Parameters:
      ctpBaseUrl - CTP base URL
      environmentId - CTP environment identifier
      userId - user identifier to send to CTP
      sessionTag - optional session tag
      Returns:
      a coverage API client
    • startSession

      public String startSession()
      Starts a coverage session.
      Returns:
      the coverage session identifier returned by CTP, or null when no session identifier is available
    • startTest

      public CoverageTestContext startTest(String test, String testCase)
      Starts coverage tracking for a test.
      Parameters:
      test - test identifier, usually the test class name
      testCase - test case identifier, usually the test method name
      Returns:
      context returned from the CTP /test/start API
    • stopTest

      public void stopTest(String test, String testCase, CoverageTestContext testContext, CoverageTestResult result, String message)
      Stops coverage tracking for a test.
      Parameters:
      test - test identifier, usually the test class name
      testCase - test case identifier, usually the test method name
      testContext - context returned by startTest(String, String)
      result - final test result
      message - optional result message, such as a failure summary
    • stopSession

      public void stopSession()
      Stops the current coverage session.
    • publishResults

      public void publishResults(String sessionId, String testConfig, String userId, String toolName)
      Publishes coverage results for a finished session.
      Parameters:
      sessionId - coverage session identifier returned by startSession()
      testConfig - optional test configuration name
      userId - optional user identifier
      toolName - optional tool name