开放的编程资料库

当前位置:我爱分享网 > PHP教程 > 正文

设置 Zend_Test 测试套件

现在Zend_Test已经发布,开发人员当然会问,“我如何设置我的测试套件?”幸运的是,在与我的同事讨论并在我的测试套件上进行了一些试验之后,我现在可以回答这个问题。

PHPUnit提供了多种设置测试套件的方法,有些简单,有些复杂。例如,ZendFramework测试套件采用更复杂的路线,添加需要大量初始设置的组件级套件,但允许我们进行相当细粒度的控制。

但是,测试和测试自动化应该很容易,而复杂的方法对于我们的大多数应用程序来说是多余的。幸运的是,PHPUnit提供了一些其他方法,使这样做相对简单。最简单的方法是使用XML配置文件。

例如,请考虑以下内容:

<phpunit>
    <testsuite name="My Test Suite">
        <directory>./</directory>
    </testsuite>

    <filter>
        <whitelist>
            <directory suffix=".php">../library/</directory>
            <directory suffix=".php">../application/</directory>
            <exclude>
                <directory suffix=".phtml">../application/</directory>
            </exclude>
        </whitelist>
    </filter>

    <logging>
        <log type="coverage-html" target="./log/report" charset="UTF-8"
            yui="true" highlight="true"
            lowUpperBound="50" highLowerBound="80"/>
        <log type="testdox-html" target="./log/testdox.html" />
    </logging>
</phpunit>

首先要注意的是,相对路径是相对于配置文件的。这允许您从测试树中的任何位置运行测试。其次,为testsuite指令提供一个directory指令扫描该目录中所有以Test.php结尾的文件,这意味着您不必手动保留测试用例列表。这是自动化套件的好方法。第三,过滤器指令允许我们确定要从覆盖率报告中包含和/或排除哪些类。最后,logging指令让我们指定要创建的日志类型和位置。

将以上内容放入应用程序的tests/phpunit.xml中,您可以使用以下命令开始编写测试用例并立即运行套件:

$ phpunit --configuration phpunit.xml

我喜欢按类型对测试用例进行分组。我有控制器、模型,通常还有库代码,需要在文件系统上组织测试以及运行实际测试。我做了两件事来促进这一点。

首先,我创建目录。例如,我的测试套件中有以下层次结构:

tests/
    phpunit.xml
    TestHelper.php
    controllers/
        IndexControllerTest.php (contains IndexControllerTest)
        ErrorControllerTest.php (contains ErrorControllerTest)
        ...
    models/
        PasteTest.php           (contains PasteTest)
        DbTable/
            PasteTest.php       (contains DbTable_PasteTest)
        ...
    My/
        Form/
            Element/
                SimpleTextareaTest.php

controllers/包含我的控制器,models/包含我的模型。如果我正在开发模块化应用程序,我会使用类似blog/controllers/的东西。库代码的层次结构与我的library/目录中的层次结构相同。

其次,我使用文档块注释对我的测试进行分组。我将以下内容添加到我的控制器测试用例中的myclass-leveldocblock:

/**
 * @group Controllers
 */

模型获得注释@groupModels等。这允许我按需运行单独的测试集:

$ phpunit --configuration phpunit.xml --group=Controllers

您可以指定多个@group注解,这意味着您可以将测试分成模块、发布报告标识符等;此外,您可以将注释添加到各个测试方法本身,以具有真正细粒度的测试运行能力。

精明的读者会注意到前面那个目录列表中的TestHelper.php文件,并且会想知道那是什么。

测试套件需要一些环境信息,就像您的应用程序一样。它可能需要默认的数据库适配器、更改的include_path、自动加载设置等等。这是我的TestHelper.php的样子:

<?php
/*
 * Start output buffering
 */
ob_start();

/*
 * Set error reporting to the level to which code must comply.
 */
error_reporting( E_ALL | E_STRICT );

/*
 * Set default timezone
 */
date_default_timezone_set('GMT');

/*
 * Testing environment
 */
define('APPLICATION_ENV', 'testing');

/*
 * Determine the root, library, tests, and models directories
 */
$root        = realpath(dirname(__FILE__) . '/../');
$library     = $root . '/library';
$tests       = $root . '/tests';
$models      = $root . '/application/models';
$controllers = $root . '/application/controllers';

/*
 * Prepend the library/, tests/, and models/ directories to the
 * include_path. This allows the tests to run out of the box.
 */
$path = array(
    $models,
    $library,
    $tests,
    get_include_path()
);
set_include_path(implode(PATH_SEPARATOR, $path));

/**
 * Register autoloader
 */
require_once 'Zend/Loader.php';
Zend_Loader::registerAutoload();

/**
 * Store application root in registry
 */
Zend_Registry::set('testRoot', $root);
Zend_Registry::set('testBootstrap', $root . '/application/bootstrap.php');

/*
 * Unset global variables that are no longer needed.
 */
unset($root, $library, $models, $controllers, $tests, $path);

上面确保我的APPLICATION_ENV常量设置正确,错误报告适合测试(即,我想看到所有错误),并且自动加载是启用。此外,我在我的注册表中放置了几个项目——引导程序和测试根目录。

在每个测试用例文件中,我然后对该文件执行require_once。在PHPUnit的未来版本中,您将能够在您的配置XML中指定一个引导文件,为每个测试用例提取该文件,您甚至能够进一步自动化您的测试环境设置。

希望这能帮助您开始应用程序测试;你还在等什么?

未经允许不得转载:我爱分享网 » 设置 Zend_Test 测试套件

感觉很棒!可以赞赏支持我哟~

赞(0) 打赏