【问题标题】:How do I run all my PHPUnit tests?如何运行我所有的 PHPUnit 测试?
【发布时间】:2010-11-27 16:47:25
【问题描述】:

我有一个名为 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;  
}  
?>

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());
    }
}  
?>

【问题讨论】:

    标签: php linux unit-testing ubuntu phpunit


    【解决方案1】:

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

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

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

    【讨论】:

    • 这不是必须的。如果您的测试文件以“TestCase.php”结尾,您可以指定 --test-suffix "TestCase.php",但默认情况下,当我们没有在命令中指定后缀时,phpunit 只会接受后缀为“Test.php”行
    【解决方案2】:

    我在 phpunit.xml 之后创建了,现在我至少可以在我的根目录中执行 phpunit --configuration phpunit.xml 来运行位于 Tests/ 中的测试 p>

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

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

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

        【讨论】:

          【解决方案5】:
          <?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;
          }
          

          【讨论】:

            猜你喜欢
            • 2020-04-28
            • 2015-03-20
            • 2017-02-14
            • 1970-01-01
            • 2013-10-12
            • 2011-05-02
            • 2014-05-31
            • 2011-09-21
            • 2012-01-08
            相关资源
            最近更新 更多