Testing - northern-bites/nbites GitHub Wiki

We use Google's GTest framework for C++ unit tests. It is a great framework that is very easy to use, and all modules should have unit tests written for them. We will be using the PyUnit (unittest) module, which is in the Python standard library for testing Python code.

How to implement

To maintain consistency in our codebase, you should create a test folder in each module's directory, which contains all that modules test files. After writing the test (look at gamestate, balltrack, or vision for examples) you need to add the test to that modules CMakeList. There is a simple macro nbites_add_test() that will create an executable and link the specified libraries. All of this should be done only while OFFLINE, not when compiling for a robot, so this should all take place in an if( OFFLINE ) block.

  1. First declare the if ( OFFLINE ) statement at the end of the current CMakeList if there are not currently any tests in the directory. Declare an endif ( OFFLINE ) statement underneath, all the following code should be indented and between the if and endif.

  2. Use nbites_add_test() to add the test. The first argument should be the name of the executable you want to create (for example this would be CommTests), the second is the cpp test file, i.e. the test you wrote. This is without the test/ directory prefix, as the macro assumes to look there already. Any additional arguments to the macro will be linked with the executable. You should link the module you are testing, but beyond that you don't have to have much.

How to use the tests

To use the tests you need to compile the codebase to work on your computer, which you designate by using make straight instead of make cross. Once the file is configured, run make to compile all the code, and then run make test to run all of the tests in the system. You will get a summary at the end of all of the tests to tell you if anything failed.

This is a crucial step for our team to take as good testing means good and easily managed code. PLEASE start using tests right. There is no excuse to saying something works before you've written a test and it has passed. The easiest way to do this is look at an example while you're going through the steps, and as always, edit the wiki as things change and as you use it to make it clearer for the next one.