Skip to content
egoogins edited this page Mar 29, 2013 · 3 revisions

We use Google's GTest framework for unit tests. It is a great framework that is very easy to use, and all modules should have unit tests written for them.

TEMPORARY USE

Since EVERYONE should be using testing, until the changes are pulled into the master, EJ's overhaul branch (egoogins/overhaul) can be merged into your overhaul branches. There should be no conflicts, ask him if you have any you can't understand. And example of a current (basic) unit tests can be found on his ball track branch (egoogins/overhaul-balltrack). Additionally go to the google documentation for significant documentation provided by google which shows you ALLLLLL the great things you can do.

How to implement

To maintain consistency in our codebase, you should create a tests folder in each modules directory which contains all that modules test files. After writing the test (once again, view EJ's balltrack branch for an example) you need to add the test to that modules CMakeList. This consists of ensuring that the CMakeList adds the executable, links the appropriate libraries, adds the test, and adds a target. All of this should be done ONLY IF YOU ARE RUNNING CODE ON YOUR MACHINE, not when compiling for a robot, so this should all take place in an IF, ENDIF block where you ensure the system is compiling for OFFLINE.

  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 inbetween the IF and ENDIF.

  2. Use add_executable() to add an executable. this takes two arguments, the first is the name of the executable, the second is the file of your executable ie the test you wrote. So an example would be add_executable( some_module_test \n tests/my_module_test.cpp)

  3. Link the libraries with target_link_libraries(). This should take in the modules library name, the executable, pthread (required for google tests), and googletest library aka ${GTEST_LIBRARY}

  4. Next add a test with add_test(). This should take two arguments, the name of the test and the executable. It's ok to use the same name for both since one is a string and one is an executable, and cmake is whicked smaht. So an example would be add_test(some_module_test \n some_module_test)

  5. Finally add the custom target for your test using add_custom_target() this should take in the custom target and your test, ie add_custom_target(mytarget \n DEPENDS some_module_test)

  6. Now you're done with the modules CMakeList, and you just need the Makefile to run your target. There is probably a better way to do this, but for now we'll assume you only want to run 1 test at a time. To run that test, go to src/man/Makefile and change two things. Search for 'check' and replace check with your custom target.

  7. You're done!!

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. then once the file is configured, run make test and it will compilethe relevant code and test your module! This is a crucial step for our team to take as good testing means good and easily managed code. PLEASE start using tests right. Look at egoogins/balltrack for a basic example, talk to somebody if you are having trouble, but don't ignore this. 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. And everyone thank Octavian when you see him for getting the ball rolling on this and setting it up. He's a boss.

Clone this wiki locally