本文介绍了在VS2010 C ++中使用CppUnit进行单元测试多类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我在VS 2010上用C ++编写了一个程序,现在我准备使用CppUnit编写UnitTest(UT).我想将每个类的UT分别编写为类,并在主函数中调用UT.
例如:我有班:A(具有功能:A1,A2,A3,A4),
B(具有功能:B1,B2,B3),
C(具有功能:C1,C2,C3,C4)
因此,我将有3个UT类,并且将在主函数中调用3UT类.

我知道,如果只在一个类中编写UT,那很容易,而且我可以做到.但我的要求要有3个班级.所以我遇到了困难.

希望我能得到您的帮助.

在此先感谢

Hi all,

I wrote a program by C++ on VS 2010, and now I prepare to write UnitTest(UT) using CppUnit. I want to UT of each class is written to separately classes and that UTs is called in a main function.
EX: i have classes : A(with function: A1,A2,A3,A4),
B(with function: B1,B2,B3),
C(with function: C1,C2,C3,C4)
So I will have 3 UT class and 3UT class will be called in a main function.

I know that if I write UTs in a only class, it is easy and I can do it. But my requirement want to have 3 class. So I am meeting difficult with this.

I hope that I will receive your help.

Thanks in advance

推荐答案

#define ASSERT_THAT(expr) \
    if(!(expr)) error_to_console( #expr, __FILE__, __LINE__ );

其中,error_to_console实现为:

void error_to_console(  const std::string &error_message,
			const std::string &file_name,
			long  line_no )
{
	std::cerr << file_name << "(" << line_no << ") : error : "
			  << error_message << std::endl;
}

然后您可以编写如下测试:

Then you can write tests like:

void require_string_to_implement_equality()
{
    ASSERT_THAT( std::string( "ABC" ) == std::string( "ABC" ) );
}

在我看来,这比搞CppUnit测试用例,测试和固定装置要容易得多.

值得探索其他单元测试框架. CppUnit的原始作者Michael Feathers开发了一个较小的,较简单的框架,称为 CppUnitLite [ ^ ].坦率地说,这可能会完成您想开始的所有事情,而没有任何实际的文档.但是该代码很容易阅读,并且网络上有很多示例.

which to my mind is a lot easier than messing about with CppUnit test cases, tests and fixtures.

It''s worth searching out other unit testing frameworks. Michael Feathers, the original author of CppUnit, developed a smaller, less complicated framework called CppUnitLite[^]. This probably does everything you want to start out, admittedly without any real documentation. However the code''s easy enough to read AND there are plenty of examples on the web.


这篇关于在VS2010 C ++中使用CppUnit进行单元测试多类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-25 08:24