views:

905

answers:

6

I have script called Script.php and tests for it in Tests/Script.php, but when I run phpunit Tests it does not execute any tests in my test file. How do I run all my tests with phpunit?

PHPUnit 3.3.17, PHP 5.2.6-3ubuntu4.2, latest Ubuntu

Output:

$ phpunit Tests
PHPUnit 3.3.17 by Sebastian Bergmann.
Time: 0 seconds
OK (0 tests, 0 assertions)

And here are my script and test files:

Script.php

<?php  
function returnsTrue() {  
    return TRUE;  
}  
?>

Tests/Script.php

<?php  
require_once 'PHPUnit/Framework.php';  
require_once 'Script.php'  

class TestingOne extends PHPUnit_Framework_TestCase  
{

    public function testTrue()
    {
        $this->assertEquals(TRUE, returnsTrue());
    }

    public function testFalse()
    {
        $this->assertEquals(FALSE, returnsTrue());
    }
}

class TestingTwo extends PHPUnit_Framework_TestCase  
{

    public function testTrue()  
    {  
        $this->assertEquals(TRUE, returnsTrue());  
    }

    public function testFalse()
    {
        $this->assertEquals(FALSE, returnsTrue());
    }
}  
?>
A: 

man, you think they would have documented this. I just looked through the manual, and they say you can pass a directory, but not really how to do it.

Perhaps your class name has to match the basename (everything but the ".php") of your test scripts filename?

JasonWoof
A: 

Edit: I use this with SimpleTest... not sure if it will work with PHPUnit.

I use this script to run all my test files.

<?php
  $testdir = "{$_SERVER['DOCUMENT_ROOT']}/tests"; 
  $contents = scandir($testdir);
  $pattern = "/test\w*\.php$/i";
  foreach ($contents as $filename) {
    if (preg_match($pattern, $filename) > 0) {
      include "http://{$_SERVER['SERVER_NAME']}/tests/$filename";
    }  
  } 
?>
Lawrence Barsanti
+4  A: 

I created following phpunit.xml and now atleast I can do phpunit --configuration phpunit.xml in my root directory to run the tests located in Tests/

<phpunit backupGlobals="false"
         backupStaticAttributes="false"
         syntaxCheck="false">
  <testsuites>
    <testsuite name="Tests">
      <directory suffix=".php">Tests</directory>
    </testsuite>
  </testsuites>
</phpunit>
JJ
A: 

I think forPHPUnit to decide to automatically run it it must follow a filename convention: somethingTest.php.

Dean
A: 
Vinesh
A: 

Php test's filename must end by Test.php

phpunit mydir will run all scripts named xxxxTest.php in directory mydir

(look likes it's not discribed in documentation)

Mabrouk