CPPUNIT实例
一、实例
在《CPPUNIT基本原理》中举了一个简单的例子,在这个实例的基础上,另外写了一个稍微复杂的例子用作cppunit的测试51Testing软件测试网|u R Js m^*ri
这个实例包含一个Write2File cpp,一个Write2File h文件,功能是实现从一个文件复制内容到另一个文件。具体实现如下:
SL{g)K4m,~6EMp*U0Write2File.cpp的代码:51Testing软件测试网2S+]ET.G6N[z
#include "Write2File.h"
vwN S`0Write2File::Write2File()
MW a!l1i hS0{
4gl~NnAt Xa0}51Testing软件测试网|!q[ Bqva
Write2File::~Write2File()
"|DSK cu0{
\)f%Ge _O0}
'N)xJX0ij7b N0int Write2File::Process() //主过程51Testing软件测试网l%J!O!] y1Kyb
{
F^Ue0Duu,u0ReadCfgFile( );
#R?iu1M0if( ReadFileSrc() )51Testing软件测试网k]yjk.O5c"Md
{
Dg(J'T.UH0return -1;
%Jap)MP#o0}51Testing软件测试网/k.oGM-oZ_)X0a
if( WriteFileOut() )
5ZvJ k,O!Z9~D0{51Testing软件测试网*|Oe IQ: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
int Write2File::ReadCfgFile() //这个函数本来是想实现从配置文件中获取输出目录和输出目录的,后来简化了这段代码,直接将路径赋值给变量了。51Testing软件测试网YKm'?/U\-F"Q5C NT
{51Testing软件测试网YL|7~b
strcpy( SrcPath , m_srcp );
7U7p!f;nr;@E b5V0strcpy( OutPath , m_outp );51Testing软件测试网-IxS$Cc'{tN3]
@}zg;J&BG0n0
return 0;51Testing软件测试网q i&Q ASWG
}
\+ss HV VQ0int 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^yf0if( ( fp = fopen( SrcFilename , "r" ) ) == NULL )51Testing软件测试网3LAMWP*Tm6{
{
'PKk NW"r2q}7Z0printf( "Open srcfile error :%s\n" , SrcFilename );51Testing软件测试网"H0}6mTy L$S
return -1; 51Testing软件测试网 nM3M NQ
}51Testing软件测试网C7@y xg s M
fnum = stat(SrcFilename,&statbuf);51Testing软件测试网$AH$y O/qjb[F"A
if( fnum < 0 ) 51Testing软件测试网"^?+O#e_nVi8s
{
{8h"~9j7\*YHv7_:?0printf("there need size of file\n");
|L8lx$i.zn;v0}51Testing软件测试网 ^-v^q6m2@'|
rCp2Vk*s0
fnum = statbuf.st_size; //源文件的大小
H6yn$`C9p w0fread( m_Buf , statbuf.st_size, 1 , fp );
H8i h*s7QLM fmR0fclose( fp );51Testing软件测试网HE3b7Hu-FKI
return 0;51Testing软件测试网{#q0Q}l3}sz"j
}51Testing软件测试网Om_.`r-n4{ S
int Write2File::WriteFileOut() //将读取的内容输入到输出文件51Testing软件测试网'@u+~W%\[!_jg
{
qB+U-E-NwT!z0FILE *fp = NULL;51Testing软件测试网7~/u4\n'z9P%w
memset( OutFilename , 0 , sizeof(OutFilename ) );51Testing软件测试网e6uL@ k-w'D
sprintf( OutFilename , "%s/test_out.txt", OutPath );
U { wM G[E\0if( access(OutFilename , F_OK) == 0 ) unlink( OutFilename );
dD-b~v\ ?0if( ( fp = fopen(OutFilename , "a+") ) == NULL )
[p QaGH F0{51Testing软件测试网F_-B(^^ey
printf("Read file %s error!\n",OutFilename );51Testing软件测试网;MMn0Z;g/UJd5P;O
return -1;
d$Q x J4QcFjj1MK0}51Testing软件测试网 l8xD'\3}oY W^$F"w2B
fwrite( m_Buf , statbuf.st_size , 1 , fp );
L#A4m)ob8I?)_"YK0fclose( fp );
Z:]$[;vk spK {0return 0;51Testing软件测试网*A*ty@6?*t7v B6kO
}51Testing软件测试网f/[,{!o3\.j{}
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(^0return -1;
.BgIbHY$@0}51Testing软件测试网6L9?9L.kS
m_argc = argc;51Testing软件测试网B-\;SW_}.q-?:p5R
strcpy( m_srcp , argv[1] );
u,`-c+R!l}z;m0strcpy( m_outp , argv[2] );51Testing软件测试网/}5n9@h5]8Sn
//m_srcp = argv;51Testing软件测试网*wF)YD5~ d
writefile.Process();
!u [ EmU"sG0return 0;51Testing软件测试网Z o mj+R
}51Testing软件测试网["s$dR8P1|s&_,x;m
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$Eyg
#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]0class Write2File
/U3n y,]Mz.vR%~3U0{51Testing软件测试网5Q:q-{i5T
public:51Testing软件测试网*g7j|.Y&GC$f nG
Write2File();51Testing软件测试网*Bm`2d f6SU
~Write2File();
z*gU3|!l E0public:
#O&U9\ X @$GI\J q0char m_Buf[m_BufSize];51Testing软件测试网gul|&Q5T
//protected:
dZ`7J i5};?%~0int ReadCfgFile();
QA? b{5s4{8c0int Process();
L7|v,pr%D1@6A6nZk0int ReadFileSrc();
7K6c[7^ j,k.i0int WriteFileOut();
Yl-f)S(}o@0char SrcPath[256];51Testing软件测试网$q/_7s6`(J ^A)P
char OutPath[256];51Testing软件测试网 K/^PbI\ M)z
char SrcFilename[256];
o(o4sDD2_0char OutFilename[256];
-TP ^y:W a2N)y"EIE0char Logfile[256];51Testing软件测试网A @0oG |yU5I7Y
char Logfilepath[256];
m0ZL)M!|,Bl&|P0char Logfilename[256];
Rm.Jp%Z2k7[9`4uc0struct stat statbuf; 51Testing软件测试网7}0H[e2CH7C.k
int fnum;
Hv"~QR7s$k0};51Testing软件测试网 N` z9aw Z
int m_argc;
w,@?KyR n0char m_srcp[256];
Vxf.w6f!B#zB0char m_outp[256];51Testing软件测试网5m8E r m/_lnP{ I
在cppunit的写的测试代码如下,其中包含main.cpp, testw2file.cpp, testw2file.h
&x%b^{|Z0main.cpp的代码如下:——基本上,这个main.cpp不需要修改了,即使增加了测试用例也不需要修改这部分代码。51Testing软件测试网Yt#z2k9y'u
#include "testw2file.h"51Testing软件测试网,G I8Y)|2g C
int main( )51Testing软件测试网8w @soR
{51Testing软件测试网&G? s3_td
//CPPUNIT_TEST_SUITE_REGISTRATION(Test);
2^-a.J]A"[6a0CPPUNIT_NS::TestResult controller;51Testing软件测试网)ZSL%f.XGP^
CPPUNIT_NS::TestResultCollector result;
t3L7G1ju0controller.addListener(&result);
]4z@5g*qGx O#~0CPPUNIT_NS::BriefTestProgressListener progress;51Testing软件测试网/s$z `K1~t&z[ W;_|
controller.addListener(&progress);51Testing软件测试网G1^ X0]2YaqT*U
CPPUNIT_NS::TestRunner runner;51Testing软件测试网P)n!y6l\
runner.addTest(CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest());
\Az}y5}+Uz4Ame w0runner.run(controller);
]4wj1L;c~~&L W[4E8?0return result.wasSuccessful()?0:-1;
{D d}7c0}51Testing软件测试网A*Wh_#a m
testw2file.h的代码如下:
$|9Os\$f0#include "cppunit/Portability.h"51Testing软件测试网D]8f~2U'H k"B A
#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%bUWl9VK6KQ0#include "Logger.h"51Testing软件测试网?da:jG5@
#include "/cxcs04/_hl/study/cppunit/testw2file/src/Write2File.cpp" //头文件把源代码文件包含进来,无须把被测试的源代码复制过来。
class W2File_Test:public CPPUNIT_NS::TestCase51Testing软件测试网4D5G_;T4w+?l [N
{
C)r.[W r0CPPUNIT_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,w R0public:
(pmRtV F\K0void setUp(void);
O:X+I8g;K;By0void tearDown(void);51Testing软件测试网3D3D:z&c;DK3[5h,x&}4[
char SrcPath[256];51Testing软件测试网1zKk X {l}3fj5q6M!r
protected:51Testing软件测试网@4cV$u#Q[)W0B E
int t_argc ;
o2f]t(B5xA E0const char *t_argv[3];
7r XN3b2Lf{9zmA9B0void testw2file(void);51Testing软件测试网fe6N(K\!H{#OZv
int f_num;51Testing软件测试网 mUH+e0|n
struct stat statbuf; 51Testing软件测试网:zV:Y4i#{l?
};
p}w B-td0CPPUNIT_TEST_SUITE_REGISTRATION(W2File_Test);
Z I+R+Rwn[0testw2file.cpp代码如下:
2?H\v.Ad*L3G0#include "testw2file.h"51Testing软件测试网"Re6H HMq"z
void W2File_Test::setUp(void)51Testing软件测试网FZ&^B8Pn8Y C!B
{51Testing软件测试网8j#n(t1g$I)H+H s
t_argc = 3;
rMQj,oH,a.P0t_argv[0]="1";
A6DA9dB0t_argv[1]="/cxcs04/_hl/study/cppunit/testw2file/src";
u3eO5i;UUDiq0t_argv[2]="/cxcs04/_hl/study/cppunit/testw2file/src";
3b3v,pZ ^i[0}
*bi;} i&UHVe0void W2File_Test::tearDown(void)51Testing软件测试网 n&fq5X+ZVUY
{
uK0jL}v)W{0f*u"o0ob1i5e$h&?&kF0
}
g%[?f,]W!C0void W2File_Test::testw2file(void)
#e5gS9_2EeJ"hq0{51Testing软件测试网D2zmq ?/b;qf
int i;