我正在使用 MSTest 执行一些单元测验,我了解到我可以使用[DynamicData]
属性输入不同的案例进行测验,但我不能使用属性属性:DynamicDataDisplayName
为不同的案例设定名称。
我的测验代码是:
[TestMethod]
[DynamicData(nameof(TestInputs_BeReady), DynamicDataSourceType.Method,
DynamicDataDisplayName = nameof(GetTestDisplayNames),
DynamicDataDisplayNameDeclaringType = typeof(List<string>))]
public void Should_BeReady(object expValue, object[] inputs)
{
// Arrange
// Act
ui.InputID = (string)inputs[0];
ui.InputName = (string)inputs[1];
ui.InputList = (List<string>)inputs[2];
// Assert
Assert.AreEqual(expValue, onReadyStateChangeArgs.Ready);
}
public static IEnumerable<object[]> TestInputs_BeReady()
{
return new[]
{
new object[] { true, new object[] { "UTSZ0", "Unit Test Size List" , new List<string> { "entry_01", "entry_02" } } },
new object[] { false, new object[] { "STEST", "Unit Test Size List" , new List<string> { "entry_01", "entry_02" } } },
new object[] { false, new object[] { "", "Unit Test Size List" , new List<string> { "entry_01", "entry_02" } } },
new object[] { false, new object[] { "UTSZ0", "" , new List<string> { "entry_01", "entry_02" } } },
new object[] { false, new object[] { "UTSZ0", "Unit Test Size List", new List<string>() } },
new object[] { false, new object[] { "UTSZ0", "Unit Test Size List", null } }
};
}
public static IEnumerable<string> GetTestDisplayNames() => new List<string> {
"All Valid", "Duplicate ID", "Missing ID", "Missing Name", "Empty List", "Null List"
};
我在测验资源管理器结果中收到了这条讯息:
我在网上搜索了如何使用,DynamicDataDisplayName
但我无法提供一些东西;我发现的只是如何使用DynamicData
编辑
我使用此代码为测验输入添加自定义名称,这要归功于
[TestMethod]
[DynamicData(nameof(TestInputs_BeReady), DynamicDataSourceType.Method,
DynamicDataDisplayName = nameof(GetTestDisplayName))]
public void Should_DoSomething(object expValue, object[] inputs, string _)
{
// Arrange
// Act
// Assert
}
public static IEnumerable<object[]> TestInputs_BeReady()
{
List<string> ITEMS_LIST = new List<string> { "entry_01", "entry_02" };
List<string> BLANK_LIST = new List<string>();
return new[]
{
new object[] { true, new object[] { UQID, NAME, ITEMS_LIST }, "All Valid" },
new object[] { false, new object[] { DPID, NAME, ITEMS_LIST }, "Duplicate ID" },
new object[] { false, new object[] { BLNK, NAME, ITEMS_LIST }, "Missing ID" },
new object[] { false, new object[] { UQID, BLNK, ITEMS_LIST }, "Missing Name" },
new object[] { false, new object[] { UQID, NAME, BLANK_LIST }, "Empty List" },
new object[] { false, new object[] { UQID, NAME, null }, "Null List" }
};
}
uj5u.com热心网友回复:
问题在于GetTestDisplayNames
方法。你应该像这样定义它
public static string GetTestDisplayNames(MethodInfo methodInfo, object[] values)
{
var expected = (bool)values[0];
var inputs = (object[])values[1];
return $"{methodInfo.Name}({expected}, {inputs.Length})";
}
(这是一种可能的实作,但您当然可以回传不同的名称)
此外,自变量DynamicDataDisplayNameDeclaringType
应该指向型别,GetTestDisplayNames
方法定义在哪里。我相信,当您将此方法定义在与测验方法(Should_BeReady
)相同的类中时,可以跳过此自变量。
0 评论