0

I am learning C++ and am working on a QT5 application which is going pretty well. I am at the point where I need to implement some unittests (catch2) on a few specific parts of the application.

I am lost... I know how to write the tests and there is quite alot of documentation about that. How to configure my project directory structure and the changes to make to CMakeList.txt is a very different story. It seems everyone sort of does whatever worked for them and then tries to not talk about it to much. 99% of all tutorials about testing start with creating the cpp files assuming everything else has already been taken care of and the left over 1% aren't really matching my requirements.

I am looking for some boiler-plate config or pointers on how to get this stuff to work. I myself came up with the following:

  • project dir
    • external
      • linux
        • Qt
        • ArcGis
      • src
        • Gui
          • Menus
            • MainMenu.h
            • MainMenu.cpp
          • Views
            • MapView.cpp
            • MapView.h
            • NavigatorView.cpp
            • NavigatorView.h
          • MainWindow.cpp
          • MainWindow.h
        • Models
          • BaseModel.h
          • BaseModel.cpp
          • SurveyModel.h
          • SurveyModel.cpp
          • SectionModel.h
          • SectionModel.cpp
          • StationModel.h
          • StationModel.cpp
        • DataSources
          • Usb
            • Reader.cpp <-- I want to build a test for this file
            • Reader.h
        • main.cpp
        • CMakeList.txt
      • tests
        • DataSources
          • Usb
            • ReaderTest.cpp <-- where this would be the test file
        • CmakeList.txt
    • CMakeList.txt
    • global_config.h.in

The CMakeList.txt for the ./src dir works and creates an executable for my application. But after that the confusion starts:

In the tests dir, I can build my tests aslong as I don't require anything from my src dir. Which make my tests rather useless. I tried include_dir and add_sub_directories and so on, but somehow I am doing something wrong.

The topLevel CMakeList.txt It would be cool if I could add different build targets in there, 1 to build the tests and one to build the application.

Is there anyone that can give me some pointers, maybe a git-repo with some boilerplate or something?

Flip Vernooij
  • 889
  • 6
  • 15
  • 1
    Normally, unit tests are developed to test a function/class implemented in the object file or a library. But in your project I don't see a separate object file or a library: all you have is an **executable**. So, **what** is tested in your case? I don't see how a directory layout is involved into the problem. – Tsyvarev Oct 05 '21 at 21:28
  • I updated the directory structure, there might be some more source files, but this should get the idea across. The file that I want to test "DataSources/Usb/Reader.cpp" uses other files in the tree, mainly the Models/*Model.cpp files. – Flip Vernooij Oct 05 '21 at 21:44
  • So you can create a test executable from two files: implementation file - `Reader.cpp` - and a test file - `ReaderTest.cpp`. And you could do that with almost any directory layout. So, what is a **problem**? There is no *universal* "best" directory layout. If you don't have detailed requirements about your project and its workflow, you could choose almost any layout. But once you have all these details.. you will find the best layout by yourself. – Tsyvarev Oct 05 '21 at 22:22
  • I kind of face multiple problems, mainly lack of experience. Where do I create the test-executable, in the topLevel CMakeList or the one in /tests? Reader.cpp uses All the models, so I should build them to right, and the config include and every single dependency that it might use (in the future) So should I somehow build "everything" in the src dir and then link it to my /test env? And if so, can I still create an executable for my /src dir? – Flip Vernooij Oct 05 '21 at 22:35
  • I am looking at this, best resource I could find so far. https://cmake.org/cmake/help/latest/guide/tutorial/ – Flip Vernooij Oct 05 '21 at 22:58
  • 1
    "Where do I create the test-executable, in the topLevel CMakeList or the one in /tests?" - It is up to you where do you create test executables. "Reader.cpp uses All the models, so I should build them to right" - It depends on what kind of unit test you want to write. E.g. it is possible to isolate a single `.cpp` file by mocking all classes used by it. It looks like you don't know where to start in developing the tests. Stack Overflow is usually a bad place for *where-to-start* sort of questions. – Tsyvarev Oct 05 '21 at 23:06
  • Yeah I couldn't agree more, it is just very hard to find any good resources. Starting of with Qt gives you a huge head-start but at the same time you sort of skip over the basics. I am following the Cmake tutorial now, guess I just need to make time for that. Do it step by step... and once finished have another look to my project. Tnx for your help! – Flip Vernooij Oct 05 '21 at 23:51

0 Answers0