如何使用“适当的”创建自定义 MSTest 断言方法调用栈
我正在编写扩展方法来测试单元测试的值。一个简单的例子是:
public static void ShouldBeTrue(this bool value)
{
if(!value)
{
throw new AssertFailedException("Expected true");
}
}
在测试中使用它:
someBool.ShouldBeTrue();
一切正常,除了抛出异常的行将是我在“测试结果”窗口中双击失败的测试时最终看到的行,并且在“测试结果详细信息”中throw-line 显示在错误堆栈跟踪中。
有没有办法解决这个问题,以便“someBool.ShouldBeTrue();”:
- 是双击“测试结果”窗口中失败的测试打开的行?
- 是堆栈跟踪中唯一的一行吗?
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();":
- is the line that double-clicking the failing test in Test Results window opens?
- is the only line in stack trace?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
已经为此编写了一个类库: http://geekswithblogs.net/sdorman/archive/2009/01/31/adding-custom-assertions-to-mstest.aspx
上面的链接中有一段引用:
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:
我想我找到了答案。您所需要做的就是将自定义断言类/方法放入单独的程序集中。如果您愿意,可以将其作为同一解决方案中的单独项目。
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.