在VS2010 C ++中使用CppUnit进行单元测试多类 [英] Unit test multi class with CppUnit in VS2010 C++

查看:116
本文介绍了在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

推荐答案

使用cppunit您可以为要编写的每个测试编写一个从CppUnit::TestCase派生的类,并在runTest()中实现该测试.因此,对于每个行为您的课程展示,您都会想要一个从CppUnit::TestCase派生的课程.

第一次使用CppUnit时,我会尽量避免使用固定装置-它们会模糊问题,直到您确定如何自己编写测试为止. Sourceforge [
With cppunit you write a class derived from CppUnit::TestCase for each test you want to write and implement the test in runTest(). So for every behaviour your classes exhibit you''re going to want a class derived from CppUnit::TestCase.

For a first time use of CppUnit I''d try and avoid fixtures - they tend to blur the issue until you''re sure about how to write the tests themselves. Sourceforge[^] has got some examples of how to use it with and without fixtures. Note that they''re using one fixture for each class under test and one TestCase for each behaviour they''re testing.

Personally I think CppUnit is rather overcomplicated for what it does. It strains C++ in ways it wasn''t meant to go. As an alternative you could always avoid using an off the shelf unit testing framework and just use free functions with some handcrafted asserts.

For example I use:
#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进行单元测试多类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆