如何运行所有的PHPUnit测试?

52

我有一个名为Script.php的脚本以及Tests/Script.php中的测试,但是当我运行phpunit Tests时,它没有执行我的测试文件中的任何测试。如何使用phpunit运行所有的测试?

PHPUnit 3.3.17,PHP 5.2.6-3ubuntu4.2,最新版Ubuntu

输出:

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

以下是我的脚本文件和测试文件:

Script.php

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

测试/脚本.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());
    }
}  
?>
5个回答

63

Php测试文件的文件名必须以Test.php结尾。

phpunit mydir 将运行目录mydir中名为xxxxTest.php的所有脚本。

(似乎在phpunit文档中没有描述这一点)


这不是必须的。如果您的测试文件以“TestCase.php”结尾,您可以指定--test-suffix“TestCase.php”,但是默认情况下,当我们未在命令行中指定后缀时,phpunit只接受后缀为“Test.php”。 - kaushik

39

我创建了以下的phpunit.xml文件,现在至少我可以在根目录下执行phpunit --configuration phpunit.xml命令来运行位于Tests/目录下的测试。

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

11

我认为PHPUnit决定是否自动运行它,必须遵循文件名约定:somethingTest.php。


3

你觉得他们会有文档记录这个。我刚刚查看了手册,他们说可以传递一个目录,但并没有说明如何实现。

也许你的类名必须与测试脚本文件名的基本名称(除了“.php”)匹配?


-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;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接