0

Asserts raise an error in test code alerting that a test failed. If I am expecting a test to pass and not raise an exception, why would I use assert_not_raised (or equivalent) instead of just letting the raised exception fail the test?

I suppose it may be more explicit for others reading the test code, i.e. that the thing being tested could raise an exception, but still seems unnecessary.

user58446
  • 269
  • 1
  • 3
  • 17
  • There may be other reasons why code would raise or not, of itself, without regard to the test passing. – matt May 09 '21 at 07:01
  • Thanks for the comment. Can you elaborate or give an example where this might be the case? I would assume _most_ uncaught exceptions that would cause the program to halt during runtime would be grounds for failing the test. – user58446 May 09 '21 at 16:12
  • In general I agree, and so does MiniTest: [Why doesn't MiniTest::Spec have a wont\_raise assertion?](https://stackoverflow.com/questions/12499259/why-doesnt-minitestspec-have-a-wont-raise-assertion) But different languages / test suites see it differently, e.g. https://developer.apple.com/documentation/xctest/error_assertions. – matt May 09 '21 at 19:38
  • In general I suppose the idea is to separate the s.u.t. and its reporting from the test and its reporting. – matt May 09 '21 at 19:45

0 Answers0