Skip to content

Commit 2e50353

Browse files
committed
add mbed unittest
1 parent 59c57db commit 2e50353

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ This document covers the installation and usage of Mbed CLI.
2525
2. [Compiling and running tests](#compiling-and-running-tests)
2626
3. [Limiting the test scope](#limiting-the-test-scope)
2727
4. [Test directory structure](#test-directory-structure)
28+
1. [Unit testing](#unit-testing)
2829
1. [Publishing your changes](#publishing-your-changes)
2930
1. [Checking status](#checking-status)
3031
2. [Pushing upstream](#pushing-upstream)
@@ -727,6 +728,25 @@ As shown above, tests exist inside `TESTS\testgroup\testcase\` directories. Plea
727728

728729
<span class="notes">**Note:** `mbed test` does not work in applications that contain a `main` function that is outside of a `TESTS` directory.</span>
729730

731+
## Unit testing
732+
733+
Use the `mbed unittest` command to build and run unit tests, and to generate files for new unit tests.
734+
735+
Build and run unit tests with `mbed unittest`. The arguments are:
736+
737+
* `--skip-build` to skip building unit tests.
738+
* `--skip-run` to skip running unit tests.
739+
* `--clean` to clean previous build data.
740+
* `-d` or `--debug` to prepare debug build.
741+
* `--coverage <TYPE>` to generate code coverage report where TYPE can be "html", "xml" or "both".
742+
* `-m <NAME>` or `--make-program <NAME>` to select which make build tool to use where NAME can be "make", "gmake", "mingw32-make" or "ninja".
743+
* `-g <NAME>` or `--generator <NAME>` to select which CMake generator to use where NAME can be "Unix Makefiles", "MinGW Makefiles" or "Ninja".
744+
* `-r <EXPRESSION>` or `--regex <EXPRESSION>` to run tests matching the regular expression.
745+
* `--build-path <PATH>` to specify build path.
746+
* `-v` or `--verbose` for verbose diagnostic output.
747+
748+
Generate files for a new unit test with `mbed unittest --new <FILE>`.
749+
730750
## Publishing your changes
731751

732752
### Checking status

mbed/mbed.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3011,6 +3011,51 @@ def help_():
30113011
return parser.print_help()
30123012

30133013

3014+
@subcommand('unittest',
3015+
dict(name='--skip-build', action='store_true', help='skip build step'),
3016+
dict(name='--skip-run', action='store_true', help='skip run step'),
3017+
dict(name='--clean', action='store_true', help='clean build data'),
3018+
dict(name=['-d', '--debug'], action='store_true', help='enable debug build'),
3019+
dict(name='--coverage', choices=['html', 'xml', 'both'], help='generate code coverage report'),
3020+
dict(name=['-m', '--make-program'], choices=['gmake', 'make', 'mingw32-make', 'ninja'], help='which make program to use'),
3021+
dict(name=['-g', '--generator'], choices=['Unix Makefiles', 'MinGW Makefiles', 'Ninja'], help='which CMake generator to use'),
3022+
dict(name=['-r', '--regex'], help='run tests matching regular expression'),
3023+
dict(name='--build-path', help='specify build path'),
3024+
dict(name='--new', help='generate files for a new unit test', metavar="FILEPATH"),
3025+
help='Create, build and run unit tests')
3026+
def unittest_(skip_build=False, skip_run=False, clean=False, debug=False, coverage=None, make_program=None, generator=None, regex=None, build_path=None, new=None):
3027+
program = Program(getcwd(), False)
3028+
program.check_requirements(True)
3029+
3030+
mbed_os_dir = program.get_os_dir()
3031+
if mbed_os_dir is None:
3032+
error("No Mbed OS directory found.")
3033+
unittests_dir = os.path.join(mbed_os_dir, "UNITTESTS")
3034+
3035+
tool = os.path.join(unittests_dir, "mbed_unittest.py")
3036+
3037+
# Prepare environment variables
3038+
env = program.get_env()
3039+
3040+
if os.path.exists(tool):
3041+
popen([python_cmd, tool]
3042+
+ (["--skip-build"] if skip_build else [])
3043+
+ (["--skip-run"] if skip_run else [])
3044+
+ (["--clean"] if clean else [])
3045+
+ (["--debug"] if debug else [])
3046+
+ (["--coverage", coverage] if coverage else [])
3047+
+ (["--make-program", make_program] if make_program else [])
3048+
+ (["--generator", generator] if generator else [])
3049+
+ (["--regex", regex] if regex else [])
3050+
+ (["--build-path", build_path] if build_path else [])
3051+
+ (["--new", new] if new else [])
3052+
+ (["--verbose"] if verbose else [])
3053+
+ remainder,
3054+
env=env)
3055+
else:
3056+
warning("Unit testing is not supported with this Mbed OS version.")
3057+
3058+
30143059
def main():
30153060
global verbose, very_verbose, remainder, cwd_root
30163061

0 commit comments

Comments
 (0)