.net - 如何使用 "proper"调用堆栈创建自定义 MSTest 断言方法

标签 .net unit-testing mstest assert

我正在编写用于测试单元测试值的扩展方法。一个天真的例子是:

public static void ShouldBeTrue(this bool value)
{
    if(!value)
    {
        throw new AssertFailedException("Expected true");
    }
}

并在测试中使用它:
someBool.ShouldBeTrue();

一切正常,除了抛出异常的行将是我在测试结果窗口中双击失败的测试时最终出现的行,并且在测试结果详细信息中,抛出行显示在错误堆栈跟踪中。

有没有办法解决这个问题,以便“someBool.ShouldBeTrue();”:
  • 在“测试结果”窗口中双击失败的测试会打开一行吗?
  • 是堆栈跟踪中的唯一一行吗?
  • 最佳答案

    已经为此编写了一个类库:http://geekswithblogs.net/sdorman/archive/2009/01/31/adding-custom-assertions-to-mstest.aspx
    上面的链接引用了一段话:

    ... For reference, those unavailable Asserts are:

    • Assert.IsNaN
    • Assert.IsEmpty
    • Assert.IsNotEmpty
    • Assert.Greater
    • Assert.GreaterOrEqual
    • Assert.Less
    • Assert.LessOrEqual
    • Assert.IsAssignableFrom
    • Assert.IsNotAssignableFrom
    • CollectionAssert.IsEmpty
    • CollectionAssert.IsNotEmpty
    • StringAssert.AreEqualIgnoringCase
    • StringAssert.IsMatch
    • FileAssert.AreEqual
    • FileAssert.AreNotEqual

    ...I have created a class library that includes all of them except the FileAssert methods and StringAssert.IsMatch. ... You can download the class from my SkyDrive public folder: https://skydrive.live.com/?cid=93d618d639ec9651&id=93D618D639EC9651%211283

    关于.net - 如何使用 "proper"调用堆栈创建自定义 MSTest 断言方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4215513/

    相关文章:

    c# - 如何在表单中编辑和保存设置?

    java - 查看单个类的代码覆盖率

    azure-devops - VSTS "Visual Studio Test"任务找不到 .Net Core dll 的测试

    oracle - System.IO.FileNotFoundException : Could not load file or assembly 'Oracle. DataAccess, ... 运行单元测试时

    c# - 如何在单元测试中模拟 RestSharp 可移植库

    c# - OutputDebugStringAppender 在 Visual Studio 中不起作用

    c# - 从 SQL 语句中检索元数据(表名)

    c# - 在 VS2012 和 VS2010 上运行相同的程序表现出不同的行为

    .net - 计算CPU使用率: Code improvement

    我可以使用 cmocka 模拟返回结构的函数吗?