CPPUnit 如何编写测试?
好吧,我基本上想开始工作并编写一些 CPPUnit 测试,但我不知道如何去做。这里我有一些代码,基本上获取指向关联按钮组和位置参数的菜单按钮的指针,我将如何为此创建测试?
CMenuButton* CMenuContainer::GetButton(const enumButtonGroup argGroup, const int32_t argPosition)
{
CMenuButton* pButton = NULL;
if (argGroup < MAX_GROUP_BUTTONS)
{
pButton = m_ButtonGroupList[argGroup].GetButton(argPosition);
}
return pButton;
回复@Fabio Ceconello,是否可以为这样的代码设置一些测试?
unsigned long CCRC32::Reflect(unsigned long ulReflect, const char cChar)
{
unsigned long ulValue = 0;
// Swap bit 0 for bit 7, bit 1 For bit 6, etc....
for(int iPos = 1; iPos < (cChar + 1); iPos++)
{
if(ulReflect & 1)
{
ulValue |= (1 << (cChar - iPos));
}
ulReflect >>= 1;
}
return ulValue;
}
Okay I basically want to get the ball rolling and write some CPPUnit tests but I have no idea how to go about it. Here I have some code that basically gets a pointer to the Menu Button for the associated button group and position arguments, how would I go about creating a test for this?
CMenuButton* CMenuContainer::GetButton(const enumButtonGroup argGroup, const int32_t argPosition)
{
CMenuButton* pButton = NULL;
if (argGroup < MAX_GROUP_BUTTONS)
{
pButton = m_ButtonGroupList[argGroup].GetButton(argPosition);
}
return pButton;
In reply to @Fabio Ceconello, would it be possible to set some tests for some code like this?
unsigned long CCRC32::Reflect(unsigned long ulReflect, const char cChar)
{
unsigned long ulValue = 0;
// Swap bit 0 for bit 7, bit 1 For bit 6, etc....
for(int iPos = 1; iPos < (cChar + 1); iPos++)
{
if(ulReflect & 1)
{
ulValue |= (1 << (cChar - iPos));
}
ulReflect >>= 1;
}
return ulValue;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
CppUnit 不太适合为用户界面创建自动化测试。它更适合仅处理单元。例如,假设您创建了
std::vector
的替代品,并希望确保它的行为与原始实现类似,您可以编写向您的实现和标准实现添加元素的测试,然后执行以下操作:进行更多处理(删除、更改元素等),并在每个步骤之后检查两者是否具有一致的结果。对于 UI,我不知道有什么好的开源/免费工具,但一个好的商业工具是 来自 Smart Bear 的 TestComplete 等。
对于您给出的第二个示例,第一件事是为 Reflect() 方法定义有效性检查。例如,您可以手动计算某些值的结果,以检查每个值的返回值是否符合预期。或者您可以使用已知完全有效的反函数。
假设第一个选项,您可以像这样编写测试:
CppUnit isn't well suited for creating automated tests for user interface. It's more for processing-only units. For instance, let's say you created a replacement for
std::vector
and want to make sure it behaves like the original one, you could write tests that add elements to both your and the standard implementation, then do some more handling (removing, changing elements, etc.) and after each step check if the two have a consistent result.For UI I'm not aware of good open source/free tools, but one good commercial tool is TestComplete from Smart Bear, among others.
For the second example you gave, the first thing is to define a validity check for the Reflect() method. You can, for instance, calculate the result of some values by hand to check if the returned value for each of them is what was expected. Or you could use an inverse function that's known to be fully working.
Assuming the first option, you could write the test like this: