pysysjava.coverage

Writer that collects and reports on Java code coverage, using the JaCoCo library.

JavaCoverageWriter

class pysysjava.coverage.JavaCoverageWriter(logfile=None, **kwargs)[source]

Bases: pysys.writer.testoutput.CollectTestOutputWriter

Writer that collects that coverage data in a single directory and writes coverage XML and HTML reports during runner cleanup.

Requires the “JaCoCo” code coverage library.

To enable this, run with -XcodeCoverage (or -XjavaCoverage) and configure the destDir plugin property in pysysproject.xml (e.g. to __coverage_java.${outDirName}).

When enabled, the getCoverageJVMArgs method can be used to generate the JVM arguments needed to collect coverage data (for example, pysysjava.javaplugin.JavaPlugin.startJava calls this method). You must configure the writer with the alias javaCoverageWriter so that the plugin knows to use it when starting Java processes.

Note that this writer uses output=file JaCoCo agent option which dumps coverage information to a file when the Java process exits. This means background processes that are still running when the PySys test finishes (and get killed by PySys) will not generate any coverage - so try to ensure clean termination where possible. (An alternative approach would be to implement a Java TCP server using the JaCoCo API which each Java process connects to, but this writer doesn’t go that far).

If coverage is generated, the directory containing all coverage files is published as an artifact named “coverageDestDir”. Optionally an archive of this directory can be generated by setting the destArchive property (see pysys.writer.testoutput.CollectTestOutputWriter), and published as “JavaCoverageArchive”.

The following properties can be set in the project configuration for this writer:

jacocoDir = ''

This mandatory writer property configures the directory containing the JaCoCo jar files org.jacoco.agent*.jar and org.jacoco.cli*.jar.

agentArgs = ''

A comma-separated string of additional agent parameters to pass to the JaCoCo agent to control gathering of coverage data.

For example “includes=myorg.myserver.*,excludes=myorg.myserver.tests.*” can be used to include/exclude packages from coverage data. See the JaCoCo documentation for more information about the Java Agent.

classpath = ''

The classpath containing the application classes which should be included in the code coverage report. If this property is not set then no HTML or XML report will be generated.

Glob * expressions can be used. For full details of how this plugin handles the classpath string see pysysjava.javaplugin.JavaPlugin.toClasspathList().

sourceDirs = ''

A semi-colon separated list of directories containing the .java source files used to compile the classpath classes.

This is required for the HTML report to show the line-by-line source file coverage.

reportArgs = ''

A space-separated string of additional command line arguments to pass to the JaCoCo report command line.

For example “–encoding utf-8”.

getCoverageJVMArgs(owner, stdouterr=None)[source]

Get the JVM arguments needed to add Java coverage to a new Java process, or empty if this coverage writer is not currently enabled.

This is called by pysysjava.javaplugin.JavaPlugin.startJava (and any custom methods that start JVMs) to add coverage collection.

Parameters
  • owner (pysys.basetest.BaseTest) – The BaseTest/BaseRunner owner of the process that is to be started. This is used to keep track of coverage names already allocated, and str(owner) is used as sessionId metadata.

  • stdouterr (str) – The name of the stdouterr for the process being measured (or None if not available), used to contribute to the coverage filename.