如何使用“适当的”创建自定义 MSTest 断言方法调用栈

发布于 2024-10-03 00:20:52 字数 470 浏览 0 评论 0原文

我正在编写扩展方法来测试单元测试的值。一个简单的例子是:

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

在测试中使用它:

someBool.ShouldBeTrue();

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

有没有办法解决这个问题,以便“someBool.ShouldBeTrue();”:

  1. 是双击“测试结果”窗口中失败的测试打开的行?
  2. 是堆栈跟踪中唯一的一行吗?

I'm writing extension methods for testing values for my unit-test. A naïve example would be:

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

And using it in a test:

someBool.ShouldBeTrue();

Everything works, except that the line throwing the exception will be the one I end up on when double-clicking the failed test in the Test Results window, and in Test Result Details the throw-line is shown in the Error Stack Trace.

Is there a way round this, so that "someBool.ShouldBeTrue();":

  1. is the line that double-clicking the failing test in Test Results window opens?
  2. is the only line in stack trace?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ぇ气 2024-10-10 00:20:52

已经为此编写了一个类库: http://geekswithblogs.net/sdorman/archive/2009/01/31/adding-custom-assertions-to-mstest.aspx

上面的链接中有一段引用:

...仅供参考,那些不可用的断言是:

  • 断言.IsNaN
  • 断言.IsEmpty
  • 断言.IsNotEmpty
  • 断言.更大
  • Assert.GreaterOrEqual
  • 断言.Less
  • Assert.LessOrEqual
  • Assert.IsAssignableFrom
  • Assert.IsNotAssignableFrom
  • CollectionAssert.IsEmpty
  • CollectionAssert.IsNotEmpty
  • StringAssert.AreEqualIgnoringCase
  • StringAssert.IsMatch
  • FileAssert.AreEqual
  • FileAssert.AreNotEqual

...我创建了一个类库,其中包含除 FileAssert 方法和 StringAssert.IsMatch 之外的所有方法。 ...您可以从我的 SkyDrive 公共文件夹下载该课程: https:/ /skydrive.live.com/?cid=93d618d639ec9651&id=93D618D639EC9651%211283

There's a class library already written for that: http://geekswithblogs.net/sdorman/archive/2009/01/31/adding-custom-assertions-to-mstest.aspx

There's a quote from the link above:

... 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

寂寞笑我太脆弱 2024-10-10 00:20:52

我想我找到了答案。您所需要做的就是将自定义断言类/方法放入单独的程序集中。如果您愿意,可以将其作为同一解决方案中的单独项目。

I think I found the answer. All you need to do is put your custom assertion classes/methods into a seperate assembly. You can have this as a seperate project in the same solution, if you like.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文