docs/develop/writingtests.rst
.. _writingtests:
Tests in MicroPython are located at the path tests/. The following is a listing of
key directories and the run-tests.py runner script:
.. code-block:: bash
. ├── basics ├── extmod ├── float ├── micropython ├── run-tests.py ...
There are subfolders maintained to categorize the tests. Add a test by creating a new file in one of the existing folders or in a new folder. It's also possible to make custom tests outside this tests folder, which would be recommended for a custom port.
For example, add the following code in a file print.py in the tests/unix/ subdirectory:
.. code-block:: python
def print_one(): print(1)
print_one()
If you run your tests, this test should appear in the test output:
.. code-block:: bash
$ cd ports/unix $ make tests skip unix/extra_coverage.py pass unix/ffi_callback.py pass unix/ffi_float.py pass unix/ffi_float2.py pass unix/print.py pass unix/time.py pass unix/time2.py
Tests are run by comparing the output from the test target against the output from CPython. So any test should use print statements to indicate test results.
For tests that can't be compared to CPython (i.e. micropython-specific functionality),
you can provide a .py.exp file which will be used as the truth for comparison.
The other way to run tests, which is useful when running on targets other than the Unix port, is:
.. code-block:: bash
$ cd tests $ ./run-tests.py
Then to run on a board:
.. code-block:: bash
$ ./run-tests.py -t /dev/ttyACM0
And to run only a certain set of tests (eg a directory):
.. code-block:: bash
$ ./run-tests.py -d basics $ ./run-tests.py float/builtin*.py
The run-tests.py script supports several parameters to customize test execution:
Target and Device Selection:
-t, --test-instanceThe -t option accepts the following for the test instance:
Test Selection:
-d, --test-dirs - Specify one or more test directories to run-i, --include REGEX - Include tests matching regex pattern-e, --exclude REGEX - Exclude tests matching regex patternfiles - Specific test files to runExecution Options:
--emit <EMITTER> - MicroPython emitter, EMITTER can be bytecode or native. Default: bytecode--via-mpy - Compile .py files to .mpy first--heapsize - Set heap size for tests-j, --jobs N - Number of tests to run simultaneouslySet the MICROPY_MPYCROSS environment variable to use a specific version of mpy-cross when using --via-mpy.
Result Management:
-r, --result-dir - Directory for test results. Default: results/--print-failures - Show diff of failed tests and exit--clean-failures - Delete .exp and .out files from prior failed tests--run-failures - Re-run only previously failed testsExamples:
.. code-block:: bash
$ ./run-tests.py --emit native -d basics extmod
$ ./run-tests.py -e async
$ ./run-tests.py -i pep
$ ./run-tests.py -j 4 basics/list*.py
$ ./run-tests.py -t /dev/ttyUSB0
$ ./run-tests.py -t u0
$ ./run-tests.py --run-failures