我正在 Visual Studio 中进行一些单元测试:
/// <summary>
/// Padding14 should throw an ArgumentOutOfRangeException if string is longer than 14
/// </summary>
[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException), "Missing ArgumentOutOfRange exception when argument too long")]
public void TestPadding14_ArgTooLong()
{
string input = "abcdefghijklmnop";
input.Padding14();
}
此行测试功能:
public static string Padding14(this string input)
{
if (input == "")
{
throw new ArgumentNullException("StringExtensions.Padding14: Input string empty");
}
if (input.Length > 14)
{
//throw new ArgumentOutOfRangeException("stringExtenstions.Padding14: Input string too long");
}
//throw new NotImplementedException();
return null;
}
预期结果:测试应该失败,失败信息中应包含字符串“Missing ArgumentOutOfRange exception when argument too long”,因为被测试函数中的异常已被注释掉。
测试按预期工作,但我无法在任何地方找到放入 ExpectedException 第二个参数中的字符串。不在测试资源管理器窗口中,不在输出 -> 测试窗口中。
那么它应该去哪里,什么会导致它不出现在那里?这适用于 Visual studio 2015 和 2017 社区版。
最佳答案
如果您查看 ExpectedExceptionBaseAttribute.NoExceptionMessage 的文档,写如下:
This API supports the product infrastructure and is not intended to be used directly from your code.
但是,在注释中写有以下内容:
If the test fails because an exception was not thrown, this message is included in the test result.
结论:
您将在测试结果中看到它,但仅如果失败。
关于c# - ExpectedException.NoExceptionMessage 中的字符串出现在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42454181/