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.
- Is this possible at all using attributes?
- How would you test if sets are identical?
Example code:
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;
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);
Help is greatly appreciated. :)
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 constantConversions
declared in theDUnitX.Utils
unit. It maps any conversion to a set toConvFail
.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 overrideGetCaseInfoArray
to decode your set arguments. As a crude example, you could use this:You can then add the following attribute to your test method:
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.
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.