Page 1 of 1

Testing Framework? assertException? assertClassExists?

Posted: Sat Aug 12, 2006 4:03 pm
by Ollie Saunders
Not fully appreciating the "tests first, code later" principle of TDD, I have made full week's worth of changes to my code a tested a mere 2 functions. Needless to say I need to test it all now. No doubt you'll be seeing a fair bit of me in this thread over the next week or so (I hope I'm not still debugging this thing after that) as I encounter difficulties with testing. Anyway, I have three questions:
  1. So far I've been using SimpleTest by good ole' Marcus Baker (who I have met hehe :lol:) but I'm wondering if it is the best around given that I am writing E_STRICT PHP 5 code and SimpleTest is anything but
  2. How do you assertException or assertNoException? I want to test that a specific but of code has not thrown an exception at the moment I'm doing it like this

    Code: Select all

    $toLoad = 'NonExistantClass';
    try {
        OF::loadClass($toLoad);
    } catch (OF_Exception $e) {};
    $this->assertNull($e);
    unset($e);
    But that could all be theorically be replaced by

    Code: Select all

    $this->assertNoException(OF::loadClass($toLoad));
    assertNoException doesn't seem to exist so perhaps there is another way of doing it or will I have to add the method myself?
  3. I am trying to test whether a class exists. At the moment I'm doing it like this

    Code: Select all

    $toLoad = 'Text.php';
        $toLoadChk = 'OF_' . $toLoad;
        $this->assertClassNotExist($toLoadChk);
        OF::loadClass($toLoad);
        $this->assertClassExists($toLoadChk);
    }
    
    
    function assertClassExists($value, $message = "%s")
    {
        $dumper = new SimpleDumper();
        $message = sprintf(
                $message,
                "[" . $dumper->describeValue($value) . "] should be an existant class");
        return $this->assertTrue(class_exists($value), $message);
    }
    function assertClassNotExist($value, $message = "%s")
    {
        $dumper = new SimpleDumper();
        $message = sprintf(
                $message,
                "[" . $dumper->describeValue($value) . "] should be a non-existant class");
        return $this->assertTrue(!class_exists($value), $message);
    }
    is that actually any good I don't know if I am extending it correctly, seems to work though.

Posted: Sat Aug 12, 2006 4:42 pm
by Weirdan
seems ok to me

Posted: Sat Aug 12, 2006 6:12 pm
by sweatje
for the class existance test why not simply:

Code: Select all

$this->assertTrue(class_exists('yourClass'), 'yourClass exists');
?

Posted: Sun Aug 13, 2006 11:38 am
by Ambush Commander
A better way of doing exceptions would be:

Code: Select all

$toLoad = 'NonExistantClass';
try {
    OF::loadClass($toLoad);
    $this->assertPass('Exception not thrown');
} catch (OF_Exception $e) {
    $this->assertFail('Exception thrown');
};
(I think I got the assertions right).

That being said, I believe there is code in the repos like $this->expectException('Exception');, although I'm not precisely sure if it's released or if it works (last it was mentioned, Marcus was saying:
Yes it should. Damn :(.

I'm still working on this. I'll see if I can check some code in over the
next few days. It was working, but I must have broken something. It's
not easy testing the unit tester with itself, and I don't always run the
visual test file.
).

Talking about class existance, that's a knotty issue, because if you run the unit test twice in the same PHP script, things won't work. I'd just not test it at all.

Posted: Mon Aug 28, 2006 9:46 am
by Ollie Saunders

Code: Select all

$toLoad = 'NonExistantClass';
try {
    OF::loadClass($toLoad);
    $this->assertPass('Exception not thrown');
} catch (OF_Exception $e) {
    $this->assertFail('Exception thrown');
};
No those assertion don't exist. How would I implement them, something like this?:

Code: Select all

class OF_UnitTest extends UnitTestCase
{
    protected function assertFail($message)
    {
        $this->assertTrue(false, $message);
    }
    protected function assertPass($message)
    {
        $this->assertTrue(true, $message);
    }
}

Posted: Mon Aug 28, 2006 9:48 am
by sweatje
I think just

Code: Select all

$this->fail('message');
may be what you are looking for.

Posted: Mon Aug 28, 2006 9:58 am
by Ollie Saunders
But then my pass count won't increase :(
and I like watching that number get higher.

Posted: Mon Aug 28, 2006 10:02 am
by sweatje

Code: Select all

$this->pass('message never displayed but ole feels better by passCount++');

Posted: Mon Aug 28, 2006 10:05 am
by Ollie Saunders
sweatje wrote:

Code: Select all

$this->pass('message never displayed but ole feels better by passCount++');
LMAO.

Now THATS what I'm talking about! :D :D