kotest: shouldNotThrow and shouldNotThrowAny matchers
Having these matchers would be great
-shouldNotThrow
-shouldNotThrowAny
Currently there is not way of expressing that easily with kotlintest matchers
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 3
- Comments: 30 (24 by maintainers)
Commits related to this issue
- Implement shouldNotThrow matchers This commit implements `shouldNotThrow`, `shouldNotThrowExactly` and `shouldNotThrowAny` matchers, with their `Unit` variations. Effectively, for the user, it doesn... — committed to kotest/kotest by LeoColman 5 years ago
- Implement shouldNotThrow matchers This commit implements `shouldNotThrow`, `shouldNotThrowExactly` and `shouldNotThrowAny` matchers, with their `Unit` variations. Effectively, for the user, it doesn... — committed to kotest/kotest by LeoColman 5 years ago
- Implement shouldNotThrow matchers (#603) This commit implements `shouldNotThrow`, `shouldNotThrowExactly` and `shouldNotThrowAny` matchers, with their `Unit` variations. Effectively, for the user,... — committed to kotest/kotest by LeoColman 5 years ago
Not sure what you mean. Why create a new issue for the same proposal as this one?
In short test-cases, it probably does create more noise than it’s worth. For example, in my case instead of:
I might write:
And it wouldn’t make much difference.
However, in longer test-cases the advantage is that it helps distinguish the assertion from the rest of the setup/cleanup code.
vs.
Without this assertion, I might be inclined to write
y.baz(z) // should not throw, but that’s not as good for the reason why comments in general are not preferable to readable code: because comments can become stale, they can lie, but code never lies.Furthermore, if you just write the assertion as a plain statement and it fails, all you get is a regular stacktrace associated with the unit test, indistinguishable in structure from the stacktraces you’d get if any other part of the test (setup/cleanup) failed. A failed
shouldNotThrowassertion can instead capture exceptions and rethrow them with a message like “Expected no exception to be thrown, but <wrapped exception> was thrown”.In the same way that the
shouldNotThrowstatement helps visually distinguish the assertion code from the rest of test code (making the test more readable), the exception it throws can help distinguish an assertion failure from any other failure within the code (making the problem easier to understand and the bug easier to fix).It’s an especially nice assertion to have when you have some function that should throw exceptions under certain circumstances and shouldn’t throw in others, and you want test both types of scenarios. That leads to nice symmetrical code:
@sksamuel Sometimes users want to write things in a more explicitly.
It’s the same reason we added the matchers
shouldNotBeTrueandshouldNotBeFalsehttps://github.com/kotlintest/kotlintest/issues/452Of course
(3 ==3).shouldBeTrueis exactly the same as(3==3).shouldNotBeFalse, but sometimes one might want to say in a way or another.Adding a
shouldNotThrowempty block doesn’t look like a problem to me, and it won’t make users more confused2 reasons that I can think of now:
shouldNotThrowAnySomethingException”