2

I have written a test and I just want to ensure that everything passed and no exceptions were thrown ?

Is there some kind of special Assert to use at the end of the test?

What are the recommendations here?

Thanks in advance

Martin
  • 23,844
  • 55
  • 201
  • 327

3 Answers3

1

The unit test will fail if an exception is thrown anyway. That is of course unless you're expecting it to fail, in which case you can capture and assert it, something like:

var exception = Assert.Throws<Exception>(() => MethodThatShouldThrowAnError());
Assert.AreEqual("Not Brilliant", exception.Message);
Dimitar Dimitrov
  • 14,868
  • 8
  • 51
  • 79
  • 1
    Yes so, if I class my test as being passed if NO EXCEPTION has happened do I just do NO assert? – Martin Jun 27 '13 at 15:18
  • 1
    @Martin Well, yes pretty much but that would not be a good test if I'm honest. If I'm in your situation I'd write another test (testing something else) entirely, if that fails for some reason when your invoking your method it indicates a bug. I hope I'm making sense. – Dimitar Dimitrov Jun 27 '13 at 17:03
1

If you use Fluent Assertions (which your tag suggests), you can do:

Action act = () => MethodThatShouldNotThrowAnError();
act.ShouldNotThrow();
Dennis Doomen
  • 8,368
  • 1
  • 32
  • 44
-1

Just don't write any return statements in the test. Then the fact that test is finished with no exception will exactly mean that everithing in the test is passed.

Alexander Stepaniuk
  • 6,217
  • 4
  • 31
  • 48
  • I think this is a poor way to write tests. If a test body is just "DoStuff()" it's very hard to maintain. Each test should contain one very specific assertion, rather than being considered green just because it didn't throw. – RJFalconer Oct 10 '16 at 22:02