我使用Embarcadero XE2在delphi中创建了一个库,我想测试它的功能和程序,但是它们太多了(超过50个)。我想创建一个表单来测试这些我的库,左边是一个函数列表(带有类似选项的树视图),并且在表单的右边有可选的文本框和一个触发按钮。我在网上找到了一些treeview教程,但他们用树视图可视化数据库。
Treeview不是必需的,任何可能的函数枚举都很棒。
有没有直接的方法来测试这么多功能?是否有更简单的方法来实现此测试(表单)?
我的库作为OpenOffice.org API的折射器,它主要包括程序,而不是函数,很难用单元测试框架测试,而且创建一个用于测试这个类的GUI会很棒,因为我必须手动检查它们是如何工作的。这种测试有什么解决方案吗?
答案 0 :(得分:6)
你可以这样做,但这确实是错误的做法。你正在重新发明轮子,并为缺少测试的东西做好准备,因为你不能始终如一地可靠地重复测试。
测试应该是可重复的,并且应该始终执行尽可能多的可能测试选项,每次更改时都可以测试,以确保您不会引入错误(或者放回您之前修复过的错误) ,并允许您轻松支持新的选项或参数或值。
您应该使用Delphi中包含的DUnit
(Delphi单元测试),现在可以使用多个版本(并且可以在SourceForge的早期版本中使用),它允许您自动化测试过程,并包括TreeView
显示测试设置(称为Suites
),组成该套件的各个测试以及每个测试的结果。它允许您测试应该工作的东西和不应该(并确保它们不会)的东西,并且一致地重复测试,这样您就不会错过任何东西。它还为您提供有关任何失败的测试的信息,以及帮助您找到(并修复)失败条件的信息。
在已经包含DUnit
的Delphi版本中(我认为这是从Delphi 2007及更高版本开始的任何内容),使用File->New->Other->Unit Tests->Test Project
创建shell。对于非基于类的测试,请创建另一个新单元并手动设置测试。这里有一个shell开始,测试两个无意义的函数:
unit MyFunctionsTests;
{
Delphi DUnit Test Case Skeleton Unit
}
interface
uses
TestFramework, MyFunctions;
type
// Test methods for MyFunctions unit
// In the real world, you'd create separate test cases,
// one for tests that should pass and one for tests that
// should fail, and run them separately or in succession.
// This is just a simple shell.
TTestMyFunctions = class(TTestCase)
strict private
public
procedure SetUp; override;
procedure TearDown; override;
published
procedure TestFunctionOne;
procedure TestFunctionTwo;
end;
implementation
procedure TTestMyFunctions.Setup;
begin
// Do any necessary initializations, set variables, etc.
end;
procedure TTestMyFunctions.TearDown;
begin
// Free any objects, release any memory, etc. here
end;
procedure TTestMyFunctions.TestFunctionOne;
begin
// FunctionOne takes two integers and adds them, and
// returns the sum
CheckTrue(FunctionOne(1, 1) = 2); // Test success
CheckFalse(FunctionOne(2, 2) = 5); // Test failure
end;
procedure TTestMyFunctions.TestFunctionTwo;
begin
CheckEqualsString('AB', FunctionTwo('A', 'B')); // Success
CheckFalse(FunctionTwo('B', 'A') = 'AB'); // Failure
end;
initialization
// Register any test cases with the test runner
RegisterTest(TTestMyFunctions.Suite);
end.
使用Project->Add to Project
,然后在上面的shell中添加您的单元(MyFunctions.pas
)和新的测试用例单元(MyFunctionTests.pas
)。看起来应该是这样的:
program MyFunctionUnitTests;
{
Delphi DUnit Test Project
-------------------------
This project contains the DUnit test framework and the GUI/Console test runners.
Add "CONSOLE_TESTRUNNER" to the conditional defines entry in the project options
to use the console test runner. Otherwise the GUI test runner will be used by
default.
}
{$IFDEF CONSOLE_TESTRUNNER}
{$APPTYPE CONSOLE}
{$ENDIF}
uses
DUnitTestRunner,
MyFunctions in '..\MyFunctions.pas',
MyFunctionsTests in 'MyFunctionsTests.pas';
{$R *.RES}
begin
DUnitTestRunner.RunRegisteredTests;
end.
现在运行项目,在出现的窗口中单击绿色Play
按钮(如Delphi IDE中的运行按钮)或点击 F9 。树视图将显示测试结果(传递绿色,失败红色)。如果测试失败,您可以在窗口底部查看该测试的错误信息。 (如果不能,请使用View
窗口显示错误。)
答案 1 :(得分:3)
使用Dunit代替。真的,你应该。这就是它的用途。 http://dunit.sourceforge.net/README.html
有一点点学习曲线,但是一旦你得到它,它就会“aahhhh!”