php - 如何在 PHPUnit 中执行所有测试?

标签 php testing selenium phpunit selenium-rc

我正在尝试从我的测试套件运行所有测试,但当我运行命令 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,就可以了。

enter image description here

最佳答案

正如您在 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/

相关文章:

php - 使用 PHP 在浏览器中预览 PDF 文件

php - 内部联接如何使用 Doctrine 和 Symfony2 处理多对多关系

selenium - 我可以在计算机 A 上运行 Selenium,并看到计算机 B 中的浏览器吗?

firefox - Selenium IDE : How to detect secure cookies on page loaded with http://?

selenium - assertValue 不适用于带有 Selenium IDE Firefox 插件的选择框

php - 我应该在 PHP 托管中遇到哪些安全问题

php - 使用 Temando API 创建预订

ruby-on-rails - 发布到创建方法会导致 session Controller 测试 (RSpec) 出错

unit-testing - 测试驱动的 SharePoint 开发有哪些好书和在线资源?

ruby-on-rails - 具有追随者/友谊的 RSpec 测试有很多通过用户