php 我如何运行我所有的 PHPUnit 测试?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1414125/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:28:08  来源:igfitidea点击:

How do I run all my PHPUnit tests?

phplinuxunit-testingubuntuphpunit

提问by JJ.

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?

我有一个名为 Script.php 的脚本,并在 Tests/Script.php 中对其进行了测试,但是当我运行 phpunit Tests 时,它不会在我的测试文件中执行任何测试。如何使用 phpunit 运行所有测试?

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

PHPUnit 3.3.17,PHP 5.2.6-3ubuntu4.2,最新的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

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

Tests/Script.php

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

采纳答案by JJ.

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

我创建了以下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>

回答by Mabrouk

Php test's filename must end with Test.php

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

phpunit mydirwill run all scripts named xxxxTest.phpin directory mydir

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

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

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

回答by Dean Rather

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

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

回答by JasonWoof

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?

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

回答by Vinesh

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