Optimal strategy for including Catch2 tests?

We’re trying to include Catch2 tests. This was my intended strategy (MacOS, XCode / AppCode):

  1. Create new folder source/tests.
  2. Add catch.hpp and tests.cpp to folder.
  3. Create new target tests.
  4. Set header search paths for tests to mimic those of other plugin targets.
  5. tests.cpp:
#include "catch.hpp"
#include <JuceHeader.h>
#include "project_module_to_test.cpp"

... Tests ...
  1. Create build config for tests target.
  2. Build and run.

Unfortunately this gives an error: No global header file was included!. This references JuceHeader.h which suggests silencing the error using #define JUCE_GLOBAL_MODULE_SETTINGS_INCLUDED.
Attempted fix: Add this to the top of tests.cpp.
Result: Lots of linker errors.

At this point I notice that the plugin targets include (in build phases) the JUCE implementation files from JUCE Library Code as source.
Attempted fix: Add these files to the source for the tests target.
Result: Lots more errors including (mysteriously) the return of the initial No global header file error.

At this point I decide that there is something going way, way, way over my head.

Is this a completely misguided way of configuring Catch2 tests for JUCE or is there something small I’m missing?

Cheers!

1 Like

Did you ever find a solution to this? I’ve seen this issue both trying to make XCTest and Catch2 work with JUCE.

We use Catch2 with a JUCE CMake based project, which works well, but it was not completely straightforeard to set up. See Setting up a Catch2 Test target with CMake

1 Like

Here’s how I’ve been testing with Catch2 and the projucer. It is pretty hacky, and there is probably a better way to do this. It also doesn’t exactly answer your question, but it is a way that works.

In addition to my main projucer project, I create a projucer command-line test project (do this by selecting “Console” for the project’s “Application” setting).

Then, I add all of the files from my original project to my test project (to do this, click the ‘+’ button in the file explorer, and select add existing files).

I also add Catch to this test project. Then you can include tests in the test projects “Main.cpp”. Then, when you build + run the test project, the tests will be run.

You can’t just create a new target in your IDE, because projucer will automatically delete them (I think) ;(

In case someone wants to peek at a CMake/Catch2 setup, you can inspect my plugin template repo (including Catch2 tests running on Github Actions)