Folder structurefor tests in project and in library


I’m rather new to cmake and I struggle a bit with my folders structure

The project is cross-compiled to arm but the tests runs on the host. I don’t think that the multi-arch setup is relevant for this question but I add it for context just in case.

So here is my current project structure


  • Src/
    main.cpp (main for the target)
  • Inc/… (all headers)
  • Test/… (all test_.cpp)
    testMain.cpp (main for the host to discover and run the tests)
    – Mock/mock_
    .h (all the mocks for the hardware abstraction between the host and the target)
    – thirdparty/
    doctest.h (test framework)
    tromploeil.h (mock framework)
  • MyLib/ (static lib used by MyProject, MyOtherProject and others)
    – Src/
    – Inc/
    – Test… (all test_.cpp)
    testMain.cpp (main for the host to discover and run the library tests)
    – Mock/mock_
    .h (all the mocks needed to test the library)
    – thirdparty/
    doctest.h (test framework)
    tromploeil.h (mock framework)

So my issue is that the test framework (Test/thirdparty) is duplicated in each project and in the library.
That makes MyLib independant from the projects using it so it seems good from that perspective but it seems a bit redundant.

And I have the additionnal probem that the mocks from the MyLib/Test/Mock folder are sometimes usefull for the tests of the projects as well.

I would like to be able to run the tests of the project only from my ide. But, for theCI builds, I would like to perform the full test suite (MyProject tests + MyLib tests)

What are the recommended/best practices for such setups?


Is this the on disk structure or in the repo? I assume repo and you are adding MyLib as a git submodule but vendoring Test/thirdparty in every project? Easy answer to reduce duplication would be to use thirdparty as a submodule too. Or use FetchContent to get thridparty and MyLib?

For the mocks: if there is a core et of mocks that are useful cross-project why not create a header only lib to modularize and share them?