SimpleTest - expecting Exceptions

SimpleTest is a great testing framework for PHP, with a Drupal module available too.

Many tests are based on assert statements, but this patterns doesn't work for functions which are expected to throw Exceptions.

In that case the following pattern is often useful.

<?php

try {
 
foo($bar);
 
pass('Foo Bar worked');
} catch (
MyException $e) {
 
$this->fail($e->getMessage());
}
?>

Tags

Post new comment

Got something to add - just enter a comment
all other fields are optional.

Your email address will not be published.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
6 + 2 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.