Added support for test hierarchies (nested test suites) // Resolve #4135

This commit is contained in:
Ivan Kravets
2022-04-22 15:19:12 +03:00
parent 8edb5ffe20
commit e3533dcb01
3 changed files with 90 additions and 18 deletions

View File

@ -44,6 +44,7 @@ Please check `Migration guide from 5.x to 6.0 <https://docs.platformio.org/en/la
* **Unit Testing** * **Unit Testing**
- Added support for test hierarchies (nested test suites) (`issue #4135 <https://github.com/platformio/platformio-core/issues/4135>`_)
- Provide more information when the native program crashed on a host (errored with a negative return code) (`issue #3429 <https://github.com/platformio/platformio-core/issues/3429>`_) - Provide more information when the native program crashed on a host (errored with a negative return code) (`issue #3429 <https://github.com/platformio/platformio-core/issues/3429>`_)
- Fixed an issue when command line parameters ("--ignore", "--filter") do not override values defined in the |PIOCONF| (`issue #3845 <https://github.com/platformio/platformio-core/issues/3845>`_) - Fixed an issue when command line parameters ("--ignore", "--filter") do not override values defined in the |PIOCONF| (`issue #3845 <https://github.com/platformio/platformio-core/issues/3845>`_)

View File

@ -171,9 +171,10 @@ def get_test_names(config):
if not os.path.isdir(test_dir): if not os.path.isdir(test_dir):
raise TestDirNotExistsError(test_dir) raise TestDirNotExistsError(test_dir)
names = [] names = []
for item in sorted(os.listdir(test_dir)): for root, _, __ in os.walk(test_dir):
if os.path.isdir(os.path.join(test_dir, item)): if not os.path.basename(root).startswith("test_"):
names.append(item) continue
names.append(os.path.relpath(root, test_dir))
if not names: if not names:
names = ["*"] names = ["*"]
return names return names

View File

@ -13,13 +13,13 @@
# limitations under the License. # limitations under the License.
import os import os
import subprocess from pathlib import Path
from platformio import proc from platformio import proc
from platformio.unittest.command import unittest_cmd from platformio.unittest.command import unittest_cmd
def test_unity_calculator(): def test_calculator_example():
result = proc.exec_command( result = proc.exec_command(
[ [
"platformio", "platformio",
@ -36,20 +36,90 @@ def test_unity_calculator():
s in (result["err"] + result["out"]) for s in ("PASSED", "FAILED") s in (result["err"] + result["out"]) for s in ("PASSED", "FAILED")
), result["out"] ), result["out"]
result = subprocess.run( # pylint: disable=subprocess-run-check
[ def test_nested_suites(clirunner, validate_cliresult, tmp_path: Path):
"platformio", project_dir = tmp_path / "project"
"test", project_dir.mkdir()
"-d", (project_dir / "platformio.ini").write_text(
os.path.join("examples", "unit-testing", "calculator"), """
"-e", [env:native]
"native", platform = native
], """
capture_output=True,
text=True,
) )
assert result.returncode != 0 test_dir = project_dir / "test"
assert all(s in str(result) for s in ("PASSED", "FAILED"))
# 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 <stdio.h>
int main() {
printf("Disabled test suite\\n")
}
"""
)
# root
(test_dir / "my_extra.h").write_text(
"""
#ifndef MY_EXTRA_H
#define MY_EXTRA_H
#include <stdio.h>
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 <unity.h>
#include <my_extra.h>
#include <include/my_nested.h>
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): def test_unity_setup_teardown(clirunner, validate_cliresult, tmpdir):