currently we are developing some bigger component tests. These are relying on some rather more complex data generators, which are creating data basis of some templates.
Problem
While testing with xUnit we feeding the test with the help of [InlineData]
:
[Theory]
[InlineData(Device.Type1, DeviceConfig.VeryHotDevice)]
[InlineData(Device.Type1, DeviceConfig.VeryColdDevice)]
public async Task TestCalculation(string deviceType, DeviceConfig deviceConfig)
The problem I have is regarding the Assertions for the different DeviceConfig
values. I have to create an switch
-statement with cases for every single one in the test method:
switch (simConfig)
{
case DeviceConfig.VeryHotDevice:
AssertVeryHotDevice(resultData);
break;
case DeviceConfig.VeryColdDevice:
AssertVeryVoldDevice(resultData);
break;
}
What I would like to have is something like the following. So basically just pass the method with the assertions for the specific [InlineData]
and .Invoke
it inside the test method.
[Theory]
[InlineData(Device.Type1, DeviceConfig.VeryHotDevice, )]
[InlineData(Device.Type1, DeviceConfig.VeryColdDevice)]
public async Task TestCalculation(string deviceType, DeviceConfig deviceConfig, Action<ResultData> assertAction)
Without going into details I already got this working by changing [InlineData]
to [MemberData]
with help of this answer (Lambda expression as inline data in xUnit).
Sadly you then run into following issue (MemberData tests show up as one test instead of many), because the Actions
are not IXunitSerializable
.
For me it's neither acceptable to show only one test (while it in reality are three to four), nor to implement some unconvenient clutter code to support IXunitSerializable
.
Question
How would you keep the assertions here some kind of dynamic and pass them directly with the test data with the goal to keep the test method clean? Of course I would also be happy, if you propose something completely different.