【问题标题】:PHPUnit does not open files without ".php" suffixPHPUnit 不打开没有“.php”后缀的文件
【发布时间】:2020-09-03 20:46:26
【问题描述】:

我决定在我的项目中使用 PHPUnit 进行测试。为了做到这一点,我从 Composer 的官方站点安装了 Composer(它是最新版本的 Composer;我提早了一点,以便您可以理解更新 Composer 不会解决问题)并安装了 PHPUnit Composer 在命令行中的帮助。我的composer.json 看起来像这样:

{
    "autoload": {
        "classmap": [
            ""
        ]
    },
    "require-dev": {
        "phpunit/phpunit": "^9"
    }
}

我也有phpunit.xml.dist。它看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php">
    <testsuites>
        <testsuite name="Components">
            <directory suffix="Test.php">./tests/components/</directory>
        </testsuite>
    </testsuites>
</phpunit>

从这段代码可以看出,我的代码按以下方式组织:

--folderForClasses
--anotherFolderForClasses
--OneMoreFolder
--vendor
   `--autoload.php
--tests
   `--components
         `--TokenGeneratorTest.php
--composer.json
--phpunit.xml.dist

可以执行如下命令:

  1. vendor\bin\phpunit tests
  2. vendor\bin\phpunit tests/components
  3. vendor\bin\phpunit tests/components/TokenGeneratorTest.php
  4. vendor\bin\phpunit --testsuite Components

因为只有一个测试文件,所有的结果如下:

PHPUnit 9.0.0 by Sebastian Bergmann and contributors.

...                                                                 3 / 3 (100%)

Time: 00:00.066, Memory: 4.00 MB

OK (3 tests, 3 assertions)

但是,我不能执行如下命令:

  1. vendor\bin\phpunit TokenGeneratorTest
  2. vendor\bin\phpunit tests/components/TokenGeneratorTest

这两个命令导致:

PHPUnit 9.0.0 by Sebastian Bergmann and contributors.

Cannot open file "TokenGeneratorTest".

还有:

PHPUnit 9.0.0 by Sebastian Bergmann and contributors.

Cannot open file "tests/components/TokenGeneratorTest".

我在几个教程中看到像最后两个这样的命令应该可以工作,但由于某种原因它们不起作用。这可能是什么原因以及如何使其发挥作用?

非常感谢。

【问题讨论】:

  • 我不确定最后两个工作是否有任何理由。您正在提供一条不存在的路径。至少在前两个示例中,您提供了一个 phpunit 可以扫描的目录。

标签: php testing phpunit


【解决方案1】:

我不确定您为什么要获得 v9.0.0(您今天应该获得带有该要求的 v9.3.8),但似乎在 v9.0.0 中删除了提供不带扩展名的测试文件名的能力.您看到的是较旧的教程。

来自 8.5.8 的帮助:

PHPUnit 8.5.8 by Sebastian Bergmann and contributors.

Usage:
  phpunit [options] UnitTest [UnitTest.php]
  phpunit [options] <directory>

来自 9.0.0 的帮助:

PHPUnit 9.0.0 by Sebastian Bergmann and contributors.

Usage:
  phpunit [options] UnitTest.php
  phpunit [options] <directory>

【讨论】:

  • 我也不知道为什么我的PHPUnit是v9.0.0。无论如何,谢谢你的帮助,这是一个非常有用的评论。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-27
相关资源
最近更新 更多