C # testing - why is ExpectedException method flagged when no exception is thrown?

I've tested expected exceptions in the past, for example:

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

      

However, I notice that there is a (clean up?) Way to test this using the ExpectedException attribute. Why does this testing method fail when no exception is thrown? Surely this defeats the purpose of the attribute.

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

      

[Edit] I am running this test with Silverlight 2

+1


a source to share


4 answers


I've never seen this passage - is that all you have? Are you absolutely sure you tagged it as TestMethod

? Does the conductor show that he is passing? Have you definitely received the most recent code?



I'll double check but I'm sure it will fail ...

+4


a source


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



+1


a source


I actually experienced that the ReSharper 4.5 test server does not work with ExpectedException in NUnit 2.5 .... but it looks like MSTest ... can you tell me what test framework you are using and which test runner you are using to run the tests?

0


a source


You have to use "ExpectedException" with the optional argument assert.fail to make the test fail if no exception is 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 ;-(");

}

      

0


a source







All Articles