Build/Test Issues

ExpectedException in mstest not being captured when it is marked as async Task.

Started by JoshSim on 6,977 views

[TestMethod]
[ExpectedException(typeof(SomeCustomException))]
public async Task Integration_FailingTest_ThrowsException()
{
await someMethod;
}


The above does not seem to work using NCrunch, it is showing success in my MSTest runner, but not in the nCrunch test runner.

Advice please?

I am evaluating it, and this is the first roadblock today.

Josh
Hi Josh,

Thanks for sharing this issue. After a quick test I've concluded that NCrunch's handling of the ExpectedException attribute within MSTest2012 doesn't work correctly with the new async behaviour. I've logged this down to be fixed in the next maintenance release. Meanwhile, I suggest working around the issue by implementing the assertion manually. For example:

		[TestMethod]
		public async Task Integration_FailingTest_ThrowsException()
		{
			try
			{
				await someMethod;
			}
			catch (Exception ex)
			{
				if (ex is SomeCustomException == false)
					Assert.Fail("Exception thrown was " + ex.InnerException + ", which is not the expected exception");
			}
		}


Cheers,

Remco

Post a reply

Log in to reply.