Including Class .h and .cpp file in src folder and in test environment

Hi,

i am currently struggeling on including files CORRECTLY in my Unit-Testing environment and googeling/doing research on forum does not really helped me.

Given Condition:
I am currently developing a application in my esp32dev environment within platformIO. In my src/ folder i got my “real-world” Application which is up and running. My main.cpp and lets say my Custom Class specific appClass.h and appClass.cpp. To ensure code quality i am testing appClass.h and appClass.cpp in unit testing also.

In my test/test_desktop I added a test_appClass.cpp file where i summaize some testszenarios like test_appClass_Methode1.h, test_appClass_Methode2.h and test_appClass_Methode3.h. In all of the .h files i start by including the the appClass.h. This should be enough - i thought because i intermediatly get the issue that the reference to the .cpp file is missing. So if i included now the .cpp i got instant the next issue because the .cpp implementation is done multiple -__-…

My current solution is that i include the .cpp file once in the three .h file and it actually works. But i do not see this as a final solution and as a current workaround.

My question is now - how should i better go on and structure that kind of test setup?!? I do not want to include .cpp file at all…

Maybe as a not-cool but simple workaround comming in my mind:

couldn’t I use the original lib/ folder of the original project structure to point into the src/ folder directorys like in a simple header file?

File within lib/ folder:
#ifdef UNITTESTING
#include “…/src/appClass.cpp”
#include “…/src/appClass.h”
#endif

could this be a simple solution for it?

Refactor to be tested logic into libraries as

lib/AppLogic/appClass{.cpp, .h}

The unit test can include the library just fine. Of course, that appClass shouldn’t have dependencies on files in src/ then, but only have dependencies on other libraries or the framework (Arduino or whatever).

You can also say that all source code in src/ should be compiled for unit testing, but you then

  1. have to ensure that your e.g. src/main.cpp does not define setup() and loop() because that will now be implemented in your test file (tests/embedded/test_1.cpp), by e.g. testing for #ifndef UNIT_TESTING void setup() { .. } void loop() { .. } #endif
  2. test_build_src = yes must be in the platformio.ini

All of this is documented in

https://docs.platformio.org/en/latest/advanced/unit-testing/structure/index.html

Hi maxgerhardt,

thank you for your response. I already saw that kind of workaround solution to put the project specific .h and .cpp file into the lib folder…actually i am not a fan of it, because it is actually not a lib at all. It is project specific stuff which should be accessable during Unit-Testing and its environment - and that part is not given.

Well, as i already said, the src folder is for esp32 environment and test_build_src will never-ever work on that. Maybe spamming all product specific files by #define / #ifndef would work of course but I think it is still a totally workaround on that…

I am currently thinking about, how big projects are solving this kind of issue…

That was (is) the first thing I’ve been asking here ;p

Not a big project, learning experience mostly, but I also wanted to test as much as I can: GitHub - MacDada/DnWiFiDoorLock – In general I separated code into 3 sections: unit testable “raw” code, mockable Arduino lib layer code and untestable code.