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

标签 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;
}

预期结果:测试应该失败,失败信息中应包含字符串“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/

相关文章:

visual-studio - MSVC 相当于 '__builtin_return_address'

C#自定义json序列化

c# - 用 Regex.Replace() 替换字符串

Java:如何将变量的名称放入字符串中?

string - Terraform 将元组展平为字符串集

regex - Visual Studio regex 使用宏删除 VB.NET 代码中的所有注释和空行

c# - 来自窗口句柄的 .net 类名

c# - WCF 日志记录和统计

c# - 在 WPF 中对齐字符串

visual-studio - UWP 和 Xamarin 构建失败且没有输出