Google的C++ Testing Framework发布已有一段时间。最近大致阅读了一下相关的文档,还尝试着些了两个简单的测试代码。其大致的思想跟Java中的JUnit,Python的pyunit一致。这些测试框架理念的最初来源,貌似是Simple Smalltalk Testing: With Patterns。Samlltalk的语言的测试框架。所以google的c++测试框架就是这种理念的c++实现。由此我们也可以看出,理念或者是老外比较喜欢用的philosophy才是最重要的,语言只是表达工具而已。

Simple Smalltalk Testing: With Patterns中,存在测试框架中的基本元素有以下几种:

Pattern Purpose
Fixture Create a common test fixture.
Test Case Create the stimulus for a test case.
Check Check the response for a test case.
Test Suite Aggregate TestCases.

check是测试框架中最小的模式,在google test中用assertion实现,就是对一些条件判断语句,返回错误的类型和相关信息。很多check就可以组合成一个Test Case(测试用例)。很多的测试用例就可以组合成一个Test Suite。但如果很多不少的测试Suite想测试同样的数据,比如一个数据对象,这时候,我们就需要Fixture(测试套件)。它会定义对象的建构和析构的操作。使得整个测试Suite基于相同的对象。

另外,还需要启动整个测试过程。称之为test runner。在google test中,由RUN_ALL_TESTS()来实现,它会启动所有的测试,不同的测试Suite之间不产生任何影响。一个测试的错误不会导致整个测试的结束。这样就可以保证整个测试的顺利完成。

大致的philosophy就是这样子的。为了保证代码的质量和更好地提高工作效率,多写些测试代码是非常有必要的。

Tags: .
首页

No Comments Now!

Be the first to comment on this entry.

请您留下评论

名称(必填)
Mail (必填),(will not be published)
网站(recommended)

Fields in bold are required. Email addresses are never published or distributed.

Some HTML code is allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
URLs must be fully qualified (eg: http://www.hongzhixiong.com),and all tags must be properly closed.

Line breaks and paragraphs are automatically converted.

Please keep comments relevant. Off-topic, offensive or inappropriate comments may be edited or removed.