--- title: Automated tests --- Both unit tests and functional tests are used to verify pyOCD. The primary difference between unit tests and functional tests is that unit tests will work without a debug probe connected. Some unit tests do take advantage of a connected probe to expand testing, but those tests will be skipped if no probe is present. In contrast, all functional tests require at least one probe to be connected. ## Test Setup For test setup it is good practice to create a virtualenv in the repos root subdirectory. Otherwise expect weird behavior during tests. ```bash $ python -mvenv --upgrade-deps .venv $ source .venv/bin/activate $ pip install -e .[test] $ pytest ``` To make the tests pick the correct setup, a [`pyocd.yaml` configuration file](configuration.md) has to be used. For functional testing the file should contain a reference to a `test_binary` from `test/data/binaries`. Example config file: ```yaml probes: E6614103E7176A23: # Probe's unique ID. target_override: nrf52 test_binary: l1_nrf52840-dk.bin frequency: 6000000 ``` ## Unit tests The unit tests are located in the `tests/unit` directory of the repo. They must be executed using pytest, as they rely on the advanced capabilities of this tool. To run the unit tests, simply invoke `pytest` in the root directory of the repo. Read the pytest help to see the many options it provides. To get code coverage results, do the following: ``` $ pytest --cov-report=html --cov=pyocd $ open htmlcov/index.html ``` Note: The `semihosting.py` unit test requires a target connection and test binary. It is currently the only unit that uses a target. When no targets are available the test cases will be skipped. ## Functional tests A series of fairly comprehensive functional tests are provided in the `test/` directory. The primary script for running these tests is `automated_test.py`. It will execute all functional tests in sequence for all connected debug probes, then produce a summary and JUnit-style XML report. This script is used to execute our CI test plan, and we frequently use it on our personal development systems to test prior to creating pull requests. The `automated_test.py` script has several command line arguments that can be used to control which test suites are run and on which debug probes. Use `--list-tests` to see available test suite names. Comma-separated lists of these names can be passed to the `-x` / `--exclude-tests` or `-i` / `--include-tests` arguments to exclude and include tests, respectively. Only one of these two arguments can be used at a time. The `-b` / `--board` argument is used to select a debug probe on which tests will be run. By default, tests will run on all available debug probes. Adding any `--board` arguments restricts tests to run on only the specified set of debug probes. ### List of functional tests Test scripts with an "n/a" for the test name are not run by `automated_test.py` (or in CI) and have to be run directly with Python.
| Test name | File | Description |
|---|---|---|
| Basic Test | basic_test.py | Simple test that checks a range of basic functionality, from flash programming to accessing memory and core registers. |
| n/a | blank_test.py | Tests ability to connect to devices with with blank flash. |
| Commander Test | commander_test.py | Tests the pyocd commander functionality. |
| Commands Test | commands_test.py | Tests commands supported by commander and gdb monitor commands. |
| Concurrency Test | concurrency_test.py | Verify multiple threads can simultaneously access a debug probe, specifically for memory transfers. |
| Connect Test | connect_test.py | Tests all combinations of the halt on connect and disconnect resume options. |
| Cortex Test | cortex_test.py | Validates CPU control operations and memory accesses. |
| Debug Context Test | debug_context_test.py | Tests some DebugContext classes. |
| Flash Loader Test | flash_loader_test.py | Test the classes in the pyocd.flash.loader module. |
| Flash Test | flash_test.py | Comprehensive test of flash programming. |
| n/a | import_all_.py | Imports all pyocd modules. Run by the GitHub "basic test" workflow. |
| Gdb Test | gdb_test.py | Tests the gdbserver by running the gdb_test_script.py script in a gdb process. Note that on Windows, the 32-bit Python 2.7 must be installed for the Python-enabled arm-none-eabi-gdb to work properly and for this test to pass. |
| Json Lists Test | json_lists_test.py | Validates the JSON output from pyocd json. |
| n/a | parallel_test.py | Checks for issues with accessing debug probes from multiple processes and threads simultaneously. |
| Probeserver Test | probeserver_test.py | Verify remote probe server and client. |
| Speed Test | speed_test.py | Performance test for memory reads and writes. |
| User Script Test | user_script_test.py | Verify loading of user scripts. |