CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-scoverage--scalac-scoverage-plugin

Comprehensive code coverage tool for Scala providing statement and branch coverage through compiler plugin instrumentation and report generation

Pending
Overview
Eval results
Files

xml-reports.mddocs/

XML Report Generation

The ScoverageXmlWriter generates structured XML coverage reports in the scoverage format for programmatic consumption, CI/CD integration, and archival purposes. The XML format provides complete coverage data including statement details, method information, and hierarchical package/class organization.

Core API

ScoverageXmlWriter Class

class ScoverageXmlWriter(
  sourceDirectories: Seq[File],
  outputDir: File,
  debug: Boolean,
  sourceEncoding: Option[String]
) extends BaseReportWriter(sourceDirectories, outputDir, sourceEncoding) {
  def write(coverage: Coverage): Unit
}

Constructor Parameters:

  • sourceDirectories: Sequence of source directories for relative path calculation
  • outputDir: Directory where XML report will be generated
  • debug: Whether to include debug information in the XML output
  • sourceEncoding: Optional character encoding for reading source files

Methods:

  • write(coverage: Coverage): Unit - Generates XML report file

Alternative Constructor

// Single source directory constructor
class ScoverageXmlWriter(
  sourceDir: File,
  outputDir: File,
  debug: Boolean,
  sourceEncoding: Option[String]
)

Usage Examples

Basic XML Report Generation

import java.io.File
import scoverage.reporter.ScoverageXmlWriter
import scoverage.domain.Coverage

val sourceDirectories = Seq(new File("src/main/scala"))
val outputDir = new File("target/scoverage-report")
val debug = false
val writer = new ScoverageXmlWriter(sourceDirectories, outputDir, debug, Some("UTF-8"))

val coverage: Coverage = loadCoverageData()
writer.write(coverage)

Debug Mode XML Generation

import java.io.File
import scoverage.reporter.ScoverageXmlWriter

// Debug mode includes additional statement details like symbol names and tree names
val writer = new ScoverageXmlWriter(
  sourceDirectories = Seq(new File("src/main/scala")),
  outputDir = new File("target/scoverage-report"),
  debug = true,
  sourceEncoding = Some("UTF-8")
)

val coverage: Coverage = loadCoverageData()
writer.write(coverage)

CI/CD Integration Example

// Generate XML report for automated processing
val xmlWriter = new ScoverageXmlWriter(
  sourceDirectories = projectSourceDirs,
  outputDir = buildOutputDir,
  debug = false, // Minimal output for CI processing
  sourceEncoding = Some("UTF-8")
)

xmlWriter.write(coverage)

// The generated scoverage.xml can then be parsed by CI tools
val reportFile = new File(buildOutputDir, "scoverage.xml")
// Process with CI/coverage tools...

Generated XML Structure

Standard Mode Output

The XML report includes the following hierarchical structure:

<scoverage statement-count="1250" 
           statements-invoked="1100"
           statement-rate="88.00"
           branch-rate="82.50"
           version="1.0"
           timestamp="1609459200000">
  <packages>
    <package name="com.example.myapp" 
             statement-count="500"
             statements-invoked="450"
             statement-rate="90.00">
      <classes>
        <class name="com.example.myapp.MyClass"
               filename="MyClass.scala"
               statement-count="50"
               statements-invoked="45"
               statement-rate="90.00"
               branch-rate="85.00">
          <methods>
            <method name="myMethod"
                    statement-count="10"
                    statements-invoked="9"
                    statement-rate="90.00"
                    branch-rate="80.00">
              <statements>
                <statement package="com.example.myapp"
                          class="MyClass"
                          class-type="Class"
                          full-class-name="com.example.myapp.MyClass"
                          source="MyClass.scala"
                          method="myMethod"
                          start="150"
                          end="175"
                          line="10"
                          branch="false"
                          invocation-count="5"
                          ignored="false"/>
              </statements>
            </method>
          </methods>
        </class>
      </classes>
    </package>
  </packages>
</scoverage>

Debug Mode Output

When debug = true, statements include additional debugging information:

<statement package="com.example.myapp"
          class="MyClass"
          class-type="Class"
          full-class-name="com.example.myapp.MyClass"
          source="MyClass.scala"
          method="myMethod"
          start="150"
          end="175"
          line="10"
          symbol="scala.Predef.println"
          tree="Apply"
          branch="false"
          invocation-count="5"
          ignored="false">
  println("Hello World")
</statement>

Output Files

File Naming

  • Standard mode: scoverage.xml
  • Debug mode: scoverage-debug.xml

File Location

Reports are written to the specified outputDir directory.

XML Schema Details

Root Element Attributes

  • statement-count: Total number of statements in the codebase
  • statements-invoked: Number of statements that were executed
  • statement-rate: Statement coverage percentage (formatted to 2 decimal places)
  • branch-rate: Branch coverage percentage (formatted to 2 decimal places)
  • version: Scoverage XML format version
  • timestamp: Report generation timestamp (milliseconds since epoch)

Package Element Attributes

  • name: Package name (fully qualified)
  • statement-count: Statements in this package
  • statements-invoked: Invoked statements in this package
  • statement-rate: Package-level statement coverage percentage

Class Element Attributes

  • name: Fully qualified class name
  • filename: Relative path to source file
  • statement-count: Statements in this class
  • statements-invoked: Invoked statements in this class
  • statement-rate: Class-level statement coverage percentage
  • branch-rate: Class-level branch coverage percentage

Method Element Attributes

  • name: Method name with parameter signature
  • statement-count: Statements in this method
  • statements-invoked: Invoked statements in this method
  • statement-rate: Method-level statement coverage percentage
  • branch-rate: Method-level branch coverage percentage

Statement Element Attributes

  • package: Package containing the statement
  • class: Simple class name
  • class-type: Type of class (Class, Object, or Trait)
  • full-class-name: Fully qualified class name
  • source: Relative path to source file
  • method: Method containing the statement
  • start: Character offset start position
  • end: Character offset end position
  • line: Line number in source file
  • branch: Whether this statement represents a branch point
  • invocation-count: Number of times statement was executed
  • ignored: Whether statement was excluded from coverage

Debug-Only Statement Attributes

  • symbol: Scala symbol name (e.g., "scala.Predef.println")
  • tree: Scala compiler tree type (e.g., "Apply", "Select")

Integration with Build Tools

SBT Integration

// Generate XML report after coverage collection
val coverage = loadCoverageData()
val xmlWriter = new ScoverageXmlWriter(
  sourceDirectories = (Compile / scalaSource).value :: Nil,
  outputDir = crossTarget.value,
  debug = false,
  sourceEncoding = Some("UTF-8")
)
xmlWriter.write(coverage)

Gradle Integration

// Using Array constructor for Gradle compatibility
val sourceArray: Array[File] = sourceDirectories.toArray
val xmlWriter = new ScoverageXmlWriter(sourceArray, outputDir, debug, encoding)
xmlWriter.write(coverage)

Error Handling

Common Issues:

  • IOException: File writing permissions or disk space issues
  • RuntimeException: Invalid source directory paths or canonical path resolution failures
  • IllegalArgumentException: Invalid coverage data or missing statement information

Best Practices:

  • Ensure output directory exists and is writable
  • Verify all source directories are accessible
  • Use debug mode only when needed as it increases file size significantly
  • Consider XML file size for large codebases (debug mode can be very large)

Install with Tessl CLI

npx tessl i tessl/maven-org-scoverage--scalac-scoverage-plugin

docs

aggregation.md

cobertura-reports.md

coverage-model.md

html-reports.md

index.md

io-utils.md

plugin.md

runtime.md

serialization.md

xml-reports.md

tile.json