本空间文章除特别说明均属原创,转载请注明出处,谢谢!

CPPUNIT实例

上一篇 / 下一篇  2008-10-10 17:22:03

一、实例

在《CPPUNIT基本原理》中举了一个简单的例子,在这个实例的基础上,另外写了一个稍微复杂的例子用作cppunit测试51Testing软件测试网| u R Jsm^*ri

这个实例包含一个Write2File cpp,一个Write2File h文件,功能是实现从一个文件复制内容到另一个文件。具体实现如下:

SL{g)K4m,~6EMp*U0

Write2File.cpp的代码:51Testing软件测试网2S+]ET.G6N[z

#include "Write2File.h"

vwN S`0

 

`yS `:VW7y0

Write2File::Write2File()

MW a!l1i hS0

{

4gl~Nn At Xa0

}51Testing软件测试网|!q[ Bqva

 

8q n#l dwqw]0

Write2File::~Write2File()

"|DSK cu0

{

\)f%G e _O0

}

'N)xJX0i j7b N0

int Write2File::Process()       //主过程51Testing软件测试网l%J!O!] y1Ky b

{

F^Ue0Duu,u0

       ReadCfgFile( );

#R ?iu1M0

       if( ReadFileSrc() )51Testing软件测试网k]yjk.O5c"Md

       {

Dg(J'T.UH0

       return -1;

%Jap)M P#o0

       }51Testing软件测试网/k.o GM-oZ_)X0a

       if( WriteFileOut() )

5ZvJ k,O!Z9~D0

       {51Testing软件测试网*|OeIQ:j7K$`*|WMA

       return -1;51Testing软件测试网W5W{S'yY%l

       }51Testing软件测试网3bA;m$h2A;I3u

       return 0;51Testing软件测试网x-x}V7I)g_Y

}51Testing软件测试网|`S$J'm-V5C

 

t-]f~rYc%iZW0

int Write2File::ReadCfgFile()        //这个函数本来是想实现从配置文件中获取输出目录和输出目录的,后来简化了这段代码,直接将路径赋值给变量了。51Testing软件测试网 YKm'?/U\-F"Q5C NT

{51Testing软件测试网Y L|7~b

       strcpy( SrcPath , m_srcp );

7U7p!f;nr;@E b5V0

       strcpy( OutPath , m_outp );51Testing软件测试网-IxS$Cc'{tN3]

       

@}zg;J&BG0n0

       return 0;51Testing软件测试网 q i&Q ASWG

}

\+ss HVVQ0

int Write2File::ReadFileSrc()        //读取源文件的内容51Testing软件测试网PI1lU Fz9o

{51Testing软件测试网 P)rX1cz

       FILE *fp = NULL;51Testing软件测试网R:X/_7S"O"x

       memset( SrcFilename , 0 ,sizeof( SrcFilename ) );51Testing软件测试网-Q%LV_2zj

       sprintf( SrcFilename , "%s/test_src.txt" , SrcPath );

NV1g2^yf0

       if( ( fp = fopen( SrcFilename , "r" ) ) == NULL )51Testing软件测试网3LAMWP*Tm6{

       {

'PKk NW"r2q}7Z0

       printf( "Open srcfile error :%s\n" , SrcFilename );51Testing软件测试网"H0}6mTy L$S

       return -1;     51Testing软件测试网 nM3M NQ

       }51Testing软件测试网C7@yxg s M

       fnum = stat(SrcFilename,&statbuf);51Testing软件测试网$A H$y O/qjb[F"A

       if( fnum < 0 )  51Testing软件测试网"^?+O#e_nVi8s

       {  

{8h"~9j7\*YHv7_:?0

       printf("there  need  size  of  file\n");  

|L8lx$i.zn;v0

       }51Testing软件测试网 ^-v^q6m2@'|

       

rCp2V k*s0

       fnum = statbuf.st_size;       //源文件的大小

H6yn$`C9p w0

       fread( m_Buf , statbuf.st_size, 1 , fp );

H8ih*s7QLMfm R0

       fclose( fp );51Testing软件测试网HE3b7Hu-FKI

       return 0;51Testing软件测试网 {#q0Q}l3}s z"j

 

-V.x {,YS6G |p L0

}51Testing软件测试网Om_.`r-n4{ S

 51Testing软件测试网4n!LWw(gel U

int Write2File::WriteFileOut()        //将读取的内容输入到输出文件51Testing软件测试网'@u+~W%\[!_ jg

{

qB+U-E-NwT!z0

       FILE *fp = NULL;51Testing软件测试网7~/u4\n'z9P%w

       memset( OutFilename , 0 , sizeof(OutFilename ) );51Testing软件测试网e6u L@k-w'D

       sprintf( OutFilename , "%s/test_out.txt", OutPath );

U{ wM G[E\0

       if( access(OutFilename , F_OK) == 0 ) unlink( OutFilename );

dD-b~v\ ?0

       if( ( fp = fopen(OutFilename , "a+") ) == NULL )

[p QaGHF0

       {51Testing软件测试网F_-B(^^ey

       printf("Read file %s error!\n",OutFilename );51Testing软件测试网;MMn0Z;g/UJd5P;O

       return -1;

d$Q x J4QcFjj1M K0

       }51Testing软件测试网 l8xD'\3}oY W^$F"w2B

       fwrite( m_Buf , statbuf.st_size , 1 , fp );

L#A4m)ob8I?)_"YK0

       fclose( fp );

Z:]$[;vksp K {0

       return 0;51Testing软件测试网*A*ty @6?*t7v B6kO

}51Testing软件测试网f/[,{!o3\.j{}

 51Testing软件测试网4t(@*h dr ](Sb3n

int main(int argc , const char **argv)         51Testing软件测试网#Xs4B ^tB^X

{51Testing软件测试网y)HH$h0{j

       Write2File writefile;51Testing软件测试网w6i6d7k nFI |

       if( argc!=3 )51Testing软件测试网3h!S-Tje&L4B,p1w

       {51Testing软件测试网1xf R4_4o c[K.ge

               printf( "ERROR,Example: Write2File srcpath outpath \n" );

5EZ(~Pfaa3~X(^0

               return -1;

.BgIb HY$@0

       }51Testing软件测试网6L9?9L.kS

       m_argc = argc;51Testing软件测试网B-\;SW_ }.q-?:p5R

       strcpy( m_srcp , argv[1] );

u,`-c+R!l}z;m0

       strcpy( m_outp , argv[2] );51Testing软件测试网/}5n9@ h5]8Sn

       //m_srcp = argv;51Testing软件测试网*wF)YD5~ d

       writefile.Process();

!u [ EmU"sG0

       return 0;51Testing软件测试网Z o mj+R

}51Testing软件测试网["s$dR8P1|s&_,x;m

 51Testing软件测试网SG6| W&O HI.j

Write2File.h的代码:

:VI bYm&e\4pAqpd:`0

#include <stdlib.h>51Testing软件测试网5@7lo%I+L:Q"P+|7K

#include <math.h>51Testing软件测试网M N+rN$Ey g

#include <iostream.h>

;I}ll soxET0

#include <unistd.h>51Testing软件测试网SG*L,k{D

#include <sys/stat.h>51Testing软件测试网b"@8\^cN#K2gS @

#include <sstream>

+cVX*t]0

 

R&t6R8i3vmqPoSy0

class Write2File

/U3n y,]Mz.v R%~3U0

{51Testing软件测试网5Q:q-{i5T

public:51Testing软件测试网*g7j|.Y&GC$f nG

       Write2File();51Testing软件测试网*Bm`2d f6SU

       ~Write2File();

z*gU3|!l E0

public:

#O&U9\ X @$GI\J q0

       char m_Buf[m_BufSize];51Testing软件测试网gul|&Q5T

//protected:

dZ`7J i5};?%~0

       int ReadCfgFile();

QA ? b{5s4{8c0

       int Process();

L7|v,pr%D1@6A6nZk0

       int ReadFileSrc();

7K6c[7^ j,k.i0

       int WriteFileOut();

Yl-f)S(}o@0

 

$r/h2`$L)z,e M\6{0

       char SrcPath[256];51Testing软件测试网$q/_7s6`(J ^A)P

       char OutPath[256];51Testing软件测试网 K/^PbI\ M)z

       char SrcFilename[256];

o(o4sDD2_0

       char OutFilename[256];

-TP ^y:Wa2N)y"EIE0

       char Logfile[256];51Testing软件测试网A@0oG |yU5I7Y

       char Logfilepath[256];

m0ZL)M!|,Bl&|P0

       char Logfilename[256];

Rm.Jp%Z2k7[9`4uc0

       struct  stat  statbuf; 51Testing软件测试网7}0H[e2CH7C.k

       int fnum;

H v"~QR7s$k0

};51Testing软件测试网N` z9aw Z

       int m_argc;

w,@?KyR n0

       char m_srcp[256];

Vxf.w6f!B#zB0

       char m_outp[256];51Testing软件测试网5m8E r m/_lnP { I

 

gB kP;]!ye0

cppunit的写的测试代码如下,其中包含main.cpp, testw2file.cpp, testw2file.h

&x%b^{|Z0

main.cpp的代码如下:——基本上,这个main.cpp不需要修改了,即使增加了测试用例也不需要修改这部分代码。51Testing软件测试网Yt#z2k9y'u

#include "testw2file.h"51Testing软件测试网,GI8Y)|2g C

 

-EqTk0o'o0

int main( )51Testing软件测试网8w @soR

{51Testing软件测试网&G? s3_td

//CPPUNIT_TEST_SUITE_REGISTRATION(Test);

2^-a.J]A"[6a0

 

q;e,V&h1d gtE0

CPPUNIT_NS::TestResult controller;51Testing软件测试网)ZSL%f.XGP^

CPPUNIT_NS::TestResultCollector result;

t3L7G1ju0

controller.addListener(&result);

]4z@5g*qGx O#~0

 51Testing软件测试网-I"L5r,o#q&D

CPPUNIT_NS::BriefTestProgressListener progress;51Testing软件测试网/s$z `K1~t&z[ W;_ |

controller.addListener(&progress);51Testing软件测试网G1^ X0]2Ya qT*U

 51Testing软件测试网g2{#k@/}7@2X1]3I

CPPUNIT_NS::TestRunner runner;51Testing软件测试网P)n!y6l\

runner.addTest(CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest());

\Az}y5}+Uz4Ame w0

runner.run(controller);

]4w j1L;c~~&L W[4E8?0

return result.wasSuccessful()?0:-1;

{D d}7c0

}51Testing软件测试网A*Wh_#a m

 51Testing软件测试网#Ds Y&s1F$~.Y5P/e r;W

testw2file.h的代码如下:

$|9Os\$f0

#include "cppunit/Portability.h"51Testing软件测试网D]8f~2U'H k"BA

#include "cppunit/TestAssert.h"51Testing软件测试网y\3aG)Ap/@s6c

#include "cppunit/extensions/TestFactoryRegistry.h"

`+k5yyP/A?Q0

#include "cppunit/extensions/HelperMacros.h"

4e0B6p)vxT?0

#include "cppunit/TestResult.h"

.kP@ fq0

#include "cppunit/TestRunner.h"

7m+r8[Zf;qCm+J0

#include "cppunit/TestResultCollector.h"

nD8Q? TRSU.Y xO0

#include "cppunit/BriefTestProgressListener.h"

0fJ J%b UWl9VK6KQ0

#include "Logger.h"51Testing软件测试网? da:jG5@

#include "/cxcs04/_hl/study/cppunit/testw2file/src/Write2File.cpp"          //头文件把源代码文件包含进来,无须把被测试的源代码复制过来。51Testing软件测试网$E u%e0d-LY

 51Testing软件测试网!ecru Zk.g0YdD

class W2File_Test:public CPPUNIT_NS::TestCase51Testing软件测试网4D5G _;T4w+?l [N

{

C)r.[W r0

       CPPUNIT_TEST_SUITE(W2File_Test);51Testing软件测试网f]-T2D?4F)t1D8[

       CPPUNIT_TEST(testw2file);51Testing软件测试网S9C.R] _R0j&n!K:v

       CPPUNIT_TEST_SUITE_END();

j|B,wR0

 

4K-_#g1^M/@0

       public:

(pmRtV F\K0

       void setUp(void);

O:X+I8g;K;By0

       void tearDown(void);51Testing软件测试网3D3D:z&c;DK3[5h,x&}4[

       char SrcPath[256];51Testing软件测试网1zKk X {l}3fj5q6M!r

 

z#| wRx_wIk0

       protected:51Testing软件测试网@4c V$u#Q[)W0BE

       int t_argc ;

o2f]t(B5xA E0

       const char *t_argv[3]; 

7r XN3b2Lf{9zmA9B0

       void testw2file(void);51Testing软件测试网fe6N(K\!H{#OZv

       int f_num;51Testing软件测试网 mUH+e0|n

       struct  stat  statbuf;       51Testing软件测试网:z V:Y4i#{l?

 51Testing软件测试网5YF ^ZO*rg@

};

p }w B-td0

 

6l;M8Jt;cm0

CPPUNIT_TEST_SUITE_REGISTRATION(W2File_Test);

ZI+R+Rwn[0

 51Testing软件测试网jzS*H|3UZ

testw2file.cpp代码如下:

2?H\v.Ad*L3G0

#include "testw2file.h"51Testing软件测试网"Re6H HMq"z

 51Testing软件测试网&U"RA1Q~3`F-r

void W2File_Test::setUp(void)51Testing软件测试网FZ&^B8Pn8YC!B

{51Testing软件测试网8j#n(t1g$I)H+H s

       t_argc = 3;

rMQj,oH,a.P0

       t_argv[0]="1";

A6DA9dB0

       t_argv[1]="/cxcs04/_hl/study/cppunit/testw2file/src";

u3eO5i;UUD iq0

       t_argv[2]="/cxcs04/_hl/study/cppunit/testw2file/src";

3b3v,pZ ^i [0

}

*bi;}i&UHV e0

 

8t:m7x%JG+`0

void W2File_Test::tearDown(void)51Testing软件测试网 n&fq5X+ZVUY

{

uK0jL}v)W{0f*u"o0

       

ob1i5e$h&?&kF0

}   

g%[?f,]W!C0

 51Testing软件测试网tC(UOD)ol

void W2File_Test::testw2file(void)

#e5gS9_2EeJ"hq0

{51Testing软件测试网D2zmq ?/b;qf

       int i;

z@7]-h |kN/Ju&y0

       FILE *fp_out=NULL;51Testing软件测试网s/` ^+p$y

       Write2File W2file;51Testing软件测试网,O0U-Fd?

       char t_outfile[256];51Testing软件测试网k,? a \/\$H,u

       sprintf( t_outfile , "%s/test_out.txt" , t_argv[1] );51Testing软件测试网C@*jW G]q Be/w

       W2file.ReadCfgFile(t_argc , t_argv );

7j{}G4Y7tZ&Vt(W$W{0

 51Testing软件测试网IM&i?&H^(]*X

       fp_out = fopen(t_outfile , "r");51Testing软件测试网:q+D$[2A }s l:n

       stat( t_outfile , &statbuf );

]*xgWe$cvB0

       f_num = statbuf.st_size;//测试代码获取输出文件的真实大小51Testing软件测试网OgAX(Z8D0H#F

       printf( "outfile size =%d , srcfile size =%d\n" , f_num , W2file.fnum );    //将代码中获取的输出文件的真实大小和源代码中源文件的大小作对比(即预期值)

1s S'XfI T;H`"R0

       CPPUNIT_ASSERT( f_num == W2file.fnum );  //判断两个值的大小,如果一致这个用例就是成功的。

1g`V`as.d;u0

 

s j1l5wO9D0

}

6u u3@7f)^?lC:QA|0

 

!LJj6EmW3I0

如果是在unix环境下,可以写makefile文件完成程序的编译,在此顺便把我的makefile文件也贴出来:

Z9\g"iM1c9}~5U0

CPP =g++ -g51Testing软件测试网FXW])DM)@N

INCL     = -I/mylib/include               51Testing软件测试网v'Y'W V%jeN

LIBS   =-L/mylib/lib -lcppunit51Testing软件测试网'O/HZ/ijT!c*}

SYSLIB   = -ldl51Testing软件测试网fa_"NsC p

PROGRAMS = testw2file

%mnp3WwY0

OBJS   =main.o testw2file.o

,g5uxT4kKo-k]R+i0

testw2file:${OBJS}51Testing软件测试网X.x)\,G{

   $(CPP) $(CPPFLAG) -o $@ ${OBJS} $(LIBS) ${SYSLIB} ${INCL}51Testing软件测试网nZSk4V^!|#u*SO

clean:51Testing软件测试网5]kTU;]&k;h

   rm *.o testw2file

^Y9P2T:i m8\~ m0

二、总结

      在网上有不少CPPUNIT的资料,不知道最初的出处在哪里,有一些看了也不是特别清楚要怎么去使用cppunit,实例也不一定就能真正运行起来(当然这份资料也是借鉴了很多网上同行的资料)。为了让入门者更容易上手,在这里班门弄匠地总结一下自己接触CPPUNIT的心得,上面也附了一两个实例,仅是互相学习的意思。

SIp.Fv!L+|3c'r*y0

      1 CPPUNIT其实是相当于一个框架,我们设计好了测试的方法、预期值后,将源代码放到这个框架中去执行,以检查源代码的输出是否跟预期值是相符的。51Testing软件测试网F+cJ8u ^0xR

      2不需要将源代码贴到测试代码中去,一般建议保持源代码和测试代码的独立性

_3_)dA@$J L"Y)c0

      3对于预期值需要根据源代码的实际情况去设置,比如如果我要判断一个复制文件的函数执行是否成功,可能先要在测试代码中实现获取复制文件的大小,再将这个大小和源文件进行对比,如果一致,说明这个函数执行是成功的。因此,如何判断某个函数的执行是否成功,这个预期值应该取什么值以及要如何获得这个预期值,都是需要思考的,而且在测试代码中的实现有可能也会比较复杂。不过只要一次实现后,以后的修改就比较小了。51Testing软件测试网/z Rr1x_4TK~tb

      4对于有桩函数或者是驱动函数的函数,需要在测试代码中实现先将被测源代码中实现桩函数或驱动函数的功能。比如被测函数需要用到一个变量,则需要在测试代码中先给这个变量赋值,或者是被测函数需要桩函数先实现某个功能或达到某个条件,则需要在测试代码中先将这个前提条件满足。

3@"A6O2ml%k.GD0

      5如果涉及数据库的代码,建议是不要用CPPUNIT来实现,其实直接用功能测试来检查结果会更快捷.51Testing软件测试网?_4W a;Otyc/~

 51Testing软件测试网 i2\ Jq~g L*F

__原创文章,欢迎探讨51Testing软件测试网yD3B;m-O


TAG:

引用 删除 xiaoler   /   2010-08-23 16:24:53
CppUnit提供一个单元测试测试框架,基本能满足单元测试的需求,但仍然存在一些弊端,建议看看C++test专业测试博客上面的一些论述:www.mytestlife.com
孙为礼的测试Blog 引用 删除 swlcom   /   2010-05-05 18:13:41
谢谢楼主共享
孙为礼的测试Blog 引用 删除 swlcom   /   2010-05-05 18:13:13
5
小宝 引用 删除 ken867   /   2008-11-25 10:34:30
CPPUNIT,测试框架,挺好用的一个工具,相对于其他的测试工具来说似乎相对比较复杂,楼主写得很好,对不知道怎么做的测试开发工程师是一个很好的提高
learning_test 引用 删除 define_NULL   /   2008-10-30 11:45:27
楼主写得不错。不过我有几点疑问!
1。 首先我觉得在测试Process(0之前,没有对ReadFileSrc() 和WriteFile Src()做测试,这样子。可行吗?万一出错在这中间,怎么定位!
2. 看你写的测试,直接是改掉了被测类属性域属性,那样的话,测试都应该按照那样去改?
3. 对于面向对象的单元测试,确实很迷糊,如有的函数没有参数,直接对类属性的修改,这个该怎么测?(只是针对这个修改API而言)
 

评分:0

我来说两句

日历

« 2024-05-16  
   1234
567891011
12131415161718
19202122232425
262728293031 

数据统计

  • 访问量: 4581
  • 日志数: 6
  • 建立时间: 2008-09-10
  • 更新时间: 2008-10-10

RSS订阅

Open Toolbar