| 1 | --- |
| 2 | title: Automated tests |
| 3 | --- |
| 4 | |
| 5 | Both unit tests and functional tests are used to verify pyOCD. |
| 6 | |
| 7 | The primary difference between unit tests and functional tests is that unit tests will work without |
| 8 | a debug probe connected. Some unit tests do take advantage of a connected probe to expand testing, |
| 9 | but those tests will be skipped if no probe is present. In contrast, all functional tests require |
| 10 | at least one probe to be connected. |
| 11 | |
| 12 | |
| 13 | ## Test Setup |
| 14 | |
| 15 | For test setup it is good practice to create a virtualenv in the repos root subdirectory. Otherwise expect weird |
| 16 | behavior during tests. |
| 17 | |
| 18 | ```bash |
| 19 | $ python -mvenv --upgrade-deps .venv |
| 20 | $ source .venv/bin/activate |
| 21 | $ pip install -e .[test] |
| 22 | $ pytest |
| 23 | ``` |
| 24 | |
| 25 | To make the tests pick the correct setup, a [`pyocd.yaml` configuration file](configuration.md) has to be used. |
| 26 | For functional testing the file should contain a reference to a `test_binary` from `test/data/binaries`. |
| 27 | |
| 28 | Example config file: |
| 29 | |
| 30 | ```yaml |
| 31 | probes: |
| 32 | E6614103E7176A23: # Probe's unique ID. |
| 33 | target_override: nrf52 |
| 34 | test_binary: l1_nrf52840-dk.bin |
| 35 | frequency: 6000000 |
| 36 | ``` |
| 37 | |
| 38 | |
| 39 | ## Unit tests |
| 40 | |
| 41 | The unit tests are located in the `tests/unit` directory of the repo. They must be executed using |
| 42 | pytest, as they rely on the advanced capabilities of this tool. |
| 43 | |
| 44 | To run the unit tests, simply invoke `pytest` in the root directory of the repo. Read the pytest |
| 45 | help to see the many options it provides. |
| 46 | |
| 47 | To get code coverage results, do the following: |
| 48 | |
| 49 | ``` |
| 50 | $ pytest --cov-report=html --cov=pyocd |
| 51 | $ open htmlcov/index.html |
| 52 | ``` |
| 53 | |
| 54 | 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. |
| 55 | |
| 56 | |
| 57 | ## Functional tests |
| 58 | |
| 59 | A series of fairly comprehensive functional tests are provided in the `test/` directory. |
| 60 | |
| 61 | The primary script for running these tests is `automated_test.py`. It will execute all functional tests in |
| 62 | sequence for all connected debug probes, then produce a summary and JUnit-style XML report. This |
| 63 | script is used to execute our CI test plan, and we frequently use it on our personal development |
| 64 | systems to test prior to creating pull requests. |
| 65 | |
| 66 | 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. |
| 67 | |
| 68 | 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. |
| 69 | |
| 70 | ### List of functional tests |
| 71 | |
| 72 | 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. |
| 73 | |
| 74 | <table> |
| 75 | |
| 76 | <tr><th width="20%">Test name</th><th width="20%">File</th><th>Description</th></tr> |
| 77 | |
| 78 | <tr><td>Basic Test</td><td><tt>basic_test.py</tt></td><td> |
| 79 | Simple test that checks a range of basic functionality, from flash programming to accessing memory and core registers. |
| 80 | </td></tr> |
| 81 | |
| 82 | <tr><td>n/a</td><td><tt>blank_test.py</tt></td><td> |
| 83 | Tests ability to connect to devices with with blank flash. |
| 84 | </td></tr> |
| 85 | |
| 86 | <tr><td>Commander Test</td><td><tt>commander_test.py</tt></td><td> |
| 87 | Tests the <tt>pyocd commander</tt> functionality. |
| 88 | </td></tr> |
| 89 | |
| 90 | <tr><td>Commands Test</td><td><tt>commands_test.py</tt></td><td> |
| 91 | Tests commands supported by commander and gdb monitor commands. |
| 92 | </td></tr> |
| 93 | |
| 94 | <tr><td>Concurrency Test</td><td><tt>concurrency_test.py</tt></td><td> |
| 95 | Verify multiple threads can simultaneously access a debug probe, specifically for memory transfers. |
| 96 | </td></tr> |
| 97 | |
| 98 | <tr><td>Connect Test</td><td><tt>connect_test.py</tt></td><td> |
| 99 | Tests all combinations of the halt on connect and disconnect resume options. |
| 100 | </td></tr> |
| 101 | |
| 102 | <tr><td>Cortex Test</td><td><tt>cortex_test.py</tt></td><td> |
| 103 | Validates CPU control operations and memory accesses. |
| 104 | </td></tr> |
| 105 | |
| 106 | <tr><td>Debug Context Test</td><td><tt>debug_context_test.py</tt></td><td> |
| 107 | Tests some <tt>DebugContext</tt> classes. |
| 108 | </td></tr> |
| 109 | |
| 110 | <tr><td>Flash Loader Test</td><td><tt>flash_loader_test.py</tt></td><td> |
| 111 | Test the classes in the <tt>pyocd.flash.loader</tt> module. |
| 112 | </td></tr> |
| 113 | |
| 114 | <tr><td>Flash Test</td><td><tt>flash_test.py</tt></td><td> |
| 115 | Comprehensive test of flash programming. |
| 116 | </td></tr> |
| 117 | |
| 118 | <tr><td>n/a</td><td><tt>import_all_.py</tt></td><td> |
| 119 | Imports all pyocd modules. Run by the GitHub "basic test" workflow. |
| 120 | </td></tr> |
| 121 | |
| 122 | <tr><td>Gdb Test</td><td><tt>gdb_test.py</tt></td><td> |
| 123 | Tests the gdbserver by running the <tt>gdb_test_script.py</tt> script in a gdb process. |
| 124 | Note that on Windows, the 32-bit Python 2.7 must be installed for the Python-enabled <tt>arm-none-eabi-gdb</tt> to work properly and for this test to pass. |
| 125 | </td></tr> |
| 126 | |
| 127 | <tr><td>Json Lists Test</td><td><tt>json_lists_test.py</tt></td><td> |
| 128 | Validates the JSON output from <tt>pyocd json</tt>. |
| 129 | </td></tr> |
| 130 | |
| 131 | <tr><td>n/a</td><td><tt>parallel_test.py</tt></td><td> |
| 132 | Checks for issues with accessing debug probes from multiple processes and threads simultaneously. |
| 133 | </td></tr> |
| 134 | |
| 135 | <tr><td>Probeserver Test</td><td><tt>probeserver_test.py</tt></td><td> |
| 136 | Verify remote probe server and client. |
| 137 | </td></tr> |
| 138 | |
| 139 | <tr><td>Speed Test</td><td><tt>speed_test.py</tt></td><td> |
| 140 | Performance test for memory reads and writes. |
| 141 | </td></tr> |
| 142 | |
| 143 | <tr><td>User Script Test</td><td><tt>user_script_test.py</tt></td><td> |
| 144 | Verify loading of user scripts. |
| 145 | </td></tr> |
| 146 | |
| 147 | </table> |
| 148 | |
| 149 | |
| 150 | ## Test binaries |
| 151 | |
| 152 | The functional tests and some unit tests (currently only `test/unit/semihosting.py`) require a test firmware binary in order to run. This firmware can be extremely simple. The only requirement is that it have a valid vector table and be executable when loaded to the base of the boot memory. Ideally an LED is blinked so there is an easily-identifiable visual signal that the firmware is running. |
| 153 | |
| 154 | Built-in targets almost all include a test binary in the pyOCD repository under `test/data/binaries/`. If the board has a board ID, e.g., those listed in `pyocd/board/board_ids.py`, the test binary is automatically identified. |
| 155 | |
| 156 | If the target is not built-in (DFP) or does not have a board ID then, the `test_binary` session option must be set to the path of a test firmware binary file relative to the `test/data/binaries` directory. This can be conveniently added as probe-specific options in a `pyocd.yaml` config file placed under `test/`. |
| 157 | |
| 158 | |
| 159 | ## Azure Pipelines |
| 160 | |
| 161 | PyOCD uses Azure Pipelines to run the CI tests for commits and pull requests. The pipeline runs the functional tests on |
| 162 | a set of test machines, called self-hosted test agents in Azure Pipelines parlance. There is one each of Mac, Linux, and |
| 163 | Windows test agents. |
| 164 | |
| 165 | The complete results from pipeline runs are [publicly |
| 166 | accessible](https://dev.azure.com/pyocd/pyocd/_build?definitionId=1&_a=summary). |
| 167 | |
| 168 | For pull requests, a pyOCD team member or collaborator must manually initiate the pipeline run by entering a special |
| 169 | comment of the form "/azp run" or "/AzurePipelines run". |
| 170 | |
| 171 | |
| 172 | ## Testing with tox |
| 173 | |
| 174 | pyOCD includes a configuration file for tox that enables easy testing of multiple Python versions. |
| 175 | The tox tool is included in `test` install extra, so it will already be present in a standard |
| 176 | pyOCD developer virtual environment. |
| 177 | |
| 178 | To run the functional tests via tox, just execute `tox` from the root of the pyOCD |
| 179 | repo. It will create new virtual environments for each Python version and run `automated_test.py`. |
| 180 | |
| 181 | Currently only the functional tests are included in the tox configuration. |