SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    SitePoint Wizard bronze trophy devbanana's Avatar
    Join Date
    Apr 2006
    Location
    Pennsylvania
    Posts
    1,736
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    SimpleTest - asserting errors/exceptions

    Hi,

    I've been using SimpleTest for unit testing. So far, it's working great.

    However, I'm having a problem when I want to make sure an error occurs. I thought that the documentation said assertError should be used, which is what i've been using. However, the case doesn't seem to be counted. That is, if I have 30 test cases, and I add an assertError assertion, there are still 30 test cases, except there is an extra exception displayed.

    Also, it seems I can't test for one error per method. It seems to stop after the first exception and moves onto the next test method.

    Then I looked at the source and noticed assertError was marked as depricated. There is a similar method down a bit in the file called expectException(), which both sounds like what i want and looks similar to assertError(). So, I tried using that, but got the same results.

    I should note that it does fail if an exception does not occur. But the odd thing is, say that one assertion fails. It'll say 31 passes, 1 fails and 10 exceptions. If it does pass, then it says 31 passes, 0 fails and 11 exceptions.

    Is this the way it is supposed to work? The way it is displayed, it looks like it doesn't think those exceptions should occur. Also I'd expect them to be counted as passes if they do pass.

  2. #2
    SitePoint Guru
    Join Date
    May 2005
    Location
    Finland
    Posts
    608
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I do this:
    PHP Code:
    function testException() {
       try {
          throw new 
    Exception;
          
    $this->fail();
       } catch (
    Exception $e) {
          
    $this->pass();
       }


  3. #3
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Using CVS SimpleTest


    PHP Code:
    <?php

    require 'simpletest/unit_tester.php';
    require 
    'simpletest/reporter.php';

    class 
    subject {
      function 
    bad() {
        throw new 
    Exception('something bad happened');
      }
    }

    class 
    testExceptions extends UnitTestCase {
      function 
    testIt() {
        
    $s = new subject;

        
    $this->expectException();
        
    $s->bad();
      }
      function 
    testNotExpected() {
        
    $s = new subject;
        
    $s->bad();
      }
      function 
    testThisShouldFail() {
        
    $this->expectException();
      }

    }

    $test = new testExceptions;
    $test->run(new TextReporter);
    produces:
    Code:
    testExceptions
    Exception 1!
    Unexpected exception of type [Exception] with message [something bad happened] in [/home/sweatje/public_html/testex.php line 8]
    1) Failed to trap exception
            in testThisShouldFail
    FAILURES!!!
    Test cases run: 1/1, Passes: 1, Failures: 1, Exceptions: 1
    All the counts look ok to me there.
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  4. #4
    SitePoint Wizard bronze trophy devbanana's Avatar
    Join Date
    Apr 2006
    Location
    Pennsylvania
    Posts
    1,736
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sweatje, thanks for the reply. That worked perfectly. I didn't realize the expectException() call had to come before the exception. Now I get:

    BananaPHP Tests

    BananaPHP Tests

    3/3 test cases complete: 42 passes, 0 fails and 0 exceptions.

  5. #5
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Great news!

    Glad to help
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •