Can unity C unittest framework be used to unittest C++ code?

774 Views Asked by At

I'm using the unity framework to unittest my C code on an AtMega32A. This works great. now I wonder if I can somehow trick unity to also test my C++ code.

I've made a small proof of concept program that exposes all the members of the C++ class via a public function. Now I would like to put a number of assert macro's in that public function. The program can be found here: https://github.com/cdwijs/cpp_unit_test

In my main I have the following:

int main(void)
{
    UNITY_BEGIN();
    //RUN_TEST(myPrivate.executeTest); //unity_internals.h(707,65): error: invalid use of non-static member function
    //RUN_TEST((void*)myPrivate.executeTest); //error: invalid use of member function (did you forget the '()' ?)
    //RUN_TEST((func)))myPrivate.executeTest); //error: 'func' was not declared in this scope
    //RUN_TEST(&myPrivate.executeTest); //error: cannot convert 'void (Private::*)()' to 'UnityTestFunction {aka void (*)()}' for argument '1' to 'void UnityDefaultTestRun(UnityTestFunction, const char*, int)'
    UNITY_END();

    myPrivate.more();
    myPrivate.more();
    myPrivate.less();

    /* Replace with your application code */
    while (1)
    {
    }
}

And in Private_test.cpp I have the following:

#include "private.h"
#include "unity.h"

void Private::executeTest (void)
{
    myNumber = 3;
    TEST_ASSERT_EQUAL_INT(3,myNumber);
    more();
    TEST_ASSERT_EQUAL_INT(4,myNumber);
}

I can't figure out howto run the test function. Any Idea's? Am I looking in the correct direction, or should I use a test framework that is specifically aimed at C++?

0

There are 0 best solutions below