C#测试-在未引发异常的情况下,为什么ExpectedException标签方法通过? [英] C# testing -- why does ExpectedException tagged method pass when exception not thrown?

查看:294
本文介绍了C#测试-在未引发异常的情况下,为什么ExpectedException标签方法通过?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去,我已经测试过预期的例外情况,如下所示:

In the past I have tested for expected exceptions like this:

[TestMethod]
public void TestThrowsException() {
  try {
    Foo();
    Assert.Fail();
  } catch (MyException ex){//good
  }
}

但是,我注意到有一种(更干净的)方法可以使用ExpectedException属性对此进行测试.当未引发异常时,为什么此测试方法通过?当然,这违反了属性的目的.

However I notice that there is a (cleaner?) way to test this using the ExpectedException attribute. Why does this test method pass when the exception is not thrown? Surely this defeats the purpose of the attribute.

[TestMethod]
[ExpectedException(typeof(MyException))]
public void TestThrowsException() {
}

我正在使用Silverlight 2运行此测试

I am running this test using Silverlight 2

推荐答案

我从未见过这样的通行证-是您真正拥有的 吗?您是否绝对确定将其标记为TestMethod?测试跑步者是否显示通过?您肯定有最新的代码吗?

I've never seen that pass - is that really all you've got? Are you absolutely sure you have marked it as a TestMethod? Does the test runner show it passing? Have you definitely got the most recent code?

我会仔细检查,但我确信那会失败...

I'll double check, but I'm sure that will fail...

这篇关于C#测试-在未引发异常的情况下,为什么ExpectedException标签方法通过?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆