PHPUnit Hello world 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
foobaa
Forum Commoner
Posts: 40
Joined: Tue Feb 13, 2007 10:36 am

PHPUnit Hello world not working

Post by foobaa »

Hi,

I'm trying to get into unit testing. I won't be using the command line - I've tried the following code but it doesn't output results of assertions, what am I doing wrong? Thanks

Code: Select all

 
<?
require_once 'PHPUnit/Framework.php';
 
class TestTest extends PHPUnit_Framework_TestCase {
    public function testTest() {
        echo "Test Test<br>";
        $this->assertTrue(false);
        $this->assertTrue(true);
    }
}
 
$suite = new PHPUnit_Framework_TestSuite('TestTest');
$suite->run(new PHPUnit_Framework_TestResult);
?>
 
foobaa
Forum Commoner
Posts: 40
Joined: Tue Feb 13, 2007 10:36 am

Re: PHPUnit Hello world not working

Post by foobaa »

Ok I realise I missed a part of the documentation - I needed to implement a TestListener...

Code: Select all

 
<?
class SimpleTestListener implements PHPUnit_Framework_TestListener {
    public function addError(PHPUnit_Framework_Test $test, Exception $e, $time){
        printf( "Error while running test '%s'.<br>", $test->getName());
    }
    public function addFailure(
            PHPUnit_Framework_Test $test,
            PHPUnit_Framework_AssertionFailedError $e,
            $time){
    }
    public function addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time){
        printf("Test '%s' is incomplete.<br>", $test->getName());
    }
    public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time){
        printf("Test '%s' has been skipped.<br>", $test->getName());
    }
    public function startTest(PHPUnit_Framework_Test $test){
        printf("%s()... ", $test->getName());
    }
    public function endTest(PHPUnit_Framework_Test $test, $time){
        if ($test->getStatus()){
            echo "FAILED... " . $test->getStatusMessage() . " ";
        } else {
            echo "OK. ";
        }
        echo "(". $test->getNumAssertions() ." assertions run) <br>";
    }
    public function startTestSuite(PHPUnit_Framework_TestSuite $suite){
        printf("<h2>TestSuite '%s' started</h2>", $suite->getName());
    }
    public function endTestSuite(PHPUnit_Framework_TestSuite $suite){
    }
}
?>
 
Then use it like this:

Code: Select all

 
class TestTest extends PHPUnit_Framework_TestCase {
    public function testTest() {
    }
}
 
$suite = new PHPUnit_Framework_TestSuite('TestTest');
$result = new PHPUnit_Framework_TestResult;
$result->addListener(new SimpleTestListener());
$suite->run($result);
 
But my next questions are:

1) Which class/method I need to extend to output the assertion message in the format I want. For example, when this assertion fails:

$this->assertTrue(false, "My assertion X failed");

it would currently output:

My assertion X failed Failed asserting that is true.

2) How to make the listener respond to successful assertions

Thanks
foobaa
Forum Commoner
Posts: 40
Joined: Tue Feb 13, 2007 10:36 am

Re: PHPUnit Hello world not working

Post by foobaa »

An addition to my hello world questions, say an assertEquals were to fail, how can I get access to the $actual and $expected arguments, outputting them in the test? I've dug around in the api documentation and it's a minefield trying to work out the best way of doing it. I'm considering overriding PHPUnit_Framework_TestCase::assertThat(), maybe? A total hack and I really don't like it! Surely there's a pre-built way of outputting expected and actual values on a fail? I'm seriously considering dumping PHPUnit in favour of an ultra simple home-brewed class. For a one-man project I'm really not feeling it!! PHPUnit seems like overkill?? I could have written a simple testing class, a test testing class and a test testing test class in this time.
foobaa
Forum Commoner
Posts: 40
Joined: Tue Feb 13, 2007 10:36 am

Re: PHPUnit Hello world not working

Post by foobaa »

Ugh Ok lol nevermind!!

Thanks for all the responses lol but I'm going to use SimpleTest instead :o)
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: PHPUnit Hello world not working

Post by Ollie Saunders »

Good choice. That <span style='color:blue' title='I&#39;m naughty, are you naughty?'>smurf</span> looks really bad.
Post Reply