SitePoint Sponsor |
|
User Tag List
Results 1 to 5 of 5
-
Sep 16, 2006, 19:06 #1
- 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.
-
Sep 17, 2006, 04:18 #2
- 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();
}
}
-
Sep 17, 2006, 06:03 #3
- 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);
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
Jason Sweat ZCE - jsweat_php@yahoo.com
Book: PHP Patterns
Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
Detestable (adjective): software that isn't testable.
-
Sep 17, 2006, 08:57 #4
- 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.
-
Sep 17, 2006, 08:59 #5
- Join Date
- Jun 2003
- Location
- Iowa, USA
- Posts
- 3,749
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Great news!
Glad to helpJason Sweat ZCE - jsweat_php@yahoo.com
Book: PHP Patterns
Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
Detestable (adjective): software that isn't testable.
Bookmarks