fod*_*ma1 2 delphi treeview unit-testing
我使用Embarcadero XE2在delphi中创建了一个库,我想测试它的功能和程序,但是它们太多了(超过50个).我想创建一个表单来测试这些我的库,左边是一个函数列表(带有类似选项的树视图),并且在表单的右边有可选的文本框和一个触发按钮.我在网上找到了一些treeview教程,但他们使用treeview可视化数据库.
Treeview不是必需的,任何可能的函数枚举都很棒.
有没有直接的方法来测试这么多功能?是否有更简单的方法来实现此测试(表单)?
我的库作为OpenOffice.org API的折射器,它主要包括程序,而不是函数,很难用单元测试框架测试,而且创建一个GUI来测试这个类会很棒,因为我必须检查手动如何工作.这种测试有什么解决方案吗?
你可以这样做,但这确实是错误的做法.你正在重新发明轮子,并为缺少测试的东西做好准备,因为你不能始终如一地可靠地重复测试.
测试应该是可重复的,并且应该始终运用尽可能多的可能的测试选项,每次更改时都可以测试,以确保您不会引入错误(或者放回您之前修复过的错误),并且允许您轻松支持新选项或参数或值.
您应该使用DUnit (Delphi单元测试),包含在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.
Run Code Online (Sandbox Code Playgroud)
使用Project->Add to Project,并添加您的unit(MyFunctions.pas)和新的测试用例单元(MyFunctionTests.pas在上面的shell中).它应该看起来像这样:
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.
Run Code Online (Sandbox Code Playgroud)
现在运行项目,在出现的窗口中单击绿色Play按钮(如Delphi IDE中的运行按钮)或点击F9.树视图将显示测试结果(传递绿色,失败红色).如果测试失败,您可以在窗口底部查看该测试的错误信息.(如果不能,请使用View窗口显示错误.)
| 归档时间: |
|
| 查看次数: |
663 次 |
| 最近记录: |