Unit tests for classes in audio plugin


I want to start adding unit tests for classes in my plugin. For example, here is a UnitTest class I've created:


#include "../JuceLibraryCode/JuceHeader.h"
#include "EnumFloatParameter.h"
class EnumFloatParameterTest : public UnitTest
    EnumFloatParameterTest() : UnitTest("EnumFloatParameterTest") {}
    void runTest()
        beginTest("Test enumerated parameters");
        const String percussion[] = { "Off", "Bass drum", "Snare", "Tom", "Cymbal", "Hi-hat" };
        int n = sizeof(percussion) / sizeof(String);
        EnumFloatParameter* efp = new EnumFloatParameter("Percussion Mode", StringArray(percussion, n));
        for (int i = 0; i < n; i++) {
            expect(i == efp->getParameterIndex());
static EnumFloatParameterTest enumTest;

The test class is built along with the rest of the plugin. But how do I run it? As a VST, my code is built into a .dll file. Is there some way in the Introjucer to specify a Unit Test target or something like that?


If you only want to run the tests occasionally, just add a #if to your code that you can enable when you want to test them. Or if the tests are quick, maybe just run them as soon as the plugin loads in a debug build?

(Your code looks a bit odd, BTW, can't really see what you're trying to achieve by testing the parameter index like that)


Thanks for the reply Jules.

What I should have asked was, how do I use the UnitTestRunner class? And if the test is run at plugin load, how do I see the results?

About the test - the class converts the floating point values used in VST to/from an enum value. I found a bug in my code related to rounding, and this unit test covers that.

Thanks again.


Maybe have a look at the way the juce demo app runs its unit tests?


I've just gotten started with JUCE and was wondering the same. I think it should be pretty simple to add a build target that just compiles a command-line binary with a main that runs your unit tests - I figure this is a lot easier than actually having to build test functionality into a VST target.

The problem is, I tried adding such a target in XCode, but then as soon as I re-ran the Introjucer it deleted my new unit test target. So I guess that's not much of an option :-/


Unit tests running at build

Yeah, I tried the same thing, except my target was configured in the introjucer. That way it will be generated as part of the project. The problem is though, you can’t make a different “type” of build. It is still an audio plugin target.


I just figured out a way around this: I made a 2nd Introjucer project that's a command line target, but it points to the same files as my plugin target except with its own main. It's not as elegant as if you could make a command line target in a plugin project, but it seems to work well enough.