我可以在DUnitX中传递一个测试用例吗? [英] Can I pass a set to a test case in DUnitX?

查看:266
本文介绍了我可以在DUnitX中传递一个测试用例吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在运行测试后检查对象的状态。这个状态包含在一个集合中。可以使用DUnitX属性将预期状态传递给测试用例,以便我可以对所有不同的输入使用相同的测试。

I'm trying to check the state of an object after running a test. This state is contained in a set. Is it possible to pass the expected state to the test case using DUnitX Attributes, so that I can use the same test for all different inputs?

我尝试传递集作为一个常数或一组,但在我的测试例程中,它总是作为一个空集来到达。

I tried to pass the set as a constant or as a set, but in my Test routine it always arrives as an empty set.


  • 这是否可以使用属性?

  • 您将如何测试集合是否相同?

示例代码:

type
  TResult = (resOK,resWarn,resError);
  TResultSet = set of TResult;

const
  cErrWarn : TResultSet = [resWarn];

type
  [TestFixture]
  TMyTest = class(TBaseTest)
    [Test]
    [TestCase('Demo1','InputA,[resWarn]')] // <-- tried as a set
    [TestCase('Demo2','InputB,cErrWarn')]  // <-- tried as a constant

    procedure Test(Input:string; ExpectedResult: TResultSet);
  end;

procedure TMyTest.Test(Input:string; ExpectedResult: TResultSet);
begin
  // ExpectedResult is always the empty set []
  RunTests(MyObject(Input));
  Assert.AreEqual(ExpectedResult, MyObject.ResultSet);
end;

我还尝试将预期结果定义为数组,但是DUnitX甚至不调用测试再说了可能这只是太多了

I also tried to define the Expected result as array, but then DUnitX doesn't even call the test anymore. Probably that's just "too much"

    procedure Test(Input:string; ExpectedResult: array of TResult);

到目前为止,我最好的想法是使用以下方法。取最多三个样本(在此插入您最喜欢的整数)...预期状态并单独检查这些状态。这不是我真正希望的,但它是诀窍。

The best I could come up with so far was to use the following approach. Take a sample of up to three (insert your favourite integer here...) expected states and check for these separately. This is not really what I was hoping for, but it does the trick.

    procedure Test(Input:string; R1,R2,R3: TResult);

帮助非常感谢。 :

推荐答案

您正在使用 TestCaseAttribute 来指定参数传递给您的测试方法。但是,它根本不提供对传递集合作为参数的任何支持。您可以看到这是通过查看 DUnitX.Utils 单位中声明的常量转换。它将任何转换映射到一个集合到 ConvFail

You are using TestCaseAttribute to specify the arguments to be passed to your test method. However, it simply does not offer any support for passing sets as arguments. You can see that this is so by looking at the constant Conversions declared in the DUnitX.Utils unit. It maps any conversion to a set to ConvFail.

所以,如果要使用属性指定此数据将需要扩展测试框架。您可以从 CustomTestCaseSourceAttribute 导出自己的后代,并覆盖 GetCaseInfoArray 来解码设置的参数。作为一个粗略的例子,您可以使用以下内容:

So, if you want to specify this data using attributes you are going to need to extend the testing framework. You could derive your own descendent from CustomTestCaseSourceAttribute and override GetCaseInfoArray to decode your set arguments. As a crude example, you could use this:

type
  MyTestCaseAttribute = class(CustomTestCaseSourceAttribute)
  private
    FCaseInfo: TestCaseInfoArray;
  protected
    function GetCaseInfoArray: TestCaseInfoArray; override;
  public
    constructor Create(const ACaseName: string; const AInput: string; const AExpectedResult: TResultSet);
  end;

constructor MyTestCaseAttribute.Create(const ACaseName, AInput: string; const AExpectedResult: TResultSet);
begin
  inherited Create;
  SetLength(FCaseInfo, 1);
  FCaseInfo[0].Name := ACaseName;
  SetLength(FCaseInfo[0].Values, 2);
  FCaseInfo[0].Values[0] := TValue.From<string>(AInput);
  FCaseInfo[0].Values[1] := TValue.From<TResultSet>(AExpectedResult);
end;

function MyTestCaseAttribute.GetCaseInfoArray: TestCaseInfoArray;
begin
  Result := FCaseInfo;
end;

然后可以将以下属性添加到测试方法中:

You can then add the following attribute to your test method:

[MyTestCase('Demo2', 'InputB', [resWarn])]
procedure Test(Input: string; ExpectedResult: TResultSet);

为避免使用RTTI,为避免使用RTTI,使用RTTI可以提供更多的灵活性。你将传递参数作为一个字符串,并使用RTTI进行解码,就像DUnitX中的代码一样。这意味着每次要使用set参数时,都不需要编写定制属性。

I've avoided using RTTI here for simplicity, but using RTTI would give you more flexibility. You'd pass the argument as a string, and decode it using RTTI, just as the code in DUnitX does. This means you don't need to write bespoke attributes every time you want to use a set argument.

更好的是在DUnitX中通过扩展转换映射到封面,并将其作为补丁提交。我相信别人会觉得有用。

Even better would be to implement this within DUnitX by extending the Conversions map to cover sets, and submit this as a patch. I'm sure others would find it useful.

这篇关于我可以在DUnitX中传递一个测试用例吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆