# Using Catch2 but need a MessageLoop

**URL:** <https://forum.juce.com/t/using-catch2-but-need-a-messageloop/41380>\
**Category:** General JUCE discussion\
**Created:** [August 22, 2020, 8:56pm UTC](https://forum.juce.com/t/using-catch2-but-need-a-messageloop/41380 "2020-08-22T20:56:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tobante](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/tobante/32/9644_2.png) [@tobante](https://forum.juce.com/u/tobante)\
**Post date:** [August 22, 2020, 8:56pm UTC](https://forum.juce.com/t/using-catch2-but-need-a-messageloop/41380/1 "2020-08-22T20:56:44Z")

</div>

I’m using Catch2 as my unit test framework, which for most cases has worked pretty good so far. Some JUCE classes need a running MessageLoop to work properly, so need to create one on startup of the test executable. From what I can tell, I have to options:

## 1

Use a JUCE GuiApplication as the test runner. Catch2 needs argc & argv, so I would need to get them from somewhere. Not sure how. Since it would be a GUI application, I would also need extra setup on the docker test runners to fake a display.

## 2

Manually start the MessageLoop. This is what I’ve been doing so far, but I’m not sure if I’m doing it correctly:

```auto
#define CATCH_CONFIG_RUNNER
#include "catch2/catch.hpp"

int main(int argc, char* argv[])
{
    auto gui = juce::ScopedJuceInitialiser_GUI {};
    return Catch::Session().run(argc, argv);
}

```

Ideally I’d like to be able to test plugins this way, so setting parameters & running the processBlock method. I’ve had some weird results where I was setting a parameter to a new value but it still contained the old value when checking. Any suggestions?

---

<div class="post-metadata">

**Author:** ![denvercoder9](https://avatars.discourse-cdn.com/v4/letter/d/b782af/32.png) [@denvercoder9](https://forum.juce.com/u/denvercoder9)\
**Post date:** [August 23, 2020, 1:00am UTC](https://forum.juce.com/t/using-catch2-but-need-a-messageloop/41380/2 "2020-08-23T01:00:53Z")

</div>

What you are doing in method number 2 is essentially what I do (I use google test though not catch).

If you are using cached values then that might explain the issue with the parameter not updating. I have had some issues dealing with value tree updates and cached values in my unit tests. If you are using cached values forcing the update of the cached value might work. You might take a look at [this thread](https://forum.juce.com/t/cachedvalue-not-updated-how-to-use-correctly/34993) and check out Daves answer. I ended up using an async updater and calling handleUpdateNowIfNeeded in my tests and that seemed to work well.

---

<div class="post-metadata">

**Author:** ![sudara](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/sudara/32/8071_2.png) [@sudara](https://forum.juce.com/u/sudara)\
**Post date:** [December 7, 2020, 5:56pm UTC](https://forum.juce.com/t/using-catch2-but-need-a-messageloop/41380/3 "2020-12-07T17:56:09Z")

</div>

I appreciated this thread!

Even trivial tests like this Catch2 example will hit `JUCE_ASSERT_MESSAGE_MANAGER_EXISTS` if there’s an APVTS involved in the processor’s constructor.

```auto
TEST_CASE("Plugin instance name", "[plugin]") {
    MyAudioProcessor testPlugin;
    CHECK_THAT( testPlugin.getName().toStdString(), Catch::Equals( "My Audio Processor" ) );
}

```

---

<div class="post-metadata">

**Author:** ![ImJimmi](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/imjimmi/32/10843_2.png) [@ImJimmi](https://forum.juce.com/u/ImJimmi)\
**Post date:** [June 30, 2021, 11:17am UTC](https://forum.juce.com/t/using-catch2-but-need-a-messageloop/41380/5 "2021-06-30T11:17:03Z")

</div>

+1 for method 2 here.

Previously I had put the `juce::ScopedJuceInitialiser_GUI` inside the `TEST_CASE` block but that seems to cause memory leaks.

Putting the GUI initialiser at the top of `main()` seems to have plugged the leaks.

---

<div class="post-metadata">

**Author:** ![benvining](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/benvining/32/9922_2.png) [@benvining](https://forum.juce.com/u/benvining)\
**Post date:** [March 31, 2023, 9:43pm UTC](https://forum.juce.com/t/using-catch2-but-need-a-messageloop/41380/6 "2023-03-31T21:43:58Z")

</div>

I wonder why it would cause leaks… The RAII mechanism should take care of it, but perhaps catch’s `TEST_CASE` expands to some kind of lambda with weird scoping/capturing or something…
