Skip to content

LCOV parsing

Cédric Belin edited this page Apr 10, 2025 · 7 revisions

The Report.Parse() static method parses a LCOV coverage report provided as string, and creates a Report instance giving detailed information about this coverage report:

using Belin.Lcov;
using System;
using System.IO;
using System.Text.Json;

try {
  var report = Report.Parse(File.ReadAllText("/path/to/lcov.info"));
  Console.WriteLine($"The coverage report contains {report.SourceFiles.Count} source files:");
  Console.WriteLine(JsonSerializer.Serialize(report, new JsonSerializerOptions {
    PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
    WriteIndented = true
  }));
}
catch (FormatException e) {
  Console.Error.WriteLine(e.Message);
}

Note

A FormatException is thrown if any error occurred while parsing the coverage report.
You can also use the convenient Report.TryParse() method to avoid the exception handling.

Converting the Report instance to JSON format will return a map like this:

{
  "testName": "Example",
  "sourceFiles": [
    {
      "path": "/home/cedx/lcov.net/fixture.cs",
      "branches": {
        "found": 0,
        "hit": 0,
        "data": []
      },
      "functions": {
        "found": 1,
        "hit": 1,
        "data": [
          {"functionName": "main", "lineNumber": 4, "executionCount": 2}
        ]
      },
      "lines": {
        "found": 2,
        "hit": 2,
        "data": [
          {"lineNumber": 6, "executionCount": 2, "checksum": "PF4Rz2r7RTliO9u6bZ7h6g"},
          {"lineNumber": 9, "executionCount": 2, "checksum": "y7GE3Y4FyXCeXcrtqgSVzw"}
        ]
      }
    }
  ]
}
Clone this wiki locally