Executing and Collecting Coverage for Unit Tests

You can include unit test results in the Jtest report by running your tests with the jtest and jtest-agent tasks, and the dedicated Unit Tests built-in test configuration:

  1. Ensure that the Jtest Plugin for Gradle is set up (Configuring the Jtest Plugin for Gradle).
  2. Execute Gradle tasks in the following order:
    - the jtest-agent task
    - the test (or build) task to ensure that unit tests are executed
    - the jtest task
      Your command line may resemble the following:

    gradle clean jtest-agent test jtest -Djtest.config="builtin://Unit Tests"

By default, Jtest collects coverage data for executed tests. To disable collecting coverage, enable the jtest.coverage.skip option (see Jtest Tasks Reference for Gradle for details).

If you are testing a multi-module project, Jtest, by default, collects coverage information for all subprojects of the root project. To collect coverage for selected subprojects, modify your command line to execute the Gradle test task for the subprojects you want to test. Your command line may resemble the following:

gradle clean jtest-agent subproject1:test subproject2:test jtest -Djtest.config="builtin://Unit Tests"

(info) If you skip a test by calling a method from the org.junit.Assume (JUnit 4) or org.junit.jupiter.api.Assumptions (JUnit 5) class, Jtest collects coverage for code lines executed before the method is called. This coverage information is not associated with any test in the coverage report.

Jtest collects test coverage for Gradle multi-module project parallel builds, enabled with the -parallel or --parallel option. For details, see https://docs.gradle.org/current/userguide/performance.html#parallel_execution. For example:

gradle -parallel clean jtest-agent test jtest -Djtest.config="builtin://Unit Tests"

Collecting Coverage for Android Projects 

Jtest allows collecting coverage from local unit tests for Android projects based on Gradle builds. Jtest supports unit tests written in Java and Kotlin. The coverage can be collected for a particular application variant (build type and product flavor). Only one application variant is supported per run. To collect coverage for more than one application variant, you must collect it separately for each variant.

To attach the Jtest Agent and collect the coverage, the following tasks must be executed:

jtestAgent[APPLICATION VARIANT NAME] - where APPLICATION VARIANT NAME is the name of the application variant for which coverage will be collected.

jtest[APPLICATION VARIANT NAME] - where APPLICATION VARIANT NAME is the name of the application variant for which the test results and collected coverage will be processed.

To list available tasks for an Android Gradle build, use the command:

gradlew tasks –I[JTEST_HOME/integration/gradle/init.gradle]

Example:

This example shows a command line that collects coverage from local unit tests for the “Debug” build type and a flavor named “Demo” (the application variant name is "DemoDebug").

gradlew compileDemoDebugUnitTestSources jtestAgentDemoDebug testDemoDebugUnitTest jtestDemoDebug –Djtest.config=”builtin://Unit Tests”

 Limitations:

  • Collecting coverage from instrumented tests (tests executed on target device emulated or physical) is not supported.
  • Collecting results from Kotlin tests and collecting coverage from Kotlin classes is not supported in the IDE; it is only supported via the CLI. 
  • Collecting and reporting coverage is supported for only one application variant at once.

Collecting Application Coverage

Jtest's coverage agent allows you to collect coverage data during manual or automated tests performed on a running application. See Application Coverage for information about collecting application coverage with Jtest.

  • No labels