ExpectedException.NoExceptionMessage中的字符串出现在哪里?

时间:2017-02-25 09:19:00

标签: c# string visual-studio unit-testing

我正在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;
}

预期结果:测试应该失败,并且有关失败的信息应该包括字符串&#34;当参数太长&#34;时缺少ArgumentOutOfRange异常,因为测试函数中的异常被注释掉了。

测试按预期工作,但我找不到任何地方放在ExpectedException的第二个参数中的字符串。不在测试资源管理器窗口中,不在输出中 - &gt;测试窗口。

那么它应该去哪里,是什么原因导致它不会出现在那里?这适用于Visual Studio 2015和2017社区版。

1 个答案:

答案 0 :(得分:3)

如果您查看ExpectedExceptionBaseAttribute.NoExceptionMessage的文档,请写下以下内容:

  

此API支持产品基础结构,不能直接在您的代码中使用。

但是,在下面的评论中写道:

  

如果由于未引发异常而导致测试失败,则此消息将包含在测试结果中。

结论:

您会在测试结果中看到它,但如果失败则会