Add unit testing framework (#26948)

- Add a framework to build and execute unit tests for Marlin.
- Enable unit test execution as part of PR checks.

---------

Co-authored-by: Costas Basdekis <costas.basdekis@gmail.com>
Co-authored-by: Scott Lahteine <thinkyhead@users.noreply.github.com>
This commit is contained in:
Jason Smith 2024-04-13 12:06:08 -07:00 committed by GitHub
parent cf7c86d581
commit d10861e478
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
21 changed files with 711 additions and 112 deletions

View file

@ -0,0 +1,59 @@
#
# collect-code-tests.py
# Convenience script to collect all code tests. Used by env:linux_native_test in native.ini.
#
import pioutil
if pioutil.is_pio_build():
import os, re
Import("env")
Import("projenv")
os.environ['PATH'] = f"./buildroot/bin/:./buildroot/tests/:{os.environ['PATH']}"
def collect_test_suites():
"""Get all the test suites"""
from pathlib import Path
return sorted(list(Path("./test").glob("*.ini")))
def register_test_suites():
"""Register all the test suites"""
targets = []
test_suites = collect_test_suites()
for path in test_suites:
name = re.sub(r'^\d+-|\.ini$', '', path.name)
targets += [name];
env.AddCustomTarget(
name = f"marlin_{name}",
dependencies = None,
actions = [
f"echo ====== Configuring for marlin_{name} ======",
"restore_configs",
f"cp -f {path} ./Marlin/config.ini",
"python ./buildroot/share/PlatformIO/scripts/configuration.py",
f"platformio test -e linux_native_test -f {name}",
"restore_configs",
],
title = "Marlin: {}".format(name.lower().title().replace("_", " ")),
description = (
f"Run a Marlin test suite, with the appropriate configuration, "
f"that sits in {path}"
)
)
env.AddCustomTarget(
name = "test-marlin",
dependencies = None,
actions = [
f"platformio run -t marlin_{name} -e linux_native_test"
for name in targets
],
title = "Marlin: Test all code test suites",
description = (
f"Run all Marlin code test suites ({len(targets)} found)."
),
)
register_test_suites()

View file

@ -71,7 +71,9 @@ if pioutil.is_pio_build():
config = env.GetProjectConfig()
result = check_envs("env:"+build_env, board_envs, config)
if not result:
# Make sure board is compatible with the build environment. Skip for _test,
# since the board is manipulated as each unit test is executed.
if not result and build_env != "linux_native_test":
err = "Error: Build environment '%s' is incompatible with %s. Use one of these environments: %s" % \
( build_env, motherboard, ", ".join([ e[4:] for e in board_envs if e.startswith("env:") ]) )
raise SystemExit(err)