Skip to content

Commit

Permalink
twister: coverage: Add coverage data to twister.json
Browse files Browse the repository at this point in the history
New Twister option `--coverage-report` allow to select what coverage
data to include into the `twister.json` file for its further analysis
together with the test meta-data and test results.

The choices are to select either the coverage summary, or the detailed
branch coverage, ztest, or all the data.
The coverage run status value is present with any non-default choice.

Depending on `--coverage-split` and `--disable-coverage-aggregation`
options the coverage data is attached either to its test suite object
or/and at the report's top level for the test plan execution scope.

Currently this mode is fully supported for `--coverage-tool gcovr`
only, as it is based on the GCOVR json reports.

Signed-off-by: Dmitrii Golovanov <[email protected]>
  • Loading branch information
golowanow committed Dec 15, 2023
1 parent e95826a commit 35a1bbe
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
9 changes: 9 additions & 0 deletions scripts/pylib/twister/twisterlib/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,15 @@ def add_parse_arguments(parser = None):
when coverage is enabled. Might run together with the aggregation mode.
Default: %(default)s""")

parser.add_argument("--coverage-report", nargs="+", default=None,
choices=['all','report','ztest','summary'],
help="""Includes coverage data into the `twister.json` report as 'coverage' object:
with `--coverage-split` it will be specific for each test suite;
the aggregated coverage of all executed test suites will be at the top-level.
Currently this mode is fully supported for `--coverage-tool gcovr` only.
The coverage run 'status' value is also present with any non-default choice.
Default: %(default)s""")

parser.add_argument("--test-config", action="store", default=os.path.join(ZEPHYR_BASE, "tests", "test_config.yaml"),
help="Path to file with plans and test configurations.")

Expand Down
23 changes: 22 additions & 1 deletion scripts/pylib/twister/twisterlib/reports.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python3
# vim: set syntax=python ts=4 :
#
# Copyright (c) 2018 Intel Corporation
# Copyright (c) 2018-2023 Intel Corporation
# SPDX-License-Identifier: Apache-2.0

import os
Expand All @@ -26,6 +26,8 @@ def __init__(self, plan, env) -> None:
self.env = env
self.timestamp = datetime.now().isoformat()
self.outdir = os.path.abspath(env.options.outdir)
self.coverage_status = None
self.coverage = None

@staticmethod
def process_log(log_file):
Expand Down Expand Up @@ -351,9 +353,28 @@ def json_report(self, filename, version="NA", platform=None):
if instance.recording is not None:
suite['recording'] = instance.recording

if self.env.options.coverage_report and instance.coverage_status is not None:
suite['coverage'] = {'status': instance.coverage_status}
do_all = 'all' in self.env.options.coverage_report
for k,v in instance.coverage.items():
if do_all or k in self.env.options.coverage_report:
logger.debug(f"Include '{instance.name}' coverage.{k} from '{v}'")
with open(v, "rt") as json_file:
suite['coverage'][k] = json.load(json_file)

suites.append(suite)

report["testsuites"] = suites

if self.env.options.coverage_report and self.coverage_status is not None:
report['coverage'] = {'status': self.coverage_status}
do_all = 'all' in self.env.options.coverage_report
for k,v in self.coverage.items():
if do_all or k in self.env.options.coverage_report:
logger.debug(f"Include aggregated coverage.{k} from '{v}'")
with open(v, "rt") as json_file:
report['coverage'][k] = json.load(json_file)

with open(filename, "wt") as json_file:
json.dump(report, json_file, indent=4, separators=(',',':'))

Expand Down

0 comments on commit 35a1bbe

Please sign in to comment.