- So far I've been using SimpleTest by good ole' Marcus Baker (who I have met hehe
) 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 - 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 thisBut that could all be theorically be replaced by
Code: Select all
$toLoad = 'NonExistantClass'; try { OF::loadClass($toLoad); } catch (OF_Exception $e) {}; $this->assertNull($e); unset($e);assertNoException doesn't seem to exist so perhaps there is another way of doing it or will I have to add the method myself?Code: Select all
$this->assertNoException(OF::loadClass($toLoad)); - I am trying to test whether a class exists. At the moment I'm doing it like thisis that actually any good I don't know if I am extending it correctly, seems to work though.
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); }
Testing Framework? assertException? assertClassExists?
Moderator: General Moderators
- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
Testing Framework? assertException? assertClassExists?
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:
for the class existance test why not simply:
?
Code: Select all
$this->assertTrue(class_exists('yourClass'), 'yourClass exists');- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
A better way of doing exceptions would be:
(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:
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.
Code: Select all
$toLoad = 'NonExistantClass';
try {
OF::loadClass($toLoad);
$this->assertPass('Exception not thrown');
} catch (OF_Exception $e) {
$this->assertFail('Exception thrown');
};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.
- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
No those assertion don't exist. How would I implement them, something like this?:Code: Select all
$toLoad = 'NonExistantClass'; try { OF::loadClass($toLoad); $this->assertPass('Exception not thrown'); } catch (OF_Exception $e) { $this->assertFail('Exception thrown'); };
Code: Select all
class OF_UnitTest extends UnitTestCase
{
protected function assertFail($message)
{
$this->assertTrue(false, $message);
}
protected function assertPass($message)
{
$this->assertTrue(true, $message);
}
}I think just may be what you are looking for.
Code: Select all
$this->fail('message');- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
Code: Select all
$this->pass('message never displayed but ole feels better by passCount++');- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
LMAO.sweatje wrote:Code: Select all
$this->pass('message never displayed but ole feels better by passCount++');
Now THATS what I'm talking about!