assertError not working

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
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

assertError not working

Post by Ollie Saunders »

I'm probably doing something stupid but this:

Code: Select all

ini_set('display_errors', true);
error_reporting(E_ALL);
require_once 'simpletest/unit_tester.php';
require_once 'simpletest/reporter.php';
function foo()
{
    trigger_error('error');
}
class TestAssertError extends UnitTestCase
{
    public function testCatchTriggeredError()
    {
        trigger_error($msg = 'error');
        $this->assertError($msg);
    }
    public function testCatchExternalTriggeredError()
    {
        foo();
        $this->assertError();
    }
}
$testCase = new TestAssertError();
$reporter = new HtmlReporter(ini_get('default_charset'));
$testCase->run($reporter);
...is producing this:

Code: Select all

TestAssertError
Fail: testCatchTriggeredError -> Expected error not found at [/osis/x/test/www/index.php line 17]
Fail: testCatchExternalTriggeredError -> Expected error not found at [/osis/x/test/www/index.php line 22]
1/1 test cases complete: 0 passes, 2 fails and 0 exceptions.
How can I make the assertions catch the errors?
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

It's your default error level I think.

Your trigger_error() call does not specify a level, so it defaults to E_USER_NOTICE. It might work if you set a E_USER_ERROR...

Edit | I take that back. I ran it myself and the tests all passed...
1/1 test cases complete: 2 passes, 0 fails and 0 exceptions.
I'm using the current CVS version, but I can't think of any difference that would make...
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Yeah. I don't see any reason why that shouldn't work. What's your PHP and SimpleTest version?
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Dammit, I was hoping I was being stupid. And specifing E_USER_ERROR made no difference at all.

PHP 5.1.2
simpletest_1.0.1alpha3
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

I try to always use the CVS version of SimpleTest since they seem to recommend that on SitePoint. Just check it out to a directory and keep updating your copy regularly. It's under a fairly active development.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

d11wtq wrote:I try to always use the CVS version of SimpleTest since they seem to recommend that on SitePoint. Just check it out to a directory and keep updating your copy regularly. It's under a fairly active development.
worked, thanks. :)
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

It was alpha3 - ;)

You should take a peek at using expectError() and expectException(). The error assertions are still supported but marked as deprecated in the code...

Code: Select all

/**
         *    @deprecated
         */
        function assertError($expected = false, $message = '%s') {
            $context = &SimpleTest::getContext();
            $queue = &$context->get('SimpleErrorQueue');
            return $queue->assertError($this->_coerceExpectation($expected), $message);
        }

        /**
         *    Prepares for an error. If the error mismatches it
         *    passes through, otherwise it is swallowed. Any
         *    left over errors trigger failures.
         *    @param SimpleExpectation/string $expected   The error to match.
         *    @param string $message                      Message on failure.
         *    @access public
         */
        function expectError($expected = false, $message = '%s') {
            $context = &SimpleTest::getContext();
            $queue = &$context->get('SimpleErrorQueue');
            $queue->expectError($this->_coerceExpectation($expected), $message);
        }
assertError is being retained for BC, but there are rafts of changes coming up in the near future that make expectError() a safer choice...
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

duly noted.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

uh well assertError was working fine and now i'm trying to use expectError. This is pretty obviously a bug but does anyone know what is going on?

Code: Select all

ini_set('display_errors', true);
error_reporting(E_ALL);
require_once 'cvs/simpletest/unit_tester.php';
require_once 'cvs/simpletest/reporter.php';
class TestAssertError extends UnitTestCase
{
    public function test1()
    {
        trigger_error('error'); // __LINE__ == 12
        $this->expectError(true);
    }
    public function test2()
    {
        trigger_error('error');
        $this->expectError(true);
    }
}
$testCase = new TestAssertError();
$reporter = new HtmlReporter(ini_get('default_charset'));
$testCase->run($reporter);
produces

Code: Select all

TestAssertError
Exception: test1 -> Unexpected PHP error [error] severity [E_USER_NOTICE] in [/osis/x/test/www/index.php line 12]
1/1 test cases complete: 1 passes, 0 fails and 1 exceptions.
Which is basically saying that despite being identical test2 is fine but test1 isn't catching the error.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Usually if something is expect rather than assert you need to call it before hand. Try calling expectError() then generate the error.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

that would explain A LOT! Thanks d11wtq
Post Reply