Simpletest expect exception

Discussion of testing theory and practice, including methodologies (such as TDD, BDD, DDD, Agile, XP) and software - anything to do with testing goes here. (Formerly "The Testing Side of Development")

Moderator: General Moderators

Post Reply
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Simpletest expect exception

Post by John Cartwright »

I can't seem to find this anywhere, but is it possible to expect an exception using lastcraft's Simpletest?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Simpletest expect exception

Post by Chris Corbyn »

Jcart wrote:I can't seem to find this anywhere, but is it possible to expect an exception using lastcraft's Simpletest?
I always do this:

Code: Select all

public function testFoo()
{
    try {
        throw new WhateverException("Expected this exception");
        $this->fail("Exception was expected, how did we get here???");
    } catch (WhateverException $e) {
        $this->pass();
    }
}
This is the way JUnit does it too AFAIK.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

try/catch.. how obvious 8)

I was expecting an assert or something. Thanks d11.
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

Code: Select all

public function testFoo()
{
    $this->expectException( new WhateverException("Expected this exception") );
    throw new WhateverException("Expected this exception");
}
The above also works - I'm fairly sure there's another method of checking the Exception message against a partial or regex though the Exception code in SimpleTest might need checking (or the API docs). This can be useful for checking a specific instance of Exception rather than simply it's type. Equivalent in JUnit might be the try...catch followed by a check on the Exception message.

Unexpected (above) or uncaught (first example) will of course be a fail event...
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Code: Select all

public function testFoo()
{
    $this->expectException( new WhateverException("Expected this exception") );
    throw new WhateverException("Expected this exception");
}
Oh so that's how you use that! I never got it to work before.

Here's the method I've been using previously:

Code: Select all

try {
   // throws
} catch (/* type to test for here */ Exception $e) {}
if ($this->assertNotNull(@$e)) {
    $this->assertWantedPattern('~whatever~i', $e->getMessage());
}
unset($e); // in case you need to test for another not null of $e
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

I'm wondering what the point of this bit of code is?

Code: Select all

f ($this->assertNotNull(@$e)) {
You already know it's "Exception" so why the assertion?

I was wondering how you could "expect" an exception but it's obvious really, SimpleTest already eats exceptions so it can display failures. The "expect" must just prevent it displaying a fail.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

I'm wondering what the point of this bit of code is?
Asserts that an exception was caught by prior catch.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

By definition there must have been an exception caught. The code is pointless at best, and if it actually turns out to be necessary, you've found a pretty major PHP bug.

Note about expectException: it takes you out of the entire test-case, so:

Code: Select all

$this->expectException();
$this->expectException();
Is impossible.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Code: Select all

$this->expectException('Exception');
throw new Exception('blah blah');
Also works. Have been using this, and expectError() today. (The latter being quite a bit more finicky than the former..)
Post Reply