Unit Tests: How to test for Exceptions

Permanent Link: Unit Tests: How to test for Exceptions 3. April 2009 RSS Feed for comments on RSS-Feed für Kommentare zu: Unit Tests: How to test for Exceptions comments feed

When unit testing, you'd also want to test whether your application throws Exceptions as expected (the following examples are based on SimpleTest). Assumption for the examples is, that we have a method that expects an integer as parameter.

First way you probably come up with is this:

try {
$class->method('abc');
} catch(Exception $e) {
$this->assertIsA('Exception', $e);
}

Generally this looks ok, but it's not. If the method doesn't throw an exception, the test won't fail since the catch block is never executed. That's why we simply drag the test out of the catch block:

try {
$class->method('abc');
} catch(Exception $e) {
}
$this->assertIsA('Exception', $e);
unset($e);

Now the test fails when the exception isn't thrown because first of all $e won't be set and will surely not be an exception. It is important to add an unset($e), especially if you're testing for more exceptions directly afterwards.

Let's now assume that the method throws an InvalidArgumentException if the given parameter is not an integer.

try {
$class->method('abc');
} catch(Exception $e) {
}
$this->assertIsA('InvalidArgumentException', $e);
unset($e);

Now the test is in a state where it fails when no exception is thrown or when the thrown exception is not an InvalidArgumentException.

In case you're not lazy on typing, you might add one more line, which also allows you to put the assert back into the catch block:

try {
$class->method('abc');
$this->fail('Excepted exception was not thrown');
} catch(Exception $e) {
$this->assertIsA('InvalidArgumentException', $e);
unset($e);
}
		
		

5 comments

VictorJuicys Gravatar

VictorJuicy
26.07.2018, 22:08 o'clock

Hello. I see that you don't update your blog too often. I know that writing
articles is time consuming and boring. But did you know that there is a tool that allows you to create new
posts using existing content (from article directories or other blogs from your niche)?
And it does it very well. The new articles are high quality and pass the copyscape
test. You should try miftolo's tools

webcam girls Gravatar

webcam girl
16.10.2018, 22:34 o'clock

Every weekend i used to pay a quick visit this website, for the reason that i wish
for enjoyment, since this this web page conations really good funny stuff too.

bitcoin sportsbooks Gravatar

bitcoin sportsbook
29.11.2018, 19:13 o'clock

http://phpdevblog.niknovo.com is very interesting,
bookmarked!

https://www.youtube.com/watch?v=Wxc80rF0BTUs Gravatar

https://www.youtube.com/watch?v=Wxc80rF0BTU
01.12.2018, 09:54 o'clock

niknovo.com is my top 2 website now !

https://www.youtube.com/watch?v=wI_GUT3TgTos Gravatar

https://www.youtube.com/watch?v=wI_GUT3TgTo
02.12.2018, 17:26 o'clock

niknovo.com is interesting, but you must improve your site speed

Write a comment

(will not be published)