我刚刚通过Composer安装了Sebastian Bergmann的PHPUnit版本3.7.19,并编写了一个我想要进行单元测试的类。
我希望我的所有类都自动加载到每个单元测试中,而不必在我的测试中使用include
或require
,但这被证明是困难的!
下面是我的目录结构(尾随的/斜杠表示目录,而不是文件):
- composer/
- phpunit/
- symfony/
- autoload.php
我的composer.json文件包含以下内容:
"require": {
"phpunit/phpunit": "3.7.*",
"phpunit/phpunit-selenium": ">=1.2"
}
我的returning.php类文件包括以下内容:
<?php
class Returning {
public $var;
function __construct(){
$this->var = 1;
}
}
?>
我的returningTest.php测试文件包括以下内容:
<?php
class ReturningTest extends PHPUnit_Framework_TestCase
{
protected $obj = null;
protected function setUp()
{
$this->obj = new Returning;
}
public function testExample()
{
$this->assertEquals(1, $this->obj->var);
}
protected function tearDown()
{
}
}
?>
但是,当我从命令行运行./vendor/bin/phpunit tests
时,我得到以下错误:
PHP致命错误:在第8行的/files/code/php/db/test/Returning ingTest.php中找不到类'Returning‘
我注意到composer
在vendor/autoload.php
中生成了一个autoload.php
文件,但不确定这是否与我的问题相关。
此外,在其他一些关于堆栈溢出的回答中,人们提到了一些关于在composer中使用PSR-0和在PHP中使用namespace
命令的内容,但我都没有成功地使用过这两个命令。
请帮帮我!我只想在PHPUnit中自动加载我的类,这样我就可以使用它们来创建对象,而不用担心include
或require
。
更新:2013年8月14日
现在,我已经创建了一个名为PHPUnit Skeleton的开源项目,以帮助您轻松启动和运行项目的PHPUnit测试。
发布于 2013-03-30 04:58:53
嗯,一开始。你需要告诉自动加载器在哪里可以找到一个类的php文件。这是通过遵循PSR-0标准完成的。
最好的方法是使用名称空间。当您请求Acme\Tests\ReturningTest
类时,自动加载器将搜索Acme/Tests/ReturningTest.php
文件。有一些很棒的命名空间教程,只需搜索和阅读即可。请注意,名称空间不是PHP中用于自动加载的东西,而是可以用于自动加载的东西。
Composer带有一个标准的PSR-0自动加载器(vendor/autoload.php
中的那个)。在您的示例中,您希望告诉自动加载器在lib
目录中搜索文件。然后,当您使用ReturningTest
时,它将查找/lib/ReturningTest.php
。
将此代码添加到您的composer.json
{
...
"autoload": {
"psr-0": { "": "lib/" }
}
}
有关更多信息,请访问the documentation。
现在,自动加载器可以找到您的类,您需要让PHPunit知道在运行测试之前有一个文件要执行:引导程序文件。您可以使用--bootstrap
选项指定引导程序文件的位置:
$ ./vendor/bin/phpunit tests --bootstrap vendor/autoload.php
但是,使用PHPunit configuration file更好
<!-- /phpunit.xml.dist -->
<?xml version="1.0" encoding="utf-8" ?>
<phpunit bootstrap="./vendor/autoload.php">
<testsuites>
<testsuite name="The project's test suite">
<directory>./tests</directory>
</testsuite>
</testsuites>
</phpunit>
现在,您可以运行该命令,它将自动检测配置文件:
$ ./vendor/bin/phpunit
如果将配置文件放入另一个目录,则需要将该目录的路径放入带有-c
选项的命令中。
发布于 2013-03-30 15:28:06
Update2另一种更简单的替代方法是在composer.json
(reference)中使用autoload-dev
指令。好处是你不需要维护两个bootstrap.php (一个用于prod,一个用于dev)来自动加载不同的类。
{
"autoload": {
"psr-4": { "MyLibrary\\": "src/" }
},
"autoload-dev": {
"psr-4": { "MyLibrary\\Tests\\": "tests/" }
}
}
更新 Wouter J的答案更完整。但我的可以帮助那些想要在tests/
文件夹中设置PSR-0自动加载的人。
Phpunit使用此模式*Test.php
扫描所有文件。所以我们不需要自己自动加载它们。但我们仍然希望自动加载tests/
下的其他支持类,如fixture/stub或一些父类。
一种简单的方法是查看Composer项目本身是如何设置phpunit测试的。这实际上非常简单。请注意带有"bootstrap“的行。
参考:https://github.com/composer/composer/blob/master/phpunit.xml.dist
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
bootstrap="tests/bootstrap.php"
>
<testsuites>
<testsuite name="Composer Test Suite">
<directory>./tests/Composer/</directory>
</testsuite>
</testsuites>
<groups>
<exclude>
<group>slow</group>
</exclude>
</groups>
<filter>
<whitelist>
<directory>./src/Composer/</directory>
<exclude>
<file>./src/Composer/Autoload/ClassLoader.php</file>
</exclude>
</whitelist>
</filter>
</phpunit>
参考:https://github.com/composer/composer/blob/master/tests/bootstrap.php
<?php
/*
* This file is part of Composer.
*
* (c) Nils Adermann <naderman@naderman.de>
* Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
error_reporting(E_ALL);
$loader = require __DIR__.'/../src/bootstrap.php';
$loader->add('Composer\Test', __DIR__);
上面的最后一行是在名称空间Composer\Test下自动加载phpunit测试类。
发布于 2013-09-29 23:42:43
这些答案都不是我想要的。是的,PHPUnit加载测试文件,但不加载存根/装置。Chaun Ma的回答没有解决这个问题,因为运行vendor/bin/phpunit
已经包含了自动加载,所以没有办法让自动加载器的实例在这一点上将更多的路径推送到它的堆栈。
我最终在文档中找到了这个:
如果您需要在多个目录中搜索相同的前缀,您可以将它们指定为一个数组,如下所示:
{“自动加载”:{ "psr-0":{ "Monolog\":"src/","lib/“}
https://stackoverflow.com/questions/15710410
复制相似问题