diff --git a/HISTORY.rst b/HISTORY.rst index c20b419a..b538fa03 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -44,9 +44,10 @@ Please check `Migration guide from 5.x to 6.0 `_ engine and documentation - - Added support for `Test Hierarchies `_ (`issue #4135 `_) - - Added a new "test" `build configuration `__ + - New `Unit Testing `_ solution and documentation + - New `Test Hierarchies `_ (`issue #4135 `_) + - New `Custom Testing Framework `_ + - New "test" `build configuration `__ - Generate reports in JUnit and JSON formats using the `pio test --output-format `__ option (`issue #2891 `_) - 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/docs b/docs index 854e0f82..f1316bde 160000 --- a/docs +++ b/docs @@ -1 +1 @@ -Subproject commit 854e0f82024bfa78b3f23594b756d033ae5f6af8 +Subproject commit f1316bdef0f1edf79e2af21ad706e26de9ede405 diff --git a/platformio/commands/run/processor.py b/platformio/commands/run/processor.py index e927bed1..dbeea442 100644 --- a/platformio/commands/run/processor.py +++ b/platformio/commands/run/processor.py @@ -68,7 +68,10 @@ class EnvironmentProcessor(object): if "clean" not in build_targets: install_project_env_dependencies( self.name, - {"project_targets": build_targets}, + { + "project_targets": build_targets, + "piotest_running_name": build_vars.get("piotest_running_name"), + }, ) result = PlatformFactory.new(self.options["platform"], autoinstall=True).run( diff --git a/platformio/package/commands/install.py b/platformio/package/commands/install.py index 2833aa11..9bbe9505 100644 --- a/platformio/package/commands/install.py +++ b/platformio/package/commands/install.py @@ -216,7 +216,9 @@ def _install_project_env_libraries(project_env, options): lib_deps = config.get(f"env:{project_env}", "lib_deps") if "__test" in options.get("project_targets", []): - test_runner = TestRunnerFactory.new(TestSuite(project_env, "*"), config) + test_runner = TestRunnerFactory.new( + TestSuite(project_env, options.get("piotest_running_name", "*")), config + ) lib_deps.extend(test_runner.EXTRA_LIB_DEPS or []) for library in lib_deps: diff --git a/platformio/test/runners/factory.py b/platformio/test/runners/factory.py index 325b7489..ef101d48 100644 --- a/platformio/test/runners/factory.py +++ b/platformio/test/runners/factory.py @@ -41,14 +41,24 @@ class TestRunnerFactory(object): module_name = f"platformio.test.runners.{test_framework}" runner_cls = None if test_framework == "custom": - custom_runner_path = os.path.join( - project_config.get("platformio", "test_dir"), "custom_runner.py" - ) + test_dir = project_config.get("platformio", "test_dir") + custom_runner_path = os.path.join(test_dir, "custom_test_runner.py") + test_name = test_suite.test_name if test_suite.test_name != "*" else None + while test_name: + if os.path.isfile( + os.path.join(test_dir, test_name, "custom_test_runner.py") + ): + custom_runner_path = os.path.join( + test_dir, test_name, "custom_test_runner.py" + ) + break + test_name = os.path.dirname(test_name) # parent dir + try: mod = load_python_module(module_name, custom_runner_path) - except ImportError: + except (FileNotFoundError, ImportError): raise UserSideException( - "Could not find custom unit testing runner " + "Could not find custom test runner " f"by this path -> {custom_runner_path}" ) else: diff --git a/tests/commands/test_test.py b/tests/commands/test_test.py index a68a20fe..43a7e3a1 100644 --- a/tests/commands/test_test.py +++ b/tests/commands/test_test.py @@ -62,13 +62,14 @@ def test_calculator_example(tmp_path: Path): assert junit_failed_testcase.find("failure").get("message") == "Expected 32 Was 33" -def test_nested_suites(clirunner, validate_cliresult, tmp_path: Path): +def test_group_and_custom_runner(clirunner, validate_cliresult, tmp_path: Path): project_dir = tmp_path / "project" project_dir.mkdir() (project_dir / "platformio.ini").write_text( """ [env:native] platform = native +test_framework = custom """ ) test_dir = project_dir / "test" @@ -108,8 +109,23 @@ void my_extra_fun(void) { """ ) + # test group + test_group = test_dir / "group" + test_group.mkdir(parents=True) + (test_group / "custom_test_runner.py").write_text( + """ +import click + +from platformio.test.runners.unity import UnityTestRunner + +class CustomTestRunner(UnityTestRunner): + def teardown(self): + click.echo("CustomTestRunner::TearDown called") +""" + ) + # test suite - test_suite_dir = test_dir / "set" / "test_nested" + test_suite_dir = test_group / "test_nested" test_include_dir = test_suite_dir / "include" test_include_dir.mkdir(parents=True) (test_include_dir / "my_nested.h").write_text( @@ -148,6 +164,7 @@ int main() { ) validate_cliresult(result) assert "Called from my_extra_fun" in result.output + assert "CustomTestRunner::TearDown called" in result.output assert "Disabled test suite" not in result.output