Page 1 of 1

PHPUnit Hello world not working

Posted: Thu Aug 20, 2009 12:40 pm
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);
?>
 

Re: PHPUnit Hello world not working

Posted: Thu Aug 20, 2009 4:59 pm
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

Re: PHPUnit Hello world not working

Posted: Fri Aug 21, 2009 3:10 am
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.

Re: PHPUnit Hello world not working

Posted: Fri Aug 21, 2009 3:38 am
by foobaa
Ugh Ok lol nevermind!!

Thanks for all the responses lol but I'm going to use SimpleTest instead :o)

Re: PHPUnit Hello world not working

Posted: Mon Sep 07, 2009 11:09 pm
by Ollie Saunders
Good choice. That <span style='color:blue' title='I&#39;m naughty, are you naughty?'>smurf</span> looks really bad.