我有一个项目,我试图运行单元测试,但没有成功。这很可能与自动加载有关。
$ phpunit tests致命错误:类'Foo'Bar'在/path/tests/Foo/BarTest.php中找不到在X线上
这一行执行新的Bar();
我有一个phpunt .xml在测试目录中,像这样
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="../tests/bootstrap.php"
backupGlobals="false"
backupStaticAttributes="false"
verbose="true">
<testsuites>
<testsuite name="App">
<directory suffix="Test.php">tests/Foo</directory>
</testsuite>
</testsuites>
<logging>
<log type="coverage-html" target="build/coverage"/>
<log type="coverage-clover" target="build/logs/clover.xml"/>
<log type="coverage-crap4j" target="build/logs/crap4j.xml"/>
<log type="junit" target="build/logs/junit.xml" logIncompleteSkipped="false"/>
</logging>
<filter>
<whitelist addUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
<exclude>
<file>src/bootstrap.php</file>
</exclude>
</whitelist>
</filter>
</phpunit>
结构是src/Foo/写到测试/Foo/BarTest.php
我的测试/bootstrap.php
if ( ! file_exists($file = __DIR__.'/../vendor/autoload.php')) {
throw new RuntimeException('Install dependencies to run this script.');
}
$loader = require_once $file;
$loader->add('Foo', __DIR__ . '../src/');
所以我将Foo命名空间映射到src目录,但PHPUnit似乎找不到它。
这个自动加载是Composer生成的。
我发现了这个问题。由于路径问题,PHPUnit没有找到引导程序,因此没有加载Composer的自动加载。
因为它没有显示任何掩盖真正问题的警告。
所以假设你要在tests文件夹中运行测试你应该像这样指向phpunt。xml
测试/phpunit.xml
<phpunit bootstrap="bootstrap.php" ...
所以bootstrap.php也应该在tests文件夹中