1

I want to make global classes because I want to do the same initialize across my tests. I tried like that, I've mutiples errors like ambiguous access. Someone have an idea ?

#include <CppUnitTest.h>

using namespace Microsoft::VisualStudio::CppUnitTestFramework;

TEST_CLASS(GLOBAL_TEST)
{
public:
    TEST_METHOD_INITIALIZE(initialize)
    {
        Logger::WriteMessage("INITIALIZE");
    }
};

TEST_CLASS(ClassA), public GLOBAL_TEST
{
public:
    TEST_METHOD(ClassA_Test1)
    {
        Logger::WriteMessage("ClassA_Test1");
    }
};

My errors :

Code    Description
C2385   ambiguous access of '__GetTestClassInfo'
C2385   ambiguous access of '__GetTestVersion'  
C2594   'static_cast': ambiguous conversions from 'void (__cdecl     ClassA::ClassA::* )(void)' to 'Microsoft::VisualStudio::CppUnitTestFramework::TestClassImpl::__voidFunc'
Cylexx
  • 346
  • 1
  • 10

1 Answers1

1

The TEST_ macros don't support inheritance, but your base class could just be defined as a normal class containing an Initialize() method. You'd still have to define a TEST_METHOD_INITIALIZE function in every derived class though.

#include <CppUnitTest.h>

using namespace Microsoft::VisualStudio::CppUnitTestFramework;

class GLOBAL_TEST
{
public:
    void Initialize()
    {
        Logger::WriteMessage("INITIALIZE");
    }
};

TEST_CLASS(ClassA), public GLOBAL_TEST
{
public:
    TEST_METHOD_INITIALIZE(MethodInitialize)
    {
        Initialize();
    }

    TEST_METHOD(ClassA_Test1)
    {
        Logger::WriteMessage("ClassA_Test1");
    }
};
Chris Olsen
  • 3,243
  • 1
  • 27
  • 41
  • Thanks for your answer, this kind of code is already used but I wanted to "hide" this part because it's often omited (and i've to review all unit tests). – Cylexx Mar 02 '20 at 14:38