CppUnit 快速使用指南

上一篇 / 下一篇  2007-11-25 20:33:38 / 个人分类:自动化测试

51Testing软件测试网u4hJW^_c2~i2[o

李群
5No1y%u;|;T02003 年 8 月 03 日51Testing软件测试网3T1F~ Qr-c(WBv*p w

IA8Ie Jo(}0本文从开发人员的角度,介绍 CppUnit 框架,希望能够使开发人员用最少的代价尽快掌握这种技术。下面从基本原理,CppUnit 原理,手动使用步骤,通常使用步骤,其他实际问题等方面进行讨论。以下讨论基于 CppUnit1.8.0。
_j2K)Q|k$^)u0背景

sq]-k6a:w051Testing软件测试网 Y!Ud!}L;j9_

CppUnit 是个基于 LGPL 的开源项目,最初版本移植自 JUnit,是一个非常优秀的开源测试框架。CppUnit 和 JUnit 一样主要思想来源于极限编程(XProgramming)。主要功能就是对单元测试进行管理,并可进行自动化测试。这样描述可能没有让您体会到测试框架的强大威力,那您在开发过程中遇到下列问题吗?如果答案是肯定的,就应该学习使用这种技术:

1E$b,_cC fXG0

ZF$l bq ? i:a0. 测试代码没有很好地维护而废弃,再次需要测试时还需要重写;51Testing软件测试网#_M3N*RGXR7G\^nb
. 投入太多的精力,找 bug,而新的代码仍然会出现类似 bug;51Testing软件测试网 _4hy8|$j:B
. 写完代码,心里没底,是否有大量 bug 等待自己;
U'w%O D0\ {$^ ?9QS0. 新修改的代码不知道是否影响其他部分代码;
3_*~9A+@F K,O0. 由于牵扯太多,导致不敢进行修改代码;
!~}e+m;lG/wN nl)N0...

f7x'T-rxY.|ce4kM051Testing软件测试网u _mI0L3Mm


;M?3VCbtf%J0这些问题下文都会涉及。这个功能强大的测试框架在国内的 C++ 语言开发人员中使用的不是很多。本文从开发人员的角度,介绍这个框架,希望能够使开发人员用最少的代价尽快掌握这种技术。下面从基本原理,CppUnit 原理,手动使用步骤,通常使用步骤,其他实际问题等方面进行讨论。以下讨论基于 CppUnit1.8.0。51Testing软件测试网~ u"vU]#I

$?Q4j^W01. 基本原理

?5ph,gC0

#@ j%vQ)We0对于上面的问题仅仅说明 CppUnit 的使用是没有效果的,下面先从测试的目的,测试原则等方面简要说明,然后介绍 CppUnit 的具体使用。51Testing软件测试网-vU9x9z,bY$]%s'zXb

^^_Y*M.p(F:y0首先要明确我们写测试代码的目的,就是验证代码的正确性或者调试 bug。这样写测试代码时就有了针对性,对那些容易出错的,易变的编写测试代码;而不用对每个细节,每个功能编写测试代码,当然除非有过量精力或者可靠性要求。51Testing软件测试网N)w'm,q T$Vr._

*@~2O?T0编码和测试的关系是密不可分的,推荐的开发过程并不要等编写完所有或者很多的代码后再进行测试,而是在完成一部分代码,比如一个函数,之后立刻编写测试代码进行验证。然后再写一些代码,再写测试。每次测试对所有以前的测试都进行一遍。这样做的优点就是,写完代码,也基本测试完一遍,心里对代码有信心。而且在写新代码时不断地测试老代码,对其他部分代码的影响能够迅速发现、定位。不断编码测试的过程也就是对测试代码维护的过程,以便测试代码一直是有效的。有了各个部分测试代码的保证,有了自动测试的机制,更改以前的代码没有什么顾虑了。在极限编程(一种软件开发思想)中,甚至强调先写测试代码,然后编写符合测试代码的代码,进而完成整个软件。51Testing软件测试网]x}#u:[&l

51Testing软件测试网/Ha} i3pr

根据上面说的目的、思想,下面总结一下平时开发过程中单元测试的原则:51Testing软件测试网;v#N7R ikK9sa2aL

!L:qj3]W@6e0先写测试代码,然后编写符合测试的代码。至少做到完成部分代码后,完成对应的测试代码;
0t| `_M\^V }0测试代码不需要覆盖所有的细节,但应该对所有主要的功能和可能出错的地方有相应的测试用例
*q)H~ ?(|}1~)B1O0Y0发现 bug,首先编写对应的测试用例,然后进行调试;51Testing软件测试网| l5[*b%te&Z y
不断总结出现 bug 的原因,对其他代码编写相应测试用例;
LAK/\:M%C,R&aM/f0每次编写完成代码,运行所有以前的测试用例,验证对以前代码影响,把这种影响尽早消除;51Testing软件测试网{1k4QaS!ivHh#w
不断维护测试代码,保证代码变动后通过所有测试;51Testing软件测试网j)r&Bu3f"Q
有上面的理论做指导,测试行为就可以有规可循。那么 CppUnit 如何实现这种测试框架,帮助我们管理测试代码,完成自动测试的?下面就看看 CppUnit 的原理。51Testing软件测试网9G3Y)O k.Cd y g5c"K

51Testing软件测试网2J$KLii#f0NS&|Z

2. CppUnit 的原理51Testing软件测试网.v/Qu pO`^? hN

[s-uzdT+|7J9[ D0在 CppUnit 中,一个或一组测试用例的测试对象被称为 Fixture(设施,下文为方便理解尽量使用英文名称)。Fixture 就是被测试的目标,可能是一个对象或者一组相关的对象,甚至一个函数。

F%cv6H.z7[051Testing软件测试网2\m6~ o"Ek5C*p

有了被测试的 fixture,就可以对这个 fixture 的某个功能、某个可能出错的流程编写测试代码,这样对某个方面完整的测试被称为TestCase(测试用例)。通常写一个 TestCase 的步骤包括:51Testing软件测试网4Y,W2eJ2{

51Testing软件测试网 f0JVx etN W

对 fixture 进行初始化,及其他初始化操作,比如:生成一组被测试的对象,初始化值;
0Y!@M(K-`ZN| q1c0按照要测试的某个功能或者某个流程对 fixture 进行操作;51Testing软件测试网b+|+m~ }8Vu(FGS%Q
验证结果是否正确;51Testing软件测试网wT2WRK
对 fixture 的及其他的资源释放等清理工作51Testing软件测试网9`%d h(?-\|
对 fixture 的多个测试用例,通常(1)(4)部分代码都是相似的,CppUnit 在很多地方引入了 setUp 和 tearDown 虚函数。可以在 setUp 函数里完成(1)初始化代码,而在 tearDown 函数中完成(4)代码。具体测试用例函数中只需要完成(2)(3)部分代码即可,运行时 CppUnit 会自动为每个测试用例函数运行 setUp,之后运行 tearDown,这样测试用例之间就没有交叉影响。51Testing软件测试网#BVuv2ht:D+U-J

:_;V1J/Y;Um&t&qO0对 fixture 的所有测试用例可以被封装在一个 CppUnit::TestFixture 的子类(命名惯例是[ClassName]Test)中。然后定义这个fixture 的 setUp 和 tearDown 函数,为每个测试用例定义一个测试函数(命名惯例是 testXXX)。下面是个简单的例子:

5UL6Bn5A0

9`*Sh I!r \$K8p051Testing软件测试网 p?&_6^*n {
class MathTest : public CppUnit::TestFixture {51Testing软件测试网+c!X6?B.HJ
protected:
f4v%Y#S}7G4i0    int m_value1, m_value2;
'I q(_8B![B+U0public:51Testing软件测试网7~d&@Am$j3g W
    MathTest() {}51Testing软件测试网o E u0o L#w
    // 初始化函数
v;GI|I7V+D;km?)~&[0    void setUp () {
t2j.z%p[%I?"XX0        m_value1 = 2;51Testing软件测试网};?L-vHP K$NBC'z
        m_value2 = 3;51Testing软件测试网Z^K L4AS*U.n
    }
vW5\ tg U0    // 测试加法的测试函数51Testing软件测试网Jo5h oj\C
    void testAdd () {51Testing软件测试网Mp0K I+Yn"Y)Fb`
        // 步骤(2),对 fixture 进行操作51Testing软件测试网9J%i.[,G-o8f0[sPl
        int result = m_value1 + m_value2;
qHr d7b2okc |2m5f3t0        // 步骤(3),验证结果是否争取
}$Sn0NJ7c?0        CPPUNIT_ASSERT( result == 5 );51Testing软件测试网It.aC P
    }
'G2~!B'L*XoY?i0    // 没有什么清理工作没有定义 tearDown.51Testing软件测试网3F?)d cm0`7j(V$s
}
6rB"gY3R Fm3?0 51Testing软件测试网 ~Y$lpJ$I kg
 51Testing软件测试网s,o$NDtNx

7T+z3lb+vn` D051Testing软件测试网Z5]Nr:oNcG?
在测试函数中对执行结果的验证成功或者失败直接反应这个测试用例的成功和失败。CppUnit 提供了多种验证成功失败的方式:

I;z#|/M6X"D&eCPW[051Testing软件测试网)@&v)f:Tmya%O


9M!I8]-rL!iZ7Y0  CPPUNIT_ASSERT(condition)     // 确信condition为真
n4\ }/h x0  CPPUNIT_ASSERT_MESSAGE(message, condition) // 当condition为假时失败, 并打印message51Testing软件测试网o9hn uw:E%U9X
  CPPUNIT_FAIL(message)      // 当前测试失败, 并打印message
(B7ky'fN[0  CPPUNIT_ASSERT_EQUAL(expected, actual)  // 确信两者相等51Testing软件测试网Xr1P0v~2h o-s
  CPPUNIT_ASSERT_EQUAL_MESSAGE(message, expected, actual) // 失败的同时打印message51Testing软件测试网'a tfI!\ O!N/v
  CPPUNIT_ASSERT_DOUBLES_EQUAL(expected, actual, delta) // 当expected和actual之间差大于delta时失败51Testing软件测试网!q]0vK\ oC0G
 
g X\-M%_jF`0 
hx$C6D*jN0要把对 fixture 的一个测试函数转变成一个测试用例,需要生成一个 CppUnit::TestCaller 对象。而最终运行整个应用程序的测试代码的时候,可能需要同时运行对一个 fixture 的多个测试函数,甚至多个 fixture 的测试用例。CppUnit 中把这种同时运行的测试案例的集合称为 TestSuite。而 TestRunner 则运行测试用例或者 TestSuite,具体管理所有测试用例的生命周期。目前提供了 3 类TestRunner,包括:
,hn7U9_%[3Y}0  CppUnit::TextUi::TestRunner  // 文本方式的TestRunner
|#a ka9r5Zsex4Wf0  CppUnit::QtUi::TestRunner  // QT方式的TestRunner51Testing软件测试网? R0e1V-O2}
  CppUnit::MfcUi::TestRunner  // MFC方式的TestRunner
!HJ1m:jz7\,?0 
{ E}-Q usQ9Q3z0 
QA3Z b&N _UC0下面是个文本方式 TestRunner 的例子:

D3hn(n4G;?B0

$K{0a%A\0
o`{C@&Kz#B B0   CppUnit::TextUi::TestRunner runner;51Testing软件测试网:P)kaa_k
   CppUnit::TestSuite *suite= new CppUnit::TestSuite();51Testing软件测试网N$PE3s}"p:|
 51Testing软件测试网,d8x8W6j#d/K
   // 添加一个测试用例51Testing软件测试网/Iy,N'QT5lk
   suite->addTest(new CppUnit::TestCaller<MathTest> (51Testing软件测试网({5{ih8`uv
                "testAdd", testAdd));
f:PaQ,b0 51Testing软件测试网j/gv9Kk
   // 指定运行TestSuite
3sq kU)`,r6X?0   runner.addTest( suite );
!s0T4{E:R0   // 开始运行, 自动显示测试进度和测试结果
3XW @ g,n#L0   runner.run( "", true );    // Run all tests and wait51Testing软件测试网-`;]tL#a7Qak#s
 51Testing软件测试网D(|(C#g(U-P$Q(pd
 51Testing软件测试网 s;c `J`Y

51Testing软件测试网#N@)hsu%K4Rlf3b|

51Testing软件测试网8X,R j5oH;L-K;|
对测试结果的管理、显示等功能涉及到另一类对象,主要用于内部对测试结果、进度的管理,以及进度和结果的显示。这里不做介绍。51Testing软件测试网lHVuMO%C

51Testing软件测试网yZl4]M4M7utt

下面我们整理一下思路,结合一个简单的例子,把上面说的思路串在一起。

)_7e7C*duj$rN]0

*D!AlK-l$y0 

0p(F ZrU s0J5Y051Testing软件测试网H9Eg-NnP

3. 手动使用步骤

N nJW)l:sx0

8A}Z@ ndv `0首先要明确测试的对象 fixture,然后根据其功能、流程,以及以前的经验,确定测试用例。这个步骤非常重要,直接关系到测试的最终效果。当然增加测试用例的过程是个阶段性的工作,开始完成代码后,先完成对功能的测试用例,保证其完成功能;然后对可能出错的部分,结合以前的经验(比如边界值测试、路径覆盖测试等)编写测试用例;最后在发现相关 bug 时,根据 bug 完成测试用例。51Testing软件测试网+|"Z"}$j:o4fg

FG%D;E W H*xN(}0比如对整数加法进行测试,首先定义一个新的 TestFixture 子类,MathTest,编写测试用例的测试代码。后期需要添加新的测试用例时只需要添加新的测试函数,根据需要修改 setUp 和 tearDown 即可。如果需要对新的 fixture 进行测试,定义新的 TestFixture 子类即可。注:下面代码仅用来表示原理,不能编译。51Testing软件测试网'rQK'|i6vhK5CV

Z!Y M}g051Testing软件测试网m#q9N|7TB \
/// MathTest.h51Testing软件测试网k7~Q9d|{z'I bQ;qa]I
// A TestFixture subclass.
I7D"~2Yw3`:M0// Announce: use as your owner risk.
%b%Y;|TIyy2yW0// Author  : liqun (liqun@nsfocus.com)
"\&Q5SgR'D&I0// Data    : 2003-7-551Testing软件测试网~'N2ITDT&c
#include "cppunit/TestFixture.h"51Testing软件测试网7l hNCD+[*Js6P
class MathTest : public CppUnit::TestFixture {51Testing软件测试网L&{ PK b*pL,N
protected:51Testing软件测试网PZ~Zl/ti
 int m_value1, m_value2;
?L#pO!jD0o0 51Testing软件测试网T0Nk7|N
public:51Testing软件测试网G;f"LX?H6C;[h
 MathTest() {}51Testing软件测试网.~?$c(c z3^ B2m3Q
 
Y p5G$S'Kz7e0 // 初始化函数
.jP2?6cWQ'}u0 void setUp ();
*N;Q|U9\9E\{0 // 清理函数51Testing软件测试网,`Y(p2z2P&_g$C i
 void tearDown();51Testing软件测试网}y_'rC6ro}N/r
 51Testing软件测试网4V,i`Q kHC%l
 // 测试加法的测试函数
&T5zr-Z(v8g4v5YV0 void testAdd ();51Testing软件测试网'b5J(k*amlxmo
 // 可以添加新的测试函数
SF{"p,MM$EC0};
E-n+t1f&K5U4L0/// MathTest.cpp51Testing软件测试网+ugEp)f
// A TestFixture subclass.51Testing软件测试网C1DH q2x(J,ui-|6A
// Announce: use as your owner risk.
.c,_#n1p ]/E'\S.qkB4id0// Author  : liqun (liqun@nsfocus.com)
+t5R/icG ?E4Pg0// Data    : 2003-7-551Testing软件测试网7oy}8tFB]4H{T
#include "MathTest.h"
@ u7zB]0#include "cppunit/TestAssert.h"
T,b{:^(rB+r0void MathTest::setUp()
9@v/uu{P0{
$t xb M'l:V9JI0     m_value1 = 2;51Testing软件测试网b.rcX Y7A
     m_value2 = 3;51Testing软件测试网IO D&Cta&n1K*N
}
2cp|/v }[0void MathTest::tearDown()
6~&~CY2W:Y)]8w{0{51Testing软件测试网*mf p%a3^8^&G-@
}51Testing软件测试网] Y[lx.oU2S M
void MathTest::testAdd()
Gh n!h0K[ B0{51Testing软件测试网q[~5\(s\q
     int result = m_value1 + m_value2;
KpQI2^P ]0     CPPUNIT_ASSERT( result == 5 );51Testing软件测试网U"T[)j!Ej
}

:H3|SFZ)nb0

N9Q-cA X5H5OGu X!w0 

`4G7y5p~#d0

:L*|,PcjgK-x051Testing软件测试网ct#]B0R7L a)iJ
然后编写 main 函数,把需要测试的测试用例组织到 TestSuite 中,然后通过 TestRuner 运行。这部分代码后期添加新的测试用例时需要改动的不多。只需要把新的测试用例添加到 TestSuite 中即可。

#{9tCLuY!?&G0

m7\\mD![o;QA9~-a@0
H)?URxUt0/// main.cpp51Testing软件测试网-g&OOBum!m$E
// Main file for cppunit test.51Testing软件测试网.e/~~~D/v
// Announce: use as your owner risk.51Testing软件测试网A0hQ$Z?W_y
// Author  : liqun (liqun@nsfocus.com)51Testing软件测试网i!NB `#B3uvjDc
// Data    : 2003-7-551Testing软件测试网+[ Q stD4~C
// Note    : Cannot compile, only for study
d%K0m8I g'Xz4~2N0#include "MathTest.h"
enrF0#include "cppunit/ui/text/TestRunner.h"
8COy~P(h8f0#include "cppunit/TestCaller.h"
L A;d`o1} H0#include "cppunit/TestSuite.h"51Testing软件测试网pTa6cfE q
int main()51Testing软件测试网`"ub*_:I D
{51Testing软件测试网5e;Y&RTAF
 CppUnit::TextUi::TestRunner runner;
@-UGRbK5^)Et6Am0 CppUnit::TestSuite *suite= new CppUnit::TestSuite();
8a3cZU;Q-E0 51Testing软件测试网KA/} C:Q(I ]S-CR n
 // 添加一个测试用例51Testing软件测试网'Cx(tD @({$\
 suite->addTest(new CppUnit::TestCaller<MathTest> (
pp1U/`!h CO)R:v0               "testAdd", testAdd));51Testing软件测试网3Z*}|!w1v^1W&ch
 
,[GMi#H-m p U/g0 // 指定运行TestSuite51Testing软件测试网 f/?(]7z2y3g i
 runner.addTest( suite );51Testing软件测试网@Gn7q/xK%C
 // 开始运行, 自动显示测试进度和测试结果51Testing软件测试网!\jtrD2nL I7@
 runner.run( "", true );    // Run all tests and wait
l-~9Z v}{z0}51Testing软件测试网 |$gHg@q] o#V6P

51Testing软件测试网;q `-X;z"Q0E b7H8Ly~

 51Testing软件测试网$F5yGk\h&^uv

51Testing软件测试网u2SH"[*s6@K\5Bl

4. 常用使用方式

@Y2R+y@:l051Testing软件测试网"h^)t;OMmFP

按照上面的方式,如果要添加新的测试用例,需要把每个测试用例添加到 TestSuite 中,而且添加新的 TestFixture 需要把所有头文件添加到 main.cpp 中,比较麻烦。为此 CppUnit 提供了 CppUnit::TestSuiteBuilder,CppUnit::TestFactoryRegistry 和一堆宏,用来方便地把 TestFixture 和测试用例注册到 TestSuite 中。下面就是通常的使用方式:51Testing软件测试网 dS,JjkF*}5C

51Testing软件测试网*aA*o:bX3Clh


P$j\u+Q.z G0/// MathTest.h51Testing软件测试网"y)s O y-hF`
// A TestFixture subclass.
2u UU7Y6HK4n CD[0// Announce: use as your owner risk.
9Ok6]h{ t D CU a0// Author  : liqun (liqun@nsfocus.com)
r"]w aR kP0// Data    : 2003-7-5
-^ l{"q;OZGu0#include "cppunit/extensions/HelperMacros.h"51Testing软件测试网-h5x[*`6M&oD
class MathTest : public CppUnit::TestFixture {51Testing软件测试网 V0YTW-J
 // 声明一个TestSuite51Testing软件测试网4Mm {iZ0t#NL
 CPPUNIT_TEST_SUITE( MathTest );51Testing软件测试网W'B;S9u!r,~W?;{
 // 添加测试用例到TestSuite, 定义新的测试用例需要在这儿声明一下
"^$kp:D(U1q0 CPPUNIT_TEST( testAdd );51Testing软件测试网IN5K7oM3@
 // TestSuite声明完成
YL9{[zLZ[ M\ X0 CPPUNIT_TEST_SUITE_END();51Testing软件测试网:fuL!^IO+D
 // 其余不变
A$}t5Y.Ww`0protected:51Testing软件测试网V#f-N0[2IN&B/y d
 int m_value1, m_value2;
lsMc$`~8bAJ;j0 51Testing软件测试网 Dktu4m6cF'b
public:51Testing软件测试网8c8Nt$M"V
 MathTest() {}
$]@ a }:E*gEf0 51Testing软件测试网#I-]Xj.@:h,c,YGim'G8]
 // 初始化函数
+o)uh[[FR1nD0 void setUp ();
0Vm!n IX.q W]0 // 清理函数51Testing软件测试网U Xx#T1uCc
 void tearDown();51Testing软件测试网`n0yPTw
 51Testing软件测试网zQ c&gh)jd B y&T5W
 // 测试加法的测试函数51Testing软件测试网 ].H]c*d"y4j
 void testAdd ();51Testing软件测试网&NauFT~
 // 可以添加新的测试函数51Testing软件测试网3At` Rs
};51Testing软件测试网 M_5z3K#i#\
/// MathTest.cpp
3_t^$Q^0// A TestFixture subclass.
Z J){u9_{0// Announce: use as your owner risk.51Testing软件测试网R:ta:Y0YO;|
// Author  : liqun (liqun@nsfocus.com)51Testing软件测试网 O3wp%\4Y/hy,@
// Data    : 2003-7-5
B'd)y/]TQOY*w(Bo_0#include "MathTest.h"51Testing软件测试网$? G%f)?dVI
// 把这个TestSuite注册到名字为"alltest"的TestSuite中, 如果没有定义会自动定义
-D4nt `&p2K,h0// 也可以CPPUNIT_TEST_SUITE_REGISTRATION( MathTest );注册到全局的一个未命名的TestSuite中.51Testing软件测试网*Ju X9b(K Jy'W:t
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( MathTest, "alltest" );
0D {%DT$Na0// 下面不变
!~,a_,AZC;kt,~0void MathTest::setUp()51Testing软件测试网3v8m je F
{
'Wc},O'k%b0s0     m_value1 = 2;
#G S8n"L{A0     m_value2 = 3;51Testing软件测试网4tV(J k^g
}51Testing软件测试网;sluu7fT m jn0{
void MathTest::tearDown()51Testing软件测试网 i/h7ZNl]
{51Testing软件测试网5IOC X8?$p t"v
}51Testing软件测试网U,G3H{"d2I1yu3T
void MathTest::testAdd()51Testing软件测试网k"H#I;B,o"?+V td
{51Testing软件测试网p;H I@t!XP.g0d'G
     int result = m_value1 + m_value2;51Testing软件测试网(B$X IF A3^ Z8~%l%S9O
     CPPUNIT_ASSERT( result == 5 );51Testing软件测试网,qjM"q q S'P6{9G
}51Testing软件测试网PU;W'Iv#s:NPm
/// main.cpp
R0o V+Z'BQ f}2YZ0// Main file for cppunit test.
1j#S%HR6O6E0// Announce: use as your owner risk.
;A,E0Z8]4@^BT3L(Hji0// Compile : g++ -lcppunit MathTest.cpp main.cpp
_IQwMy:x j5V$J0// Run     : ./a.out51Testing软件测试网7XP'zoT{ m h*C
// Test    : RedHat 8.0 CppUnit1.8.051Testing软件测试网vpMn |ZB
// Author  : liqun ( a litthle modification.liqun@nsfocus.com)51Testing软件测试网w Q:eT3J A+bo
// Data    : 2003-7-5
6Ly7h7N%z7f6I0// 不用再包含所有TestFixture子类的头文件51Testing软件测试网9o/xw*A-f.K
#include <cppunit/extensions/TestFactoryRegistry.h>51Testing软件测试网&tMVZl*WYp
#include <cppunit/ui/text/TestRunner.h>51Testing软件测试网*J4Ze i]g8W!Oi
// 如果不更改TestSuite, 本文件后期不需要更改.51Testing软件测试网0px\2P u|
int main()
w|d'Q q_X0{
p OF,k1U j#Ih0 CppUnit::TextUi::TestRunner runner;51Testing软件测试网izv]MrQ2X
 51Testing软件测试网(S ]E4C\)y u0A.m:D
 // 从注册的TestSuite中获取特定的TestSuite, 没有参数获取未命名的TestSuite.51Testing软件测试网$@4C0V2r|1X)COk
 CppUnit::TestFactoryRegistry &registry = CppUnit::TestFactoryRegistry::getRegistry("alltest");
(m/J6h3V@d8W'H1wGg0 // 添加这个TestSuite到TestRunner中
[ o.P%v md(_6?0 runner.addTest( registry.makeTest() );51Testing软件测试网!V5Jo iese
 // 运行测试51Testing软件测试网dtJF Tm m
 runner.run();51Testing软件测试网c.h/a.zB-V%z+u
}

N GZHVcs5v.s0

pG3Zb5u}&{0 51Testing软件测试网J b-EN0y
这样添加新的测试用例只需要在类定义的开始声明一下即可。

{x(c7@%{%d"HO&D051Testing软件测试网q4V(S| q L)F9A+i

 

?6XAv ^8e[0

S3S(H+P b-~m05. 其他实际问题51Testing软件测试网^$CJ PkP SI

SC:`N8Fg U0通常包含测试用例代码和被测试对象是在不同的项目中。应该在另一个项目(最好在不同的目录)中编写 TestFixture,然后把被测试的对象包含在测试项目中。51Testing软件测试网i&{9c~+o4QR#l;k

51Testing软件测试网2A0qn jZ9uN%M:qW

对某个类或者某个函数进行测试的时候,这个 TestFixture 可能引用了别的类或者别的函数,为了隔离其他部分代码的影响,应该在源文件中临时定义一些桩程序,模拟这些类或者函数。这些代码可以通过宏定义在测试项目中有效,而在被测试的项目中无效。

d hYLmL Q0

)A SR)S-Kv0 

/[0` r@@1V-Y051Testing软件测试网k~ fS JX]5~%t9gDT

参考资料51Testing软件测试网 [6X wXjXZ$h"V

51Testing软件测试网$Ze~vEC T

CppUnit 主页51Testing软件测试网} l#g(l#v5d
JUnitTest Infected: Programmers Love Writing Tests51Testing软件测试网 d3x%WJS2j ~8f.j}
CppUnit Cookbook51Testing软件测试网w7W,H+oN[#i8w0O
xprogramming 主页51Testing软件测试网Td }"i%v2eI4s
测试框架

Y/|oKm0

8{^raD'S0CppUnit 原码解读:http://morningspace.51.net/resource/cppunit/cppunit_anno.html  51Testing软件测试网[~t1aEO6o6tI [)E

3z!o {0p0^4Y.z0 51Testing软件测试网.cvo/?$z W%TJK5\

%Hy m|1H q0Nb t8j ~0关于作者

I{*Z?H&v:{j]*i051Testing软件测试网w-C_9z1g:s;m

  李群,当前关注于网络安全产品的开发、研究;软件开发过程等方面。您可以通过liqun@nsfocus.com和他联系。
M\ `h TP0 
i,VY}zf/j,F0 
Ppd2u@Oi#L g0

1|*|mY.q0

TAG: CppUnit 自动化测试

 

评分:0

我来说两句

Open Toolbar