使用XUnit,我有一个具有多个MemberData属性的方法(引用复杂的数据结构)。它运作良好,但问题是,如果任何MemberData数据集未通过测试,则很难弄清楚是哪一个,因为在VS Test Explorer中,失败的测试旁边会显示方法参数而不是数据集名称。
示例:
[Theory]
[MemberData(nameof(DataSet1))]
[MemberData(nameof(DataSet2))]
[MemberData(nameof(DataSet3))]
[MemberData(nameof(DataSet4))]
public void TestMethod(IEnumerable<string> argument1, IEnumerable<string> argument2, decimal argument3) {
Assert.True(argument3 == 100m);
}
public static IEnumerable<object[]> DataSet1
=> new object[][]
{
new object[]
{
new List<string>()
{
"string 1",
"string 2",
"string 3",
"string 4",
"string 5",
},
new List<string>()
{
"string 1",
"string 2",
"string 3",
"string 4",
"string 5",
},
100m
}
};
VS Test Explorer输出(由于缺乏声誉而无法添加图像,因此添加文本):
Result1 Name: (...) TestMethod(argument1: ["string 1", "string 2", "string 3", "string 4", "string 5"], argument2: ["string 1", "string 2", "string 3", "string 4", "string 5"], argument3: 200)
Result1 Outcome: Failed
因此,作为解决方法,我添加了另一个带有数据集名称的参数,例如:
public void TestMethod(string testName, // added workaround
IEnumerable<string> argument1, IEnumerable<string> argument2, decimal argument3) {
Assert.True(argument3 == 100m);
}
public static IEnumerable<object[]> DataSet1
=> new object[][]
{
new object[]
{
nameof(DataSet1), // added workaround
new List<string>()
{
"string 1",
(...)
然后我可以在 VS Test Explorer输出中看到数据集名称:
Result1 Name: (...) TestMethod(
testName: "DataSet2", // now properly shows failed dataset name
argument1: ["string 1", "string 2", "string 3", "string 4", "string 5"], argument2: ["string 1", "string 2", "string 3", "string 4", "string 5"], argument3: 200)
Result1 Outcome: Failed
那么,有没有一种直接的方法来在VS Test Explorer中查看MemberData数据集名称,而没有解决方法?