Testing Framework? assertException? assertClassExists?

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

Testing Framework? assertException? assertClassExists?

Post 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.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

seems ok to me
User avatar
sweatje
Forum Contributor
Posts: 277
Joined: Wed Jun 29, 2005 10:04 pm
Location: Iowa, USA

Post by sweatje »

for the class existance test why not simply:

Code: Select all

$this->assertTrue(class_exists('yourClass'), 'yourClass exists');
?
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

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

Post 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);
    }
}
User avatar
sweatje
Forum Contributor
Posts: 277
Joined: Wed Jun 29, 2005 10:04 pm
Location: Iowa, USA

Post by sweatje »

I think just

Code: Select all

$this->fail('message');
may be what you are looking for.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

But then my pass count won't increase :(
and I like watching that number get higher.
User avatar
sweatje
Forum Contributor
Posts: 277
Joined: Wed Jun 29, 2005 10:04 pm
Location: Iowa, USA

Post by sweatje »

Code: Select all

$this->pass('message never displayed but ole feels better by passCount++');
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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
Post Reply