From 26ed6a55485d26bb9b2af78c83e138538ea116f8 Mon Sep 17 00:00:00 2001 From: Ivan Kravets Date: Mon, 25 Apr 2022 13:23:33 +0300 Subject: [PATCH] Implement required setUp/tearDown functions for the latest Unity testing framework --- docs | 2 +- examples | 2 +- platformio/unittest/command.py | 5 +- platformio/unittest/runners/unity.py | 17 +++- tests/commands/test_test.py | 143 +++++++++++++++++++-------- 5 files changed, 121 insertions(+), 48 deletions(-) diff --git a/docs b/docs index ad578864..ec9d6ca6 160000 --- a/docs +++ b/docs @@ -1 +1 @@ -Subproject commit ad5788644cf754ce615082041badd711933ea807 +Subproject commit ec9d6ca64c129e473282c2c13b852fbd10ba886a diff --git a/examples b/examples index 18c0d444..a7b73dc2 160000 --- a/examples +++ b/examples @@ -1 +1 @@ -Subproject commit 18c0d44404215089c1d02d60a2a700bd5f7d1673 +Subproject commit a7b73dc2ff92c38a5c29991a218f3e15c75942f3 diff --git a/platformio/unittest/command.py b/platformio/unittest/command.py index 25f8f57a..3ca776fa 100644 --- a/platformio/unittest/command.py +++ b/platformio/unittest/command.py @@ -112,8 +112,9 @@ def unittest_cmd( # pylint: disable=too-many-arguments,too-many-locals,redefine if not verbose: click.echo("Verbose mode can be enabled via `-v, --verbose` option") - click.secho("Collected %d tests" % len(test_names), bold=True, nl=False) - click.echo(" (%s)" % ", ".join(test_names)) + click.secho("Collected %d tests" % len(test_names), bold=True, nl=not verbose) + if verbose: + click.echo(" (%s)" % ", ".join(test_names)) test_summary = TestSummary(os.path.basename(project_dir)) default_envs = config.default_envs() diff --git a/platformio/unittest/runners/unity.py b/platformio/unittest/runners/unity.py index 6e599704..fa929bb2 100644 --- a/platformio/unittest/runners/unity.py +++ b/platformio/unittest/runners/unity.py @@ -57,7 +57,7 @@ void unityOutputFlush(); void unityOutputComplete(); #define UNITY_OUTPUT_START() unityOutputStart((unsigned long) $baudrate) -#define UNITY_OUTPUT_CHAR(a) unityOutputChar(a) +#define UNITY_OUTPUT_CHAR(c) unityOutputChar(c) #define UNITY_OUTPUT_FLUSH() unityOutputFlush() #define UNITY_OUTPUT_COMPLETE() unityOutputComplete() @@ -207,14 +207,23 @@ void unityOutputComplete(void) { unittest_uart_end(); } def configure_build_env(self, env): env.Append(CPPDEFINES=["UNITY_INCLUDE_CONFIG_H"]) + if self.custom_unity_config_exists( + [env.subst(item) for item in (env.get("CPPPATH") or [])] + ): + return env env.Replace( UNITY_CONFIG_DIR=os.path.join("$BUILD_DIR", "unity_config"), BUILD_UNITY_CONFIG_DIR=os.path.join("$BUILD_DIR", "unity_config_build"), ) + env.Append(CPPPATH=["$UNITY_CONFIG_DIR"]) self.generate_unity_extras(env.subst("$UNITY_CONFIG_DIR")) - env.Append( - CPPPATH=["$UNITY_CONFIG_DIR"], - LIBS=[env.BuildLibrary("$BUILD_UNITY_CONFIG_DIR", "$UNITY_CONFIG_DIR")], + env.BuildSources("$BUILD_UNITY_CONFIG_DIR", "$UNITY_CONFIG_DIR") + return env + + @staticmethod + def custom_unity_config_exists(include_dirs): + return any( + os.path.isfile(os.path.join(d, "unity_config.h")) for d in include_dirs ) def generate_unity_extras(self, dst_dir): diff --git a/tests/commands/test_test.py b/tests/commands/test_test.py index fe53d159..3f34ef4a 100644 --- a/tests/commands/test_test.py +++ b/tests/commands/test_test.py @@ -126,6 +126,10 @@ void setUp(){ my_extra_fun(); } +void tearDown(void) { + // clean stuff up here +} + void dummy_test(void) { TEST_ASSERT_EQUAL(1, TEST_ONE); } @@ -146,46 +150,6 @@ int main() { assert "Disabled test suite" not in result.output -def test_unity_setup_teardown(clirunner, validate_cliresult, tmpdir): - project_dir = tmpdir.mkdir("project") - project_dir.join("platformio.ini").write( - """ -[env:native] -platform = native -""" - ) - test_dir = project_dir.mkdir("test") - test_dir.join("test_main.c").write( - """ -#include -#include - -void setUp(){ - printf("setUp called"); -} -void tearDown(){ - printf("tearDown called"); -} - -void dummy_test(void) { - TEST_ASSERT_EQUAL(1, 1); -} - -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 all(f in result.output for f in ("setUp called", "tearDown called")) - - def test_crashed_program(clirunner, tmpdir): project_dir = tmpdir.mkdir("project") project_dir.join("platformio.ini").write( @@ -230,6 +194,97 @@ int main(int argc, char *argv[]) { ) +def test_unity_setup_teardown(clirunner, validate_cliresult, tmpdir): + project_dir = tmpdir.mkdir("project") + project_dir.join("platformio.ini").write( + """ +[env:native] +platform = native +""" + ) + test_dir = project_dir.mkdir("test") + test_dir.join("test_main.c").write( + """ +#include +#include + +void setUp(){ + printf("setUp called"); +} +void tearDown(){ + printf("tearDown called"); +} + +void dummy_test(void) { + TEST_ASSERT_EQUAL(1, 1); +} + +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 all(f in result.output for f in ("setUp called", "tearDown called")) + + +def test_unity_custom_config(clirunner, validate_cliresult, tmpdir): + project_dir = tmpdir.mkdir("project") + project_dir.join("platformio.ini").write( + """ +[env:native] +platform = native +""" + ) + test_dir = project_dir.mkdir("test") + test_dir.join("unity_config.h").write( + """ +#include + +#define CUSTOM_UNITY_CONFIG + +#define UNITY_OUTPUT_CHAR(c) putchar(c) +#define UNITY_OUTPUT_FLUSH() fflush(stdout) +""" + ) + test_dir.join("test_main.c").write( + """ +#include +#include + +void setUp(){ +#ifdef CUSTOM_UNITY_CONFIG + printf("Found custom unity_config.h\\n"); +#endif +} +void tearDown(){ +} + +void dummy_test(void) { + TEST_ASSERT_EQUAL(1, 1); +} + +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 all(f in result.output for f in ("Found custom unity_config", "dummy_test")) + + def test_legacy_unity_custom_transport(clirunner, validate_cliresult, tmpdir): project_dir = tmpdir.mkdir("project") project_dir.join("platformio.ini").write( @@ -247,6 +302,14 @@ test_transport = custom """ #include +void setUp(void) { + // set stuff up here +} + +void tearDown(void) { + // clean stuff up here +} + void dummy_test(void) { TEST_ASSERT_EQUAL(1, 1); }