我无法使用 phpunit 进行测试:无法打开文件“自动加载.php”


I can't test with phpunit: Cannot open file "autoload.php"


配置 :

  • PHPUNIT: 4.5.0
  • PHP : 5.4.12
  • 服务器:万普
  • 作曲家: 版本 1.0-dev 2015-02-17 21:55:44

作曲家.json :

{
    "require-dev": {
        "phpunit/phpunit": "4.5.*"
    }
}

自动加载.php :

<?php
date_default_timezone_set("Europe/Paris");
require __DIR__.'/vendor/Symfony/Component/ClassLoader/UniversalClassLoader.php';
use Symfony'Component'ClassLoader'UniversalClassLoader;
$loader = new UniversalClassLoader();
$loader->registerNamespaces(array(
    'Hangman' => __DIR__.'/src',
    'Symfony' => __DIR__.'/vendor',
));
$loader->register();

phpunit.xml :

<?xml version="1.0" encoding="UTF-8"?>
<!-- http://phpunit.de/manual/4.1/en/appendixes.configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.1/phpunit.xsd"
         backupGlobals="false"
         colors="true"
         bootstrap="autoload.php"
        >
    <testsuites>
        <testsuite name="hangman">
            <directory>tests/Hangman/Tests</directory>
        </testsuite>
    </testsuites>
    <filter>
        <blacklist>
            <directory>vendor</directory>
        </blacklist>
    </filter>
</phpunit>

问题:

我执行了:phpunit --bootstrap autoload.php tests

我的错误:Cannot open file "autoload.php"

你可以帮我吗?

我正在使用 PHPUnit 7,并在此基础上我在下面提到了经过测试并 100% 工作

的解决方案

如果您是Windows用户,请在cmd上键入以下内容:"vendor/bin/phpunit" --bootstrap ./vendor/autoload.php ./tests/EmailTest

确保我在供应商和测试之前更新了 ./。

你可以在

你的phpunit.xml中尝试bootstrap="vendor/autoload.php"。然后 PHPUnit 和你的测试将使用 Composer 自动加载器。

或者,您可以通过添加require 'vendor/autoload.php';来要求autoload.php中加入作曲家自动加载器(除了Symfony的UCL(。这会导致注册两个自动加载器。

然后运行:

  1. composer update - 获取依赖项并重建自动加载文件
  2. phpunit - 执行测试运行

你不需要用--bootstrap运行phpunit,因为指令已经在你的phpunit.xml中设置了。


我认为您的文件夹布局不正确。你从这个开始:

c:'wamp'www'yourproject
  'src
  'tests
    '- phpunit.xml.dist
  'vendor
  'composer.json
  1. phpunit添加到作曲家的require-dev部分.json
  2. 添加"bin-dir" : "bin" ,以便 phpunit.bat 存在于c:'wamp'www'yourproject'bin'phpunit.bat而不是c:'wamp'www'yourproject'vendor'bin'phpunit.bat
  3. composer install或更新
  4. 执行c:'wamp'www'yourproject'bin'phpunit.bat -c c:'wamp'www'yourproject'tests'phpunit.xml.dist