/usr/portage

Testing exceptions (2) 5

As I previously mentioned we made some effort to better check exceptions. The target is not only ensuring that a various exception is thrown but also that this exception is really the one which is expected. The result was a patch for PHPUnit to test for the exception message (which is already in) and the exception code. As Sebastian mentioned in the PHPUnit 3.2.9 release announcement, the latter is now included too. Therefore a few new options are available.

Example will fail:

<?php
    public function testException()
    {
        $this->setExpectedException('Exception', 'Exception message', 100);
        throw new Exception('Invalid message', 10);
    }

Example will pass:

<?php
    public function testException()
    {
        $this->setExpectedException('Exception', 'Exception message', 100);
        throw new Exception('Exception message', 100);
    }

Specifying exception tests with the annotations syntax:

<?php
    /**
     * @expectedException Exception error message 100
     */
    public function testException()
    {
         throw new Exception('error message', 100);
    }

Another attractive new feature is the option to use namespaces as a test organization separators. Until now, the PEAR scheme was used for PHPUnit test (_ becomes the directory separator, .php is auto-attached). This means a test was called My_ObjectTest and was supposed to be located in My/ObjectTest.php. Thanks to a really simple patch the test location stays the same, while the class name might be changed to My::ObjectTest. Happy namespacing.

Filed under , , & five comments & no trackbacks

Trackbacks

Trackback specific URI for this entry

No Trackbacks

Comments

  1. tobias answers:
    published on January 18th 2008, 03:30:20 am *

    Thank you very much for the patch about exception codes. I just searched for tests like that 5 days ago ;o)

    Reply

  2. Lars Strojny supposes:
    published on January 18th 2008, 04:25:41 am *

    Credit where credit is due. The harder part, the regex, was done by Sebastian. Just to be clear about that.

    Reply

  3. Sebastian Bergmann supposes:
    published on January 18th 2008, 08:06:56 am *

    The regular expression was crafted by Kore Nordmann.

    Reply

  4. ... answers:
    published on February 7th 2008, 07:51:25 am *

    Questo invece presidente desnudos gays ansa aishwarya ha rai [URL=http://www.diseno-de-casa.trenibuti.info] noi diseno casa de [/URL] accordo.

    Reply

  5. glocuntyAllot says:
    published on February 8th 2008, 08:46:46 am *

    The Associated Press, Go to Google News … city have decided to allow a school health center to make birth control pills available to girls as young as 11. ... http://free-order-xanax.100webspace.net/map.html – pharmacy prescription us without xanax , xanax 50 mg

    Reply

Add a Comment & let me know what you think