gpt4 book ai didi

unit-testing - 我可以将类型参数传递给 DUnitx 中的测试用例吗?

转载 作者:行者123 更新时间:2023-12-03 18:26:35 24 4
gpt4 key购买 nike

我正在为我的 FastCode 项目编写测试用例。

我写了一个像这样的通用测试器:

  TTest<T> = record
private
class var Def: System.Generics.Defaults.IComparer<T>;
class var F: FastDefaults.TComparison<T>;
strict private
class function Slow(const Left, Right: T): integer; static;
class function Fast(const Left, Right: T): integer; static;
class constructor Init;
public
class procedure Test(const Left, Right: T); static;
end;

典型的测试用例如下所示:

[Test]
[TestCase('Single', '100.0,100.0')]
...many more testcases...
procedure TestSingle(const L,R: Single);
[Test]
[TestCase('Double', '100.0,100.0')]
...many more testcases... (identical to the one above).
procedure TestDouble(const L,R: double);

测试代码通常如下(针对每种类型重复):

procedure TestDefault.TestSingle(const L, R: Single);
begin
TTest<Single>.Test(L,R);
end;

我想做什么:

[Test]
[TestTypes('single,double,extended')]
[TestCase('description', '100.0,100.0')]
...many more test cases....
procedure Test<TC>(L,R: TC);

并针对所述类型进行测试,这样我就不必编写那么多样板文件。
可以在 DUnitX 中完成这样的事情吗?

最佳答案

尝试使您的测试类通用,并为您要测试的每个具体类型注册您的测试类。

  [TestFixture]
TMyTestObject<T> = class(TObject)
public
// Sample Methods
// Simple single Test
[Test]
procedure Test1;
// Test with TestCase Atribute to supply parameters.
[Test]
[TestCase('TestA','1,2')]
[TestCase('TestB','3,4')]
procedure Test2(const AValue1 : T; const AValue2 : T);
end;

//...

initialization
TDUnitX.RegisterTestFixture(TMyTestObject<integer>);
TDUnitX.RegisterTestFixture(TMyTestObject<double>);

关于unit-testing - 我可以将类型参数传递给 DUnitx 中的测试用例吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30758918/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com