2012-12-13 6 views
0

저는 C++로 작성된 프로그램을 가지고 있으며 변경 작업을 할 때 아무 것도 깨지 않았는지 확인하기 위해 단위 테스트를 추가하고 싶습니다.매크로로 만든 단위 테스트 방법

우리는 자주 사용되는 특정 개체를 만들기 위해 매크로를 사용했습니다.

: 우리가 switchpoints를 포함하는 클래스의 생성자에서 매크로

class Classname 
{ 
    private: 
     PROPERTY_SWITCHPOINT(SwitchpointObject) 
} 

를 호출 switchpoints 우리는 다음 수행을 포함하는 클래스의 헤더에서

#define PROPERTY_SWITCHPOINT(var) \ 
private: \ 
    comp::SwitchPoint* m##var; \ 
public: \ 
    void set##var(bool val, unsigned short switchIdx = 0) \ 
    {\ 
     if(m##var) m##var->setValue(val,switchIdx); \ 
    }\ 
    bool get##var() \ 
    {\ 
     return (NULL == m##var ? false : m##var->getValue()); \ 
    }\ 
    comp::SwitchPoint* get##var##Ptr() \ 
    {\ 
     return m##var; \ 
    } 

:이 같을 것이다

Classname::Classname() 
{ 
    mSwitchpointObject = CreateSwitchpoint("Switchpoint name", 2); 
} 

comp::SwitchPoint* Classname::CreateSwitchpoint(const std::string& name, unsigned short numberOfSwitches = 1) 
{ 
    comp::SwitchPoint* sp = new comp::SwitchPoint(name, true, numberOfSwitches); 
    return sp; 
} 

이제이 개체의 값을 얻기 위해 mSwitchpointObject->getValue()을 사용할 수 있습니다. 이 모든 것은 작동하지만 unittest ++ 프레임 워크를 사용하고있는 단위 테스트를 만들 수는 없습니다. 나는이 시험에 그것을 시도 :

#include "UnitTest++.h" 
#include "Classname.h" 

namespace 
{ 
    TEST(SwitchpointTest1) 
    { 
     PROPERTY_SWITCHPOINT(SwitchpointObject) 
     mSwitchpointTestVariabele   = CreateSwitchpoint("test switchpoint", 2); 
     CHECK_EQUAL(mSwitchpointTestVariabele->getValue(), true); 
     //mSwitchpointTestVariabele->setValue(false, 0); 
     //CHECK_EQUAL(mSwitchpointTestVariabele->getValue(), false); 
     //mSwitchpointTestVariabele->setValue(false, 1); 
     //CHECK_EQUAL(mSwitchpointTestVariabele->getValue(), false); 
     //mSwitchpointTestVariabele->setValue(true, 0); 
     //CHECK_EQUAL(mSwitchpointTestVariabele->getValue(), false); 
     //mSwitchpointTestVariabele->setValue(true, 1); 
     //CHECK_EQUAL(mSwitchpointTestVariabele->getValue(), true); 
    } 
} 

그러나 그것은 나에게 오류를 컴파일러 제공 :

| |In member function 'virtual void<unnamed>::TestSwitchpointTest1::RunImpl() const':| 
| 9|error: expected primary-expression before 'private'| 
| 9|error: expected ';' before 'private'| 
| 9|error: expected primary-expression before 'public'| 
| 9|error: expected ';' before 'public'| 
| 9|error: a function-definition is not allowed here before '{' token| 
| 9|error: a function-definition is not allowed here before '{' token| 
|10|error: 'mSwitchpointTestVariabele' was not declared in this scope| 
|10|error: 'CreateSwitchpoint' was not declared in this scope| 
| |=== Build finished: 8 errors, 0 warnings ===| 

나는 문제가 매크로를 테스트해야하는 코드의 일부를 생성하는 것입니다 추측과를 유닛 테스트는이 파트가 생성되기 전에 수행되지만 Aleguna의 회신을 통해 이것이 문제가 될 수 없다는 것을 이해합니다.

이와 같은 코드 테스트는 어떻게 작성해야합니까?

+1

오류가 발생합니까? –

+1

unittest ++을 어떻게 사용했는지 우리에게 알려주지 않고 실수로 어떤 실수를했는지 말해 주시겠습니까? – PlasmaHH

+0

매크로는 컴파일러가 시작되기 전이라도 확장되었으므로 단위 테스트 런타임은 물론 –

답변

0

컴파일 문제가있어 실제로 어떤 코드가 생성되었는지 살펴볼 것을 권하고 싶습니다. 매크로가 예상 한 코드를 생성하지 못하는 것처럼 들립니다. Windows/VS를 사용하는 경우 "cl.exe/E"또는 g ++/gcc에서 "gcc -E source.cpp"를 사용하여 전 처리기를 실행하고 컴파일 할 C++ 파일을 생성 할 수 있습니다.

단위 테스트는 코드가 성공적으로 컴파일 된 후에 만 ​​실행할 수 있습니다 (단위 테스트는 다른 코드처럼 테스트 할 수 있습니다).

귀하의 단위 테스트 코드는 다음에 확대 :

#include "UnitTest++.h" 
    #include "Classname.h" 

    namespace 
    { 
     TEST(SwitchpointTest1) 
     { 
       private: 
     comp::SwitchPoint* mSwitchpointObject; 
    public: 
     void setSwitchpointObject(bool val, unsigned short switchIdx = 0) 
      { 
       if(mSwitchpointObject) mSwitchpointObject->setValue(val,switchIdx); 
      } 
     bool getSwitchpointObject() 
     { 
      return (NULL == mSwitchpointObject ? false : mSwitchpointObject->getValue()); 
     } 
     comp::SwitchPoint* getSwitchpointObject##Ptr() 
     { 
      return mSwitchpointObject; 
     } 


mSwitchpointTestVariabele   = CreateSwitchpoint("test switchpoint", 2); 
     CHECK_EQUAL(mSwitchpointTestVariabele->getValue(), true); 
     //mSwitchpointTestVariabele->setValue(false, 0); 
     //CHECK_EQUAL(mSwitchpointTestVariabele->getValue(), false); 
     //mSwitchpointTestVariabele->setValue(false, 1); 
     //CHECK_EQUAL(mSwitchpointTestVariabele->getValue(), false); 
     //mSwitchpointTestVariabele->setValue(true, 0); 
     //CHECK_EQUAL(mSwitchpointTestVariabele->getValue(), false); 
     //mSwitchpointTestVariabele->setValue(true, 1); 
     //CHECK_EQUAL(mSwitchpointTestVariabele->getValue(), true); 
    } 
} 
+0

또한 단위 테스트에서 PROPERTY_SWITCHPOINT는 자신이 생각하는대로 수행하지 않습니다. 매크로를 확장하여 클래스 내부를 TEST (SwitchPointtest1) 네임 스페이스에 덤프합니다. – jeremy

+0

좋은 생각이었습니다. 나는 이중 개인 또는 공개 키워드를 찾을 수 없지만 Aleguna가 언급 한 것을 확인했습니다. – Nico

+0

ClassName.h를 포함하기 때문에 매크로를 다시 추가하지 않고 해당 개체에 액세스 할 수 있어야합니다. – jeremy

0

음 오류가

In member function 'virtual void<unnamed>::TestSwitchpointTest1::RunImpl() const': 

당신은 사용할 수 없습니다 꽤 명백하다 키워드 (매크로에 포함) private:public: 함수 본문에.

+0

나는 Jeremy의 충고를 따랐다. 그리고 너는 옳았다.시험에서 매크로를 호출 할 수있는 방법이 있습니까? – Nico

+0

코드가 이미 생성되었으므로 매크로 줄을 제거하면됩니다. PROPERTY_SWITCHPOINT (SwitchpointObject) – jeremy