From e3533dcb019fd15c3d2d14645810a1d259acc945 Mon Sep 17 00:00:00 2001 From: Ivan Kravets Date: Fri, 22 Apr 2022 15:19:12 +0300 Subject: [PATCH] Added support for test hierarchies (nested test suites) // Resolve #4135 --- HISTORY.rst | 1 + platformio/unittest/command.py | 7 ++- tests/commands/test_test.py | 100 ++++++++++++++++++++++++++++----- 3 files changed, 90 insertions(+), 18 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index a46cb461..2e30d5ce 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -44,6 +44,7 @@ Please check `Migration guide from 5.x to 6.0 `_) - Provide more information when the native program crashed on a host (errored with a negative return code) (`issue #3429 `_) - Fixed an issue when command line parameters ("--ignore", "--filter") do not override values defined in the |PIOCONF| (`issue #3845 `_) diff --git a/platformio/unittest/command.py b/platformio/unittest/command.py index 0a21b9df..50202f0a 100644 --- a/platformio/unittest/command.py +++ b/platformio/unittest/command.py @@ -171,9 +171,10 @@ def get_test_names(config): if not os.path.isdir(test_dir): raise TestDirNotExistsError(test_dir) names = [] - for item in sorted(os.listdir(test_dir)): - if os.path.isdir(os.path.join(test_dir, item)): - names.append(item) + for root, _, __ in os.walk(test_dir): + if not os.path.basename(root).startswith("test_"): + continue + names.append(os.path.relpath(root, test_dir)) if not names: names = ["*"] return names diff --git a/tests/commands/test_test.py b/tests/commands/test_test.py index e17d6391..dd8ef501 100644 --- a/tests/commands/test_test.py +++ b/tests/commands/test_test.py @@ -13,13 +13,13 @@ # limitations under the License. import os -import subprocess +from pathlib import Path from platformio import proc from platformio.unittest.command import unittest_cmd -def test_unity_calculator(): +def test_calculator_example(): result = proc.exec_command( [ "platformio", @@ -36,20 +36,90 @@ def test_unity_calculator(): s in (result["err"] + result["out"]) for s in ("PASSED", "FAILED") ), result["out"] - result = subprocess.run( # pylint: disable=subprocess-run-check - [ - "platformio", - "test", - "-d", - os.path.join("examples", "unit-testing", "calculator"), - "-e", - "native", - ], - capture_output=True, - text=True, + +def test_nested_suites(clirunner, validate_cliresult, tmp_path: Path): + project_dir = tmp_path / "project" + project_dir.mkdir() + (project_dir / "platformio.ini").write_text( + """ +[env:native] +platform = native +""" ) - assert result.returncode != 0 - assert all(s in str(result) for s in ("PASSED", "FAILED")) + test_dir = project_dir / "test" + + # non-test folder, does not start with "test_" + disabled_dir = test_dir / "disabled" + disabled_dir.mkdir(parents=True) + (disabled_dir / "main.c").write_text( + """ +#include + +int main() { + printf("Disabled test suite\\n") +} + """ + ) + + # root + (test_dir / "my_extra.h").write_text( + """ +#ifndef MY_EXTRA_H +#define MY_EXTRA_H + +#include + +void my_extra_fun(void); +#endif +""" + ) + (test_dir / "my_extra.c").write_text( + """ +#include "my_extra.h" + +void my_extra_fun(void) { + printf("Called from my_extra_fun\\n"); +} +""" + ) + + # test suite + test_suite_dir = test_dir / "set" / "test_nested" + test_include_dir = test_suite_dir / "include" + test_include_dir.mkdir(parents=True) + (test_include_dir / "my_nested.h").write_text( + """ +#define TEST_ONE 1 +""" + ) + (test_suite_dir / "main.c").write_text( + """ +#include +#include +#include + +void setUp(){ + my_extra_fun(); +} + +void dummy_test(void) { + TEST_ASSERT_EQUAL(1, TEST_ONE); +} + +int main() { + UNITY_BEGIN(); + RUN_TEST(dummy_test); + UNITY_END(); +} + """ + ) + result = clirunner.invoke( + unittest_cmd, + ["-d", str(project_dir), "-e", "native"], + ) + validate_cliresult(result) + assert "Called from my_extra_fun" in result.output + assert "Disabled test suite" not in result.output def test_unity_setup_teardown(clirunner, validate_cliresult, tmpdir):