2

I have three code files for an arduino project:

main.ino  <-- main sketch
helper.cpp  <-- helper functions, no avr code
helper_test.cpp  <-- unit test for helpers

Arduino will attempt to include helper_test.cpp, and will be confused by its inclusion of the unit test library header files (which happen to be google test). If the unit test contains a C main function it will skip everything in main.ino and try to use only that.

I know there are dedicated arduino unit test frameworks, these are just regular c++ unit tests for math functions that don't touch any avr-related code.

How can I lay these files out or make this so arduino won't try to include helper_test.cpp but will include helper.cpp?

Ulrich Eckhardt
  • 16,572
  • 3
  • 28
  • 55
ʞɔıu
  • 47,148
  • 35
  • 106
  • 149
  • Every project I've downloaded from the web or worked on (that uses unit tests) keeps tests separated from production code. Using defines may seem appealing at first but your project will grow and number of `cpp` files with tests will increase. There is no point in including dozens of `cpp` files whose code is completely excluded. Not to mention that you have to constantly add/remove that define to exclude/include tests in your build. – Marko Popovic Jan 22 '16 at 08:36
  • I am currently thinking about the same problem. I would like to build sketches that rely on my own libraries that are separated from the main sketch, and live in a project where they can be unit tested. I was thinking that the library project should contain a sub-folder that could be directly pointed to from Arduino. But the overall project adds gtest and other tests, so that within the context of that project you can test the library, emulating any hardware pieces. I am currently working through an example of this here: https://github.com/kigster/librgb – Konstantin Gredeskoul Jan 25 '16 at 03:16
  • Did you manage to solve your problem? Consider accepting one of the answers if it was helpful. – Marko Popovic Jan 27 '16 at 08:52

4 Answers4

0

You need to create two separate projects. One project will contain your production code. It will have these files:

main.ino  <-- main sketch
helper.h  <-- header for helper functions
helper.cpp  <-- implementation for helper functions

and all other files necessary to create your arduino app. You will build it when you want to create the app itself. The other project will be a standard executable that will run unit tests:

main_test.cpp  <-- main 
helper_test.cpp  <-- unit test for helpers
../arduino_project/helper.h <-- header for helper functions
../arduino_project/helper.cpp <-- implementation for helper functions

Every time you add new function or make change to some helper function, you build this executable and run it to see if all tests pass.As you can see, this project will add files (that contain function which will be tested) from your main project. The best way would be to add the location of source files from the main project as additional include directories for unit tests project.

Andrew Bullock
  • 36,616
  • 34
  • 155
  • 231
Marko Popovic
  • 3,999
  • 3
  • 22
  • 37
0

Just let him include it, but exclude all the code. I mean:

File test_enablers.h
#define INCLUDE_HELPER_TEST

File helper_test.cpp
#include "test_enablers.h"
#ifdef INCLUDE_HELPER_TEST
... your helper test code
#endif /* INCLUDE_HELPER_TEST */

You can add as many test files as you want; just add the flags in the .h file

If you want to disable the test file just comment the #define and the file won't be compiled.

If you want to exclude some code in some other files (e.g. the setup and loop functions in the main, since you are already implementing it in the test, just write

File main.ino
#include "test_enablers.h"
#ifndef INCLUDE_HELPER_TEST
... your production setup and loop
#endif /* INCLUDE_HELPER_TEST */

(note the ifndef in place of the ifdef)

frarugi87
  • 2,826
  • 1
  • 20
  • 41
0

I don't think the Arduino compiler (and related systems) are set up to handle unit testing, I had to build my own framework to do this that I've written about in this answer to a related Arduino-unit-testing question.

For this to work, you'd need to put the bulk of your code in a library, and the unit tests would be stored as a subdirectory of that library called test/. You could test the complete sketches by putting them in the examples/ directory of the library, but only compilation can be tested on those.

Ian
  • 11,280
  • 3
  • 36
  • 58
0

If you want to unit test, simply create code that is going to execute one task. One can purchase a Logic Analyzer and examine the ports' outputs to see if the right signal is being delivered. Unit Testing has to be done differently in most cases due to the nature of Arduinos. Hope this helps.

ncaswell
  • 1
  • 3