首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >由宏创建的单元测试方法

由宏创建的单元测试方法
EN

Stack Overflow用户
提问于 2012-12-13 23:19:39
回答 2查看 2.5K关注 0票数 0

我有一个用C++编写的程序,为了确保我们在进行更改时不会破坏任何东西,我想添加单元测试。

在程序中,我们使用宏来创建某些经常使用的对象。这看起来像这样:

代码语言:javascript
复制
#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; \
   }

在包含开关点的类的头中,我们调用宏

代码语言:javascript
复制
class Classname
{
    private:
        PROPERTY_SWITCHPOINT(SwitchpointObject)
}

在包含切换点的类的构造函数中,我们将执行以下操作:

代码语言:javascript
复制
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++框架。我在下面的测试中尝试过:

代码语言:javascript
复制
#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);
    }
}

但这给我带来了编译器错误:

代码语言:javascript
复制
|  |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的回复中我理解这不可能是问题所在。

我应该如何为这样的代码编写测试呢?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-12-14 00:39:47

我建议看看实际生成了什么代码,因为你有一个编译问题,听起来宏并没有生成你期望的代码。如果你使用的是windows/VS,你可以使用"cl.exe /E“或在g++/gcc中,你可以使用”windows -E source.cpp“来运行预处理器并生成将要编译的c++文件。

单元测试只能在代码成功编译后运行(并且可以像对任何其他代码进行单元测试一样进行测试)。

您的单元测试代码将扩展为:

代码语言:javascript
复制
 #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
EN

Stack Overflow用户

发布于 2012-12-14 00:43:48

这个错误是显而易见的

代码语言:javascript
复制
In member function 'virtual void<unnamed>::TestSwitchpointTest1::RunImpl() const':

您不能在函数体中使用private:public: (包含在宏中)关键字。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13862910

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档