48

我有一个名为 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)

这是我的脚本和测试文件:

脚本.php

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

测试/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());
    }
}  
?>
4

5 回答 5

60

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

phpunit mydirxxxxTest.php将运行目录 mydir 中 命名的所有脚本

(看起来它没有在 phpunit 文档中描述)

于 2010-10-15T13:40:46.467 回答
40

我创建了以下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>
于 2009-09-12T14:24:37.633 回答
10

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

于 2009-09-14T02:32:48.513 回答
3

你认为他们会记录下来。我只是浏览了手册,他们说你可以传递一个目录,但不是真的如何去做。

也许您的类名必须与测试脚本文件名的基本名称(除了“.php”之外的所有内容)匹配?

于 2009-09-12T04:06:19.370 回答
-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;
}
于 2010-04-23T07:47:12.023 回答