diff --git a/HISTORY.rst b/HISTORY.rst index a905f858..aac8e1c8 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -20,6 +20,7 @@ test-driven methodologies, and modern toolchains for unrivaled success. 6.1.14 (2024-??-??) ~~~~~~~~~~~~~~~~~~~ +* Introduced the ``--json-output`` option to the `pio test `__ command, enabling users to generate test results in the JSON format * Broadened version support for the ``pyelftools`` dependency, enabling compatibility with lower versions and facilitating integration with a wider range of third-party tools (`issue #4834 `_) * Addressed an issue where passing a relative path (``--project-dir``) to the `pio project init `__ command resulted in an error (`issue #4847 `_) * Resolved an issue related to the relative package path in the `pio pkg publish `__ command diff --git a/docs b/docs index 37c10d2f..5ada9ce2 160000 --- a/docs +++ b/docs @@ -1 +1 @@ -Subproject commit 37c10d2f72c18d1b3f84db661e96fa7732d2c7ad +Subproject commit 5ada9ce23361b0fd672932574fcea2ad9badf443 diff --git a/platformio/test/cli.py b/platformio/test/cli.py index 748580c8..9760545a 100644 --- a/platformio/test/cli.py +++ b/platformio/test/cli.py @@ -14,6 +14,7 @@ import os import shutil +import subprocess import click @@ -79,6 +80,7 @@ from platformio.test.runners.factory import TestRunnerFactory help="A program argument (multiple are allowed)", ) @click.option("--list-tests", is_flag=True) +@click.option("--json-output", is_flag=True) @click.option("--json-output-path", type=click.Path()) @click.option("--junit-output-path", type=click.Path()) @click.option( @@ -105,6 +107,7 @@ def cli( # pylint: disable=too-many-arguments,too-many-locals,redefined-builtin monitor_dtr, program_args, list_tests, + json_output, json_output_path, junit_output_path, verbose, @@ -156,6 +159,7 @@ def cli( # pylint: disable=too-many-arguments,too-many-locals,redefined-builtin stdout_report.generate(verbose=verbose or list_tests) for output_format, output_path in [ + ("json", subprocess.STDOUT if json_output else None), ("json", json_output_path), ("junit", junit_output_path), ]: diff --git a/platformio/test/reports/json.py b/platformio/test/reports/json.py index 3d3c6f6e..8d834b38 100644 --- a/platformio/test/reports/json.py +++ b/platformio/test/reports/json.py @@ -15,6 +15,7 @@ import datetime import json import os +import subprocess import click @@ -24,6 +25,9 @@ from platformio.test.result import TestStatus class JsonTestReport(TestReportBase): def generate(self, output_path, verbose=False): + if output_path == subprocess.STDOUT: + return click.echo("\n\n" + json.dumps(self.to_json())) + if os.path.isdir(output_path): output_path = os.path.join( output_path, @@ -40,6 +44,8 @@ class JsonTestReport(TestReportBase): if verbose: click.secho(f"Saved JSON report to the {output_path}", fg="green") + return True + def to_json(self): result = dict( version="1.0",