我决定在项目中使用PHPUnit进行测试。为了做到这一点,我从Composer的官方网站上安装了Composer(它是最新版本的Composer;我提前一点提到了,这样你就可以理解更新Composer不会解决问题(,并在Composer的帮助下在命令行中安装了PHPUnit。我的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
ICAN执行命令,如:
vendorbinphpunit tests
vendorbinphpunit tests/components
vendorbinphpunit tests/components/TokenGeneratorTest.php
vendorbinphpunit --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)
但是,我不能执行命令,如:
vendorbinphpunit TokenGeneratorTest
vendorbinphpunit 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".
我在几篇教程中看到,像前两篇这样的命令应该可以工作,但由于某些原因,它们不起作用。这可能是什么原因,以及如何使其发挥作用?
提前非常感谢。
我不知道你为什么会得到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>