示例:影片出租店程序(单元测试)
代码:
unit uMovieTest;
interface
uses
Windows,SysUtils,Classes,TestFramework,TestExtensions;
type
TTest = class(TTestCase)
published
procedure Test;
procedure TestHtml;
end;
implementation
uses uMovie1;
procedure TTest.Test;
const
CNT_Text = 'Rental Record for ZhangSan'#$D#$A
+ 'aaa 2'#$D#$A
+ 'bbb 6'#$D#$A
+ 'ccc 1.5'#$D#$A
+ 'Amount owed is 9.5'#$D#$A
+ 'You earned 4 frequent renter points';
var
ACustomer: TCustomer;
AMovie1,AMovie2,AMovie3: TMovie;
AText: string;
begin
AMovie1 := TMovie.Create('aaa',REGULAR);
AMovie2 := TMovie.Create('bbb',NEW_RELEASE);
AMovie3 := TMovie.Create('ccc',CHILDRENS);
try
ACustomer := TCustomer.Create('ZhangSan');
try
with ACustomer do
begin
AddRental(TRental.Create(AMovie1,1));
AddRental(TRental.Create(AMovie2,2));
AddRental(TRental.Create(AMovie3,3));
end;
AText := ACustomer.Statement;
Check(AText = CNT_Text, 'Test fail');
finally
ACustomer.Free;
end;
finally
AMovie1.Free;
AMovie2.Free;
AMovie3.Free;
end;
end;
procedure TTest.TestHtml;
const
CNT_Text = '<H1>Rental for <EM>ZhangSan</EM></H1><P>'#$D#$A
+ 'aaa:2<BR>'#$D#$A
+ 'bbb:6<BR>'#$D#$A
+ 'ccc:1.5<BR>'#$D#$A
+ '<P>You owe <EM>9.5</EM><P>'#$D#$A
+ 'On this Rental you earned <EM>4</EM> frequent renter points<P>';
var
ACustomer: TCustomer;
AMovie1,AMovie2,AMovie3: TMovie;
AText: string;
begin
AMovie1 := TMovie.Create('aaa',REGULAR);
AMovie2 := TMovie.Create('bbb',NEW_RELEASE);
AMovie3 := TMovie.Create('ccc',CHILDRENS);
try
ACustomer := TCustomer.Create('ZhangSan');
try
with ACustomer do
begin
AddRental(TRental.Create(AMovie1,1));
AddRental(TRental.Create(AMovie2,2));
AddRental(TRental.Create(AMovie3,3));
end;
AText := ACustomer.HtmlStatement;
Check(AText = CNT_Text, 'Test fail');
finally
ACustomer.Free;
end;
finally
AMovie1.Free;
AMovie2.Free;
AMovie3.Free;
end;
end;
initialization
TestFramework.RegisterTest(TTest.Suite);
end.