【发布时间】:2011-08-31 01:29:11
【问题描述】:
我一直在寻找帮助我将 PHPUnit 与 CakePHP 集成的教程。也希望使用 Selenium 测试,所以更喜欢 PHPUnit。
我一直在尝试按照http://cakebaker.42dh.com/2006/03/22/selenium/ 上的教程进行操作,但似乎无法正常工作。有什么好的教程吗?
谢谢!
【问题讨论】:
标签: unit-testing cakephp phpunit cakephp-1.3
我一直在寻找帮助我将 PHPUnit 与 CakePHP 集成的教程。也希望使用 Selenium 测试,所以更喜欢 PHPUnit。
我一直在尝试按照http://cakebaker.42dh.com/2006/03/22/selenium/ 上的教程进行操作,但似乎无法正常工作。有什么好的教程吗?
谢谢!
【问题讨论】:
标签: unit-testing cakephp phpunit cakephp-1.3
这相对容易。我使用作曲家安装的 cake 1.3。这就是我的 composer.json 的样子:
{
"config": {
"vendor-dir": "vendors/composer"
},
"require": {
"phpunit/phpunit": "3.7.*",
"cakephp/cakephp-1.3": "1.3",
},
"repositories": [
{
"type": "package",
"package": {
"name": "cakephp/cakephp-1.3",
"version": "1.3",
"source": {
"url": "https://github.com/cakephp/cakephp.git",
"type": "git",
"reference": "1.3"
}
}
}
]
}
然后是tests目录下的phpunit bootstrap.php文件:
<?php
include('../vendors/composer/autoload.php');
include('../webroot/index.php');
这是 phpunit.xml 来自同一个目录:
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
bootstrap="bootstrap.php"
backupStaticAttributes="false"
cacheTokens="false"
colors="false"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
forceCoversAnnotation="false"
mapTestClassNameToCoveredClassName="false"
printerClass="PHPUnit_TextUI_ResultPrinter"
processIsolation="false"
stopOnError="false"
stopOnFailure="false"
stopOnIncomplete="false"
stopOnSkipped="false"
testSuiteLoaderClass="PHPUnit_Runner_StandardTestSuiteLoader"
strict="false"
verbose="false"
>
<testsuites>
<testsuite name="AllTests">
<directory>.</directory>
</testsuite>
</testsuites>
<filter>
<blacklist>
<directory suffix=".php"></directory>
<file></file>
<exclude>
<directory suffix=".php"></directory>
<file></file>
</exclude>
</blacklist>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php"></directory>
<file></file>
<exclude>
<directory suffix=".php"></directory>
<file></file>
</exclude>
</whitelist>
</filter>
</phpunit>
不要忘记在测试设置中加载您的应用程序类。你可以用 cakephp 的方式来做。例如,如果您的控制器名为 calendar,您的 calendarTest.php 可能如下所示:
<?php
/**
* Class ComponentsCommonTest
* @property calendarController $calendarController
*/
class CalendarTest extends PHPUnit_Framework_TestCase
{
/**
* @var calendarController $calendarController
*/
private $calendarController;
function setUp()
{
App::import('Core', array('View', 'Controller', 'Model', 'Router'));
App::import('Controller', 'Calendar');
$this->calendarController =& new CalendarController();
$this->calendarController->constructClasses();
$this->calendarController->layout = null;
}
}
模型、供应商类别等也是如此。非常适合我。
【讨论】:
不幸的是,CakePHP 不是为与 PHPUnit 一起工作而设计的。 CakePHP 已切换到使用SimpleTest,您将有两种选择之一,重构您的测试以使用 SimpleTest 或修改核心以使用 PHPUnit。
但是应该声明Mark Story has stated that CakePHP 2.0 will use PHPUnit 是它的测试框架,所以如果你可以等到那时,那可能是最好的选择。
【讨论】: