mirror of
https://github.com/platformio/platformio-core.git
synced 2025-07-31 18:44:27 +02:00
Add support for GoogleTest testing and mocking framework // Resolve #3572
This commit is contained in:
@@ -46,8 +46,9 @@ Please check `Migration guide from 5.x to 6.0 <https://docs.platformio.org/en/la
|
|||||||
* **Unit Testing**
|
* **Unit Testing**
|
||||||
|
|
||||||
- Refactored from scratch `Unit Testing <https://docs.platformio.org/en/latest/advanced/unit-testing/index.html>`_ solution and its documentation
|
- Refactored from scratch `Unit Testing <https://docs.platformio.org/en/latest/advanced/unit-testing/index.html>`_ solution and its documentation
|
||||||
- New: `Test Hierarchies <https://docs.platformio.org/en/latest/advanced/unit-testing/structure.html>`_ (`issue #4135 <https://github.com/platformio/platformio-core/issues/4135>`_)
|
- New: `Test Hierarchy <https://docs.platformio.org/en/latest/advanced/unit-testing/structure.html>`_ (`issue #4135 <https://github.com/platformio/platformio-core/issues/4135>`_)
|
||||||
- New: `doctest <https://docs.platformio.org/en/latest/advanced/unit-testing/frameworks/doctest.html>`__ testing framework (`issue #4240 <https://github.com/platformio/platformio-core/issues/4240>`_)
|
- New: `Doctest <https://docs.platformio.org/en/latest/advanced/unit-testing/frameworks/doctest.html>`__ testing framework (`issue #4240 <https://github.com/platformio/platformio-core/issues/4240>`_)
|
||||||
|
- New: `GoogleTest <https://docs.platformio.org/en/latest/advanced/unit-testing/frameworks/googletest.html>`__ testing and mocking framework (`issue #3572 <https://github.com/platformio/platformio-core/issues/3572>`_)
|
||||||
- New: `Semihosting <https://docs.platformio.org/en/latest/advanced/unit-testing/semihosting.html>`__ (`issue #3516 <https://github.com/platformio/platformio-core/issues/3516>`_)
|
- New: `Semihosting <https://docs.platformio.org/en/latest/advanced/unit-testing/semihosting.html>`__ (`issue #3516 <https://github.com/platformio/platformio-core/issues/3516>`_)
|
||||||
- New: Hardware `Simulators <https://docs.platformio.org/en/latest/advanced/unit-testing/simulators/index.html>`__ for Unit Testing (QEMU, Renode, SimAVR, and custom solutions)
|
- New: Hardware `Simulators <https://docs.platformio.org/en/latest/advanced/unit-testing/simulators/index.html>`__ for Unit Testing (QEMU, Renode, SimAVR, and custom solutions)
|
||||||
- New: ``test`` `build configuration <https://docs.platformio.org/en/latest/projectconf/build_configurations.html>`__
|
- New: ``test`` `build configuration <https://docs.platformio.org/en/latest/projectconf/build_configurations.html>`__
|
||||||
|
2
docs
2
docs
Submodule docs updated: 3bbe54ac3b...db1960e263
2
examples
2
examples
Submodule examples updated: cb4befb56b...042c58bb71
@@ -652,7 +652,7 @@ ProjectOptions = OrderedDict(
|
|||||||
group="test",
|
group="test",
|
||||||
name="test_framework",
|
name="test_framework",
|
||||||
description="A unit testing framework",
|
description="A unit testing framework",
|
||||||
type=click.Choice(["doctest", "unity", "custom"]),
|
type=click.Choice(["doctest", "googletest", "unity", "custom"]),
|
||||||
default="unity",
|
default="unity",
|
||||||
),
|
),
|
||||||
ConfigEnvOption(
|
ConfigEnvOption(
|
||||||
|
@@ -30,11 +30,11 @@ class TestStatus(enum.Enum):
|
|||||||
@classmethod
|
@classmethod
|
||||||
def from_string(cls, value: str):
|
def from_string(cls, value: str):
|
||||||
value = value.lower()
|
value = value.lower()
|
||||||
if value.startswith("fail"):
|
if value.startswith(("failed", "fail")):
|
||||||
return cls.FAILED
|
return cls.FAILED
|
||||||
if value.startswith(("pass", "success")):
|
if value.startswith(("passed", "pass", "success", "ok")):
|
||||||
return cls.PASSED
|
return cls.PASSED
|
||||||
if value.startswith(("ignore", "skip")):
|
if value.startswith(("skipped", "skip", "ignore", "ignored")):
|
||||||
return cls.SKIPPED
|
return cls.SKIPPED
|
||||||
if value.startswith("WARNING"):
|
if value.startswith("WARNING"):
|
||||||
return cls.WARNED
|
return cls.WARNED
|
||||||
|
115
platformio/test/runners/googletest.py
Normal file
115
platformio/test/runners/googletest.py
Normal file
@@ -0,0 +1,115 @@
|
|||||||
|
# Copyright (c) 2014-present PlatformIO <contact@platformio.org>
|
||||||
|
#
|
||||||
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
# you may not use this file except in compliance with the License.
|
||||||
|
# You may obtain a copy of the License at
|
||||||
|
#
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
# See the License for the specific language governing permissions and
|
||||||
|
# limitations under the License.
|
||||||
|
|
||||||
|
import os
|
||||||
|
import re
|
||||||
|
|
||||||
|
import click
|
||||||
|
|
||||||
|
from platformio.test.result import TestCase, TestCaseSource, TestStatus
|
||||||
|
from platformio.test.runners.base import TestRunnerBase
|
||||||
|
|
||||||
|
|
||||||
|
class DoctestTestCaseParser:
|
||||||
|
|
||||||
|
# Examples:
|
||||||
|
# [ RUN ] FooTest.Bar
|
||||||
|
# ...
|
||||||
|
# [ FAILED ] FooTest.Bar (0 ms)
|
||||||
|
STATUS__NAME_RE = r"^\[\s+(?P<status>[A-Z]+)\s+\]\s+(?P<name>[^\(\s]+)"
|
||||||
|
|
||||||
|
# Examples:
|
||||||
|
# [ RUN ] FooTest.Bar
|
||||||
|
# test/test_gtest/test_main.cpp:26: Failure
|
||||||
|
# Y:\core\examples\unit-testing\googletest\test\test_gtest\test_main.cpp:26: Failure
|
||||||
|
SOURCE_MESSAGE_RE = r"^(?P<source_file>.+):(?P<source_line>\d+):(?P<message>.*)$"
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self._tmp_tc = None
|
||||||
|
|
||||||
|
def parse(self, line):
|
||||||
|
if self._tmp_tc:
|
||||||
|
self._tmp_tc.stdout += line
|
||||||
|
return self._parse_test_case(line)
|
||||||
|
|
||||||
|
def _parse_test_case(self, line):
|
||||||
|
status, name = self._parse_status_and_name(line)
|
||||||
|
if status == "RUN":
|
||||||
|
self._tmp_tc = TestCase(name, TestStatus.PASSED, stdout=line)
|
||||||
|
return None
|
||||||
|
if not status or not self._tmp_tc:
|
||||||
|
return None
|
||||||
|
source, message = self._parse_source_and_message(self._tmp_tc.stdout)
|
||||||
|
test_case = TestCase(
|
||||||
|
name=self._tmp_tc.name,
|
||||||
|
status=TestStatus.from_string(status),
|
||||||
|
message=message,
|
||||||
|
source=source,
|
||||||
|
stdout=self._tmp_tc.stdout.strip(),
|
||||||
|
)
|
||||||
|
self._tmp_tc = None
|
||||||
|
return test_case
|
||||||
|
|
||||||
|
def _parse_status_and_name(self, line):
|
||||||
|
result = (None, None)
|
||||||
|
line = line.strip()
|
||||||
|
if not line.startswith("["):
|
||||||
|
return result
|
||||||
|
match = re.search(self.STATUS__NAME_RE, line)
|
||||||
|
if not match:
|
||||||
|
return result
|
||||||
|
return match.group("status"), match.group("name")
|
||||||
|
|
||||||
|
def _parse_source_and_message(self, stdout):
|
||||||
|
for line in stdout.split("\n"):
|
||||||
|
line = line.strip()
|
||||||
|
if not line:
|
||||||
|
continue
|
||||||
|
match = re.search(self.SOURCE_MESSAGE_RE, line)
|
||||||
|
if not match:
|
||||||
|
continue
|
||||||
|
return (
|
||||||
|
TestCaseSource(
|
||||||
|
match.group("source_file"), int(match.group("source_line"))
|
||||||
|
),
|
||||||
|
(match.group("message") or "").strip() or None,
|
||||||
|
)
|
||||||
|
return (None, None)
|
||||||
|
|
||||||
|
|
||||||
|
class GoogletestTestRunner(TestRunnerBase):
|
||||||
|
|
||||||
|
EXTRA_LIB_DEPS = ["google/googletest@^1.11.0"]
|
||||||
|
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
super().__init__(*args, **kwargs)
|
||||||
|
self._tc_parser = DoctestTestCaseParser()
|
||||||
|
os.environ["GTEST_COLOR"] = "no" # disable ANSI symbols
|
||||||
|
|
||||||
|
def configure_build_env(self, env):
|
||||||
|
if self.platform.is_embedded():
|
||||||
|
return
|
||||||
|
env.Append(CXXFLAGS=["-std=c++11"])
|
||||||
|
|
||||||
|
def on_testing_line_output(self, line):
|
||||||
|
if self.options.verbose:
|
||||||
|
click.echo(line, nl=False)
|
||||||
|
|
||||||
|
test_case = self._tc_parser.parse(line)
|
||||||
|
if test_case:
|
||||||
|
click.echo(test_case.humanize())
|
||||||
|
self.test_suite.add_case(test_case)
|
||||||
|
|
||||||
|
if "Global test environment tear-down" in line:
|
||||||
|
self.test_suite.on_finish()
|
@@ -436,6 +436,10 @@ void unittest_uart_end(){}
|
|||||||
validate_cliresult(result)
|
validate_cliresult(result)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.skipif(
|
||||||
|
sys.platform == "win32" and os.environ.get("GITHUB_ACTIONS") == "true",
|
||||||
|
reason="skip Github Actions on Windows (MinGW issue)",
|
||||||
|
)
|
||||||
def test_doctest_framework(clirunner, tmp_path: Path):
|
def test_doctest_framework(clirunner, tmp_path: Path):
|
||||||
project_dir = tmp_path / "project"
|
project_dir = tmp_path / "project"
|
||||||
project_dir.mkdir()
|
project_dir.mkdir()
|
||||||
@@ -562,3 +566,57 @@ int main(int argc, char **argv)
|
|||||||
json_report = load_json(str(json_output_path))
|
json_report = load_json(str(json_output_path))
|
||||||
assert json_report["testcase_nums"] == 1
|
assert json_report["testcase_nums"] == 1
|
||||||
assert json_report["failure_nums"] == 1
|
assert json_report["failure_nums"] == 1
|
||||||
|
|
||||||
|
|
||||||
|
def test_googletest_framework(clirunner, tmp_path: Path):
|
||||||
|
project_dir = os.path.join("examples", "unit-testing", "googletest")
|
||||||
|
junit_output_path = tmp_path / "junit.xml"
|
||||||
|
result = clirunner.invoke(
|
||||||
|
pio_test_cmd,
|
||||||
|
[
|
||||||
|
"-d",
|
||||||
|
project_dir,
|
||||||
|
"-e",
|
||||||
|
"native",
|
||||||
|
"--output-format=junit",
|
||||||
|
"--output-path",
|
||||||
|
str(junit_output_path),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
assert result.exit_code != 0
|
||||||
|
# test JUnit output
|
||||||
|
junit_testsuites = ET.parse(junit_output_path).getroot()
|
||||||
|
assert int(junit_testsuites.get("tests")) == 4
|
||||||
|
assert int(junit_testsuites.get("errors")) == 0
|
||||||
|
assert int(junit_testsuites.get("failures")) == 1
|
||||||
|
assert len(junit_testsuites.findall("testsuite")) == 4
|
||||||
|
junit_failed_testcase = junit_testsuites.find(".//testcase[@name='FooTest.Bar']")
|
||||||
|
assert junit_failed_testcase.get("status") == "FAILED"
|
||||||
|
assert "test_main.cpp" in junit_failed_testcase.get("file")
|
||||||
|
assert junit_failed_testcase.get("line") == "26"
|
||||||
|
assert junit_failed_testcase.find("failure").get("message") == "Failure"
|
||||||
|
assert "Expected equality" in junit_failed_testcase.find("failure").text
|
||||||
|
|
||||||
|
# test program arguments
|
||||||
|
json_output_path = tmp_path / "report.json"
|
||||||
|
result = clirunner.invoke(
|
||||||
|
pio_test_cmd,
|
||||||
|
[
|
||||||
|
"-d",
|
||||||
|
project_dir,
|
||||||
|
"-e",
|
||||||
|
"native",
|
||||||
|
"--output-format=json",
|
||||||
|
"--output-path",
|
||||||
|
str(json_output_path),
|
||||||
|
"-a",
|
||||||
|
"--gtest_filter=-FooTest.Bar",
|
||||||
|
],
|
||||||
|
)
|
||||||
|
assert result.exit_code == 0
|
||||||
|
# test JSON
|
||||||
|
json_report = load_json(str(json_output_path))
|
||||||
|
assert json_report["testcase_nums"] == 3
|
||||||
|
assert json_report["failure_nums"] == 0
|
||||||
|
assert json_report["skipped_nums"] == 1
|
||||||
|
assert len(json_report["test_suites"]) == 4
|
||||||
|
Reference in New Issue
Block a user