C# 测试——为什么当未抛出异常时,带 ExpectedException 标记的方法会通过?

发布于 2024-07-19 05:34:38 字数 438 浏览 6 评论 0原文

过去,我已经测试过这样的预期异常:

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

但是我注意到有一种(更干净?)的方法可以使用 ExpectedException 属性来测试它。 为什么这个测试方法在没有抛出异常的情况下却通过了呢? 这肯定违背了该属性的目的。

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

[编辑] 我正在使用 Silverlight 2 运行此测试

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

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

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() {
}

[Edit] I am running this test using Silverlight 2

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

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

发布评论

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

评论(4

聚集的泪 2024-07-26 05:34:38

我从未见过那张通行证 - 这就是你所拥有的一切吗? 您绝对确定您将其标记为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...

纵性 2024-07-26 05:34:38

Jon Skeet 事实上是对的,我确实有旧版本的测试框架。 我已在此处更新到 12 月 8 日版本 http://code。 msdn.microsoft.com/silverlightut/Release/ProjectReleases.aspx?ReleaseId=1913 并在使用 ExpectedException 进行标记时获得了预期的行为。

Jon Skeet was in fact right, I did have an old version of the testing framework. I updated to the Dec 08 release here http://code.msdn.microsoft.com/silverlightut/Release/ProjectReleases.aspx?ReleaseId=1913 and got the expected behaviour when tagging with ExpectedException.

痴梦一场 2024-07-26 05:34:38

我实际上经历过 ReSharper 4.5 testrunner 无法与 NUnit 2.5 中的 ExpectedException 一起使用。 ...但这看起来像 MSTest ...您能详细说明您正在使用哪个测试框架以及您正在使用哪个测试运行程序来执行测试吗?

I've actually experienced that ReSharper 4.5 testrunner does not work with ExpectedException in NUnit 2.5. ...but this looks like MSTest ... can you elaborate on which test framework you are using and which test runner you are using to execute the tests?

为你鎻心 2024-07-26 05:34:38

您应该将“ExpectedException”与附加的assert.fail一起使用,以便在未引发异常的情况下测试失败(仍然在带有.net 4和Microsoft.VisualStudio.QualityTools.UnitTestFramework的VS 2010中):

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

    Do.SomethingThatThrowsAnException();
    assert.fail("No exception ;-(");

}

You should use 'ExpectedException' with an additional assert.fail, so that the test fails if the exception is not thrown (still in VS 2010 with .net 4 and Microsoft.VisualStudio.QualityTools.UnitTestFramework):

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

    Do.SomethingThatThrowsAnException();
    assert.fail("No exception ;-(");

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