Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExpectedExceptionAttribute is not working in MSTest

This is weird, but all of a sudden the ExpectedExceptionAttribute quit working for me the other day. Not sure what's gone wrong. I'm running VS 2010 and VS 2005 side-by-side. It's not working in VS 2010. This test should pass, however it is failing:

[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void Test_Exception()
{
    throw new ArgumentNullException("test");
}

Any ideas? This really sux.

like image 431
Micah Avatar asked Jul 23 '09 00:07

Micah


1 Answers

Not to resurrect a dead thread, but I came across this when this all the sudden happened to me, in case it can help others. I did finally track down what the problem was, which may correlate with what Jon found. The ExpectedException attribute appears to only work if the project is recognized as a TestProject. (Not just a .Net assembly)

Unload the project, edit the csproj file and check that the following setting is there:

<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>

(Assuming VS2010 project) Reload the project and rebuild. ExpectedException tests should now pass.

We ran into this issue when standardizing tests from NUnit to MSTest (Thank you TFS CI Build) and found that After replacing Assert.Throws<> beautiful simplicity & flexibility with [ExpectedException(Type)] crap, (Not to mention losing [TestCase()]!) the ExpectedException tests failed for no reason. Toggle back to NUnit with ExpectedException, no problem, MSTest refuses to run it.

Needless to say I will be pushing to get NUnit back, after finding: http://blog.shawnewallace.com/2011/02/running-nunit-tests-in-tfs-2010.html

like image 116
Steve Py Avatar answered Oct 26 '22 10:10

Steve Py