我正在尝试从我的测试套件运行所有测试,但当我运行命令 phpunit
时,PHPUnit 找不到测试。我在 phpunit.xml 中配置测试套件。
phpunit.xml
<?xml version="1.0" encoding="UTF-8" ?>
<phpunit backupGlobals="true"
backupStaticAttributes="false"
bootstrap="./bootstrap.php"
cacheTokens="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
forceCoversAnnotation="false"
mapTestClassNameToCoveredClassName="false"
processIsolation="false"
stopOnError="false"
stopOnFailure="false"
stopOnIncomplete="false"
stopOnSkipped="false"
strict="false"
verbose="true">
<testsuites>
<testsuite name="All Tests">
<directory suffix="*.php">.</directory>
</testsuite>
</testsuites>
</phpunit>
bootstrap.php
<?php
error_reporting(E_ALL | E_STRICT);
date_default_timezone_set('America/Sao_Paulo');
require_once(dirname(__FILE__) . '/WebTestCase.php');
WebTestCase.php
<?php
define('TEST_BASE_URL', 'http://localhost:8080');
class WebTestCase extends PHPUnit_Extensions_SeleniumTestCase
{
protected function setUp() {
$this->setBrowser('*firefox');
$this->setBrowserUrl(TEST_BASE_URL);
$this->setHost('localhost');
$this->setTimeOut(30);
}
}
测试页面.php
class TestPage extends WebTestCase
{
public function testTitle()
{
$this->open("/");
$title = $this->getTitle();
$this->assertEquals('Home', $title);
}
}
如果我运行 phpunit 传递文件测试,如 phpunit TestPage.php
,就可以了。
最佳答案
正如您在 documentation 中所读到的那样:
Note If you point the PHPUnit command-line test runner to a directory it will look for *Test.php files.
采用这种格式进行测试类(class)是一种很好的做法。但是,如果这不适合您,您可以通过创建 phpunit.xml
文件并正确设置它来更改此行为:
<?xml version="1.0" encoding="utf-8" ?>
<phpunit>
<testsuite name='Name your suite'>
<directory suffix=".php">/path/to/files</directory>
</testsuite>
</phpunit>
请注意,我删除了 *
。理论上,phpunit应该遍历该目录并执行文件末尾带有 .php
的所有文件。
我认为如果您删除 *
并设置正确的路径,它应该可以工作。
关于php - 如何在 PHPUnit 中执行所有测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28073239/