1*cfb92d14SAndroid Build Coastguard Worker# OpenThread Unit Tests 2*cfb92d14SAndroid Build Coastguard Worker 3*cfb92d14SAndroid Build Coastguard WorkerThis page describes how to build and run OpenThread unit tests. It will be helpful for developers to debug failed unit test cases if they got one in CI or to add some new test cases. 4*cfb92d14SAndroid Build Coastguard Worker 5*cfb92d14SAndroid Build Coastguard Worker## Build Simulation 6*cfb92d14SAndroid Build Coastguard Worker 7*cfb92d14SAndroid Build Coastguard WorkerThe unit tests cannot be built solely without building the whole project. So first build OpenThread on the simulation platform, which will also build all unit tests: 8*cfb92d14SAndroid Build Coastguard Worker 9*cfb92d14SAndroid Build Coastguard Worker``` 10*cfb92d14SAndroid Build Coastguard Worker# Go to the root directory of OpenThread 11*cfb92d14SAndroid Build Coastguard Worker$ script/cmake-build simulation 12*cfb92d14SAndroid Build Coastguard Worker``` 13*cfb92d14SAndroid Build Coastguard Worker 14*cfb92d14SAndroid Build Coastguard Worker## List all tests 15*cfb92d14SAndroid Build Coastguard Worker 16*cfb92d14SAndroid Build Coastguard WorkerTo see what tests are available in OpenThread: 17*cfb92d14SAndroid Build Coastguard Worker 18*cfb92d14SAndroid Build Coastguard Worker``` 19*cfb92d14SAndroid Build Coastguard Worker# Make sure you are at the simulation build directory (build/simulation) 20*cfb92d14SAndroid Build Coastguard Worker$ ctest -N 21*cfb92d14SAndroid Build Coastguard Worker``` 22*cfb92d14SAndroid Build Coastguard Worker 23*cfb92d14SAndroid Build Coastguard Worker## Run the Unit Tests 24*cfb92d14SAndroid Build Coastguard Worker 25*cfb92d14SAndroid Build Coastguard WorkerTo run all the unit tests: 26*cfb92d14SAndroid Build Coastguard Worker 27*cfb92d14SAndroid Build Coastguard Worker``` 28*cfb92d14SAndroid Build Coastguard Worker# Make sure you are at the simulation build directory (build/simulation) 29*cfb92d14SAndroid Build Coastguard Worker$ ctest 30*cfb92d14SAndroid Build Coastguard Worker``` 31*cfb92d14SAndroid Build Coastguard Worker 32*cfb92d14SAndroid Build Coastguard WorkerTo run a specific unit test, for example, `ot-test-spinel`: 33*cfb92d14SAndroid Build Coastguard Worker 34*cfb92d14SAndroid Build Coastguard Worker``` 35*cfb92d14SAndroid Build Coastguard Worker# Make sure you are at the simulation build directory (build/simulation) 36*cfb92d14SAndroid Build Coastguard Worker$ ctest -R ot-test-spinel 37*cfb92d14SAndroid Build Coastguard Worker``` 38*cfb92d14SAndroid Build Coastguard Worker 39*cfb92d14SAndroid Build Coastguard Worker## Update a Test Case 40*cfb92d14SAndroid Build Coastguard Worker 41*cfb92d14SAndroid Build Coastguard WorkerIf you are developing a unit test case and have made some changes in the test source file, you will need rebuild the test before running it: 42*cfb92d14SAndroid Build Coastguard Worker 43*cfb92d14SAndroid Build Coastguard Worker``` 44*cfb92d14SAndroid Build Coastguard Worker# Make sure you are at the simulation build directory (build/simulation) 45*cfb92d14SAndroid Build Coastguard Worker$ ninja <test_name> 46*cfb92d14SAndroid Build Coastguard Worker``` 47*cfb92d14SAndroid Build Coastguard Worker 48*cfb92d14SAndroid Build Coastguard WorkerThis will only build the test and take a short time. 49*cfb92d14SAndroid Build Coastguard Worker 50*cfb92d14SAndroid Build Coastguard WorkerIf any changes or fixes were made to the OpenThread code, then you'll need to rebuild the entire project: 51*cfb92d14SAndroid Build Coastguard Worker 52*cfb92d14SAndroid Build Coastguard Worker``` 53*cfb92d14SAndroid Build Coastguard Worker# Make sure you are at the simulation build directory (build/simulation) 54*cfb92d14SAndroid Build Coastguard Worker$ ninja 55*cfb92d14SAndroid Build Coastguard Worker``` 56*cfb92d14SAndroid Build Coastguard Worker 57*cfb92d14SAndroid Build Coastguard WorkerThis will build the updated OpenThread code as well as the test cases. 58