assertError

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
Ree
Forum Regular
Posts: 592
Joined: Fri Jun 10, 2005 1:43 am
Location: LT

assertError

Post by Ree »

I am trying to get into unit testing lately and got a simple question. When exactly is assertError and its variants used? I am using SimpleTest, btw.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

There is information in the documentation.
Marcus Baker wrote:

Code: Select all

trigger_error('Disaster');
trigger_error('Catastrophe');
$this->assertError();
$this->assertError('Catastrophe');
$this->assertNoErrors();
This one takes some explanation as in fact they all pass!

PHP errors in SimpleTest are trapped and placed in a queue. Here the first error check catches the "Disaster" message without checking the text and passes. This removes the error from the queue. The next error check tests not only the existence of the error, but also the text which here matches so another pass. With the queue now empty the last test will pass as well. If any unchecked errors are left at the end of a test method then an exception will be reported in the test. Note that SimpleTest cannot catch compile time PHP errors.
Ree
Forum Regular
Posts: 592
Joined: Fri Jun 10, 2005 1:43 am
Location: LT

Post by Ree »

Right, I have missed it for some reason. However, I would like my classes to be aware of any errors themselves, without using PHP errors. I created a simple database connect-disconnect test case:

Code: Select all

function testDisconnectAfterUnsuccessfullConnection()
  {
    $db = &new Database('foobar', db_name, db_user, db_pass);
    $db->disconnect();
    $this->assertIdentical($db->getError(), 'NoConnection');
  }
It passes all right, BUT it throws an exception (SimpleTest caught PHP native error) which, as you could've guessed, occurs in mysql_connect() call (wrong host). Well, I do know, that IF I set error reporting to 0 in my script, I will not get the error and I know my class will work just fine in this case since it passes the test. I can put $this->assertError() in the test and the exception will be gone... but I just thought myself, it looks like I have to 'fix' my tests to get green?? Shouldn't I be fixing my classes instead?? So, is the assertError used in tests only to get around the native PHP errors, or is there another purpose I do not know?
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

You suppress the error using @.
Ree
Forum Regular
Posts: 592
Joined: Fri Jun 10, 2005 1:43 am
Location: LT

Post by Ree »

Yes, then what is the purpose of assertError?
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

SimpleTest's assertError is for native PHP errors. Since you've decided to roll your own error system, it's a bit of a null point, as you thought.

However, SimpleTest makes it easy for you to overload the assertError() assertion and supplant it with something that plays nice with your own error checking (even if it's as simple as a wrapper for AssertIdentical).

I wouldn't be so quick to dismiss the value of traditional errors: by defining your own handlers, you can do some pretty powerful stuff. However, at the end of the day, what we really want to use is Exceptions.

I can't wait til the day SimpleTest works in PHP5.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

I'm afraid I've been too busy working with what I've got in php4 to look at php5 properly. I haven't got a clue what SimpleTest does with exceptions.

I'll really need to get up to speed - especially now that Gentoo is making it easy to install v4 and v5 together.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

SimpleTest calls them exceptions, but they're not really exceptions. PHP's set_error_handler gives you, at best, a crude substitute for exceptions. SimpleTest capitalizes on this fact: the same way exceptions jump to the catch block, SimpleTest checks if any "try/catch blocks" (really assertError()) have been set, otherwise, it throws a warning: the equivalent of "uncaught exception" except that it's not fatal.
Post Reply