简体   繁体   中英

Does AssertFailedException have any special treatment in unit-tests?

I was wondering if there is anything special about the AssertFailedException ?

Is it required to be thrown for failed unit-tests or is it ok to throw any exception?

Would something not work if I throw any other exception or would the test engine behave in any different way?

I mean, is the AssertFailedException treated differently then other exceptions?

OK, the answer is YES.

I did some tests and found out that the AssertFailedException is treated differently and it's quite evil.

The AssertFailedException hides the entire stack-trace and all messages from inner exceptions and outputs only it's own messasge. It doesn't matter if I set the innter exception or not. It won't be displayed.

This is the strck trace that you see:

Exception of type 'UnitTestDemo.MyException' was thrown. at UnitTestDemo.Foo(String bar) in C:[...]\\Validations.cs:line 41 at UnitTestDemo.IsNullOrEmpty() in C:[...]\\ValidationTests.cs:line 29

And this is what you get when you throw a custom exception with an inner one:

Test method UnitTestDemo.IsNullOrEmptyPasses threw exception: UnitTestDemo.ValidationException: Exception of type 'UnitTestDemo.ValidationException' was thrown but none has been expected. ---> UnitTestDemo.ValidationException: 'bar' must be null or empty.

at UnitTestDemo.Validation'1.Throw[TException](Func'2 getMessage) in C:[...]\\ValidationHelper.cs:line 25 at UnitTestDemo.IsNullOrEmpty(ValidationContext'1 context, Func'2 getMessage) in C:[...]\\Validations.cs:line 42 at UnitTestDemo.ValidationTests.<>c.b__2_0() in C:[...]\\ValidationTests.cs:line 29 at UnitTestDemo.DoesNotThrow(ValidationContext`1 context) in C:[...]\\Validations.cs:line 36 --- End of inner exception stack trace --- at UnitTestDemo.DoesNotThrow(ValidationContext'1 context) in C:[...]\\Validations.cs:line 40 at UnitTestDemo.IsNullOrEmptyPasses() in C:[...]\\ValidationTests.cs:line 29

You see not only the message from the test but also from inner exceptions and the complete stack trace.

I would say that yes this type of exception is special, but only in that it happens to be the type of exception thrown when the following code is encountered:

Assert.Fail();

No this is definitely not required, but whenever I am using MSTest this is the type of exception that I throw whenever I need to blatantly indicate to the Test-Explorer that my test has failed.

No, everything would still work. However, keep in mind that whenever you throw an exception (at least in the case of Visual-Studio's MSTest) your test will indeed fail. Actually, this is how you will test your code for some of the problems/bugs it may have. Sometimes when my tests fail it was because my code is throwing an exception (and fails), allowing me to track down and fix my bug.

Please see the documentation over at MSDN . As you can see this type of exception just inherits the normal System.Exception and therefore cannot be expected to behave any differently than any other kind of exception.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM