저는 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의 회신을 통해 이것이 문제가 될 수 없다는 것을 이해합니다.
이와 같은 코드 테스트는 어떻게 작성해야합니까?
오류가 발생합니까? –
unittest ++을 어떻게 사용했는지 우리에게 알려주지 않고 실수로 어떤 실수를했는지 말해 주시겠습니까? – PlasmaHH
매크로는 컴파일러가 시작되기 전이라도 확장되었으므로 단위 테스트 런타임은 물론 –