52

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());
    }
}  
?>
JJ.
  • 1,099
  • 1
  • 8
  • 12

5 Answers5

63

Php test's filename must end with Test.php

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

(looks likes it's not described in the phpunit documentation)

Timo Huovinen
  • 53,325
  • 33
  • 152
  • 143
Mabrouk
  • 699
  • 6
  • 2
  • Its not a must. You can specify --test-suffix "TestCase.php" if your test files are ending with "TestCase.php", but by default phpunit will only accept suffix as "Test.php" when we are not specifying a suffix in the command line – kaushik Jan 02 '15 at 02:53
39

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.
  • 1,099
  • 1
  • 8
  • 12
11

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

Dean Rather
  • 31,756
  • 15
  • 66
  • 72
3

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?

Joseph at SwiftOtter
  • 4,276
  • 5
  • 37
  • 55
JasonWoof
  • 4,176
  • 1
  • 19
  • 28
-6
<?php
//Files required for phpunit test
require_once 'PHPUnit/Framework.php';
//Knowing the drupal environment
require_once './includes/bootstrap.inc';     //initialize the Drupal framework
//Loading the drupal bootstrap
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
//Helper file
include_once 'helper.inc';
//Including inc file of addresses module
include_once(module_load_include('inc','addresses_user','addresses_user'));

class addresses_test extends PHPUnit_Framework_TestCase {

protected $uid;

protected function setUp()
{
    $this->uid = 1;
}
MartyIX
  • 27,828
  • 29
  • 136
  • 207
Vinesh
  • 1
  • 3