4

저는 C++ 테스트 기반 개발을하고 있습니다. 나는 같은 일을하는 일련의 수업을 가지고있다.C++ 단위 테스트 테스트, 템플릿 테스트 클래스 사용

동일한 입력이 동일한 출력을 제공합니다 (또는 테스트해야합니다). Visual Studio 2012의

CppUnitTestFramework를 사용하고 있습니다. 템플릿 테스트 클래스를 만들고 싶었으므로 테스트를 한 번 작성하고 필요에 따라 클래스를 템플릿으로 만들 수 있지만이 방법은 찾을 수 없습니다. 내 목표 :

/* two classes that do the same thing */ 
class Class1 
{ 
    int method() 
    { 
     return 1; 
    } 
}; 

class Class2 
{ 
    int method() 
    { 
     return 1; 
    } 
}; 

/* one set of tests for all classes */ 
template< class T> 
TEST_CLASS(BaseTestClass) 
{ 
    TEST_METHOD(testMethod) 
    { 
     T obj; 

     Assert::AreEqual(1, obj.method()); 
    } 
}; 

/* only have to write small amout to test new class */ 
class TestClass1 : BaseTestClass<Class1> 
{ 
}; 

class TestClass2 : BaseTestClass<Class1> 
{ 
}; 

CppUnitTestFramework를 사용하여이 작업을 수행 할 수있는 방법이 있습니까?

이렇게 할 수있는 또 다른 단위 테스트 프레임 워크가 있습니까?

답변

2

CppUnitTestFramework, 내가 익숙하지있는 이 할 수있는 방법이 있는지 모르겠지만, 뭔가 당신이 확실히 이 googletest 에서 할 수있는은 (클래스의 임의의 목록을 지정하고 프레임 워크를 생성 이있다 템플릿에 따라) 모든 테스트가 동일합니다. 제 생각에 이 적합하다고 생각합니다.

googletest를 소스 here으로 다운로드 할 수 있습니다. 당신이 원하는 것

관용구는 다음과 같습니다

typedef ::testing::Types</* List of types to test */> MyTypes; 
... 
TYPED_TEST_CASE(FooTest, MyTypes); 
... 
TYPED_TEST(FooTest, DoesBlah) { 
    /* Here TypeParam is instantiated for each of the types 
     in MyTypes. If there are N types you get N tests. 
    */ 
    // ...test code 
} 

TYPED_TEST(FooTest, DoesSomethingElse) { 
    // ...test code 
} 

공부한다 primersamples. 내가 인터페이스와 그것의 여러 구현을 가지고 : 그리고 또한 나는 비슷한 문제가 있었다 More Assertions

+1

답변에 대한 환호성. 이 googletest를 보는 사람은 조금만 가고 가야합니다. 나는 이들을 조합하여 http://blog.knatten.org/2012/09/26/installing-and-using-googletest-with-visual-studio/ 및 http://stackoverflow.com/questions/531941에 사용했습니다./how-to-setup-google-c-testing-framework-gtest-on-visual-studio-2005를 참조하십시오. 컴파일러를 프로젝트 속성에 2010 버전으로 설정하지 않으면 Visual Studio 2012에서 컴파일되지 않습니다. –

0

을 확인 Typed Tests

에 대한 AdvancedGuide 로 이동합니다. 물론 인터페이스에 대한 테스트 만 작성하고 싶습니다. 또한 각 구현에 대한 테스트를 복사하고 싶지 않습니다.

글쎄, 내 솔루션은별로 예쁘지 않지만 간단하고 지금까지 생각 해낸 유일한 솔루션입니다.

Class1과 Class2에 대해 동일한 작업을 수행 한 다음 각 구현에 대해보다 특수화 된 테스트를 추가 할 수 있습니다.

setup.cpp

#include "stdafx.h" 

class VehicleInterface 
{ 
public: 
    VehicleInterface(); 
    virtual ~VehicleInterface(); 
    virtual bool SetSpeed(int x) = 0; 
}; 

class Car : public VehicleInterface { 
public: 
    virtual bool SetSpeed(int x) { 
     return(true); 
    } 
}; 

class Bike : public VehicleInterface { 
public: 
    virtual bool SetSpeed(int x) { 
     return(true); 
    } 
}; 


#define CLASS_UNDER_TEST Car 
#include "unittest.cpp" 
#undef CLASS_UNDER_TEST 


#define CLASS_UNDER_TEST Bike 
#include "unittest.cpp" 
#undef CLASS_UNDER_TEST 

unittest.cpp

#include "stdafx.h" 
#include "CppUnitTest.h" 

#define CONCAT2(a, b) a ## b 
#define CONCAT(a, b) CONCAT2(a, b) 

using namespace Microsoft::VisualStudio::CppUnitTestFramework; 


TEST_CLASS(CONCAT(CLASS_UNDER_TEST, Test)) 
{ 
public: 
    CLASS_UNDER_TEST vehicle; 
    TEST_METHOD(CONCAT(CLASS_UNDER_TEST, _SpeedTest)) 
    { 
     Assert::IsTrue(vehicle.SetSpeed(42)); 
    } 
}; 

당신은 빌드에서 "unittest.cpp"를 제외해야합니다.