PHPUnit-使用配置文件时“未执行测试" [英] PHPUnit - 'No tests executed' when using configuration file
问题描述
为了提高代码质量,我决定尝试学习如何使用单元测试而不是平庸的最佳测试解决方案来测试代码.
To improve my quality of code, I've decided to try to learn how to test my code using Unit Testing instead of my mediocre-at-best testing solutions.
我决定使用composer为个人库安装PHPUnit,这使我可以实现常见的数据库功能.最初,我没有用于PHPUnit的配置文件,而当我运行以下命令时:
I decided to install PHPUnit using composer for a personal library that allows me to achieve common database functions. At first I didn't have a configuration file for PHPUnit and when I ran commands like:
$ phpunit tests/GeneralStringFunctions/GeneralStringFunctionsTest
请注意,这是一个终端命令,因此我没有包含.php
扩展名.上面提到的GeneralStringFunctionsTest实际上是一个GeneralStringFunctionsTest.php
文件.
Please note that this is a terminal command, so I didn't include the .php
extension. The GeneralStringFunctionsTest referred to above is actually a GeneralStringFunctionsTest.php
file.
输出是我期望的:
时间:31 ms,内存:2.75Mb
Time: 31 ms, Memory: 2.75Mb
好的(1个测试,1个断言)
OK (1 test, 1 assertion)
然后,我尝试使用配置文件自动加载测试套件,而不必每次都手动输入文件.我在根目录中创建了一个名为phpunit.xml
的文件,并将以下内容输入到该文件中: http://pastebin.com/0j0L4WBD :
I then tried to use a configuration file to automatically load the test suite instead of having to manually type in the file every time. I created a file called phpunit.xml
in my root directory, and entered the following into the file: http://pastebin.com/0j0L4WBD:
<?xml version = "1.0" encoding="UTF-8" ?>
<phpunit>
<testsuites>
<testsuite name="Tests">
<directory>tests</directory>
</testsuite>
</testsuites>
</phpunit>
现在,当我运行命令时:
Now, when I run the command:
phpunit
我得到以下输出:
Sebastian Bergmann和贡献者的PHPUnit 4.5.0.
PHPUnit 4.5.0 by Sebastian Bergmann and contributors.
从/Users/muyiwa/Projects/DatabaseHelper/phpunit.xml中读取的配置
Configuration read from /Users/muyiwa/Projects/DatabaseHelper/phpunit.xml
时间:16毫秒,内存:1.50Mb
Time: 16 ms, Memory: 1.50Mb
未执行测试!
如果有帮助,我的目录结构如下:
src-顶级目录(包含我的所有源代码)
测试-顶级目录(包含我所有的测试,其结构与我的 src 文件夹相同)
供应商-Composer第三方文件
In case it's helpful, my directory structure is as follows:
src - Top level directory (with all my source code)
tests - Top level directory (with all my tests, structured the same as my src folder)
vendor - Composer third party files
我还拥有composer json和lock文件,以及顶层phpunit xml文件作为文件.
I also have the composer json and lock file, as well as the phpunit xml file in the top level as files.
- 将
phpunit.xml
中的目录更改为tests/GeneralStringFunctions
- 将
phpunit.xml
中的目录更改为./tests
- 将
phpunit.xml
文件移动到tests
目录,然后将目录更改为./
而不是tests
. - 在
phpunit.xml
中的目录标记中添加后缀属性,以将测试"指定为显式后缀.
- Changing the directory in
phpunit.xml
totests/GeneralStringFunctions
- Changing the directory in
phpunit.xml
to./tests
- Moving the
phpunit.xml
file to thetests
directory and then changing the directory to be./
instead oftests
. - Adding a suffix attribute to the directory tag in
phpunit.xml
to specify "Tests" as the explicit suffix.
推荐答案
对于它的价值(很晚),我最近在为一个简单的网站制作一个新的Laravel 5.1项目时遇到了这个问题.我尝试调试它,并在尝试时感到困惑:
For what it's worth (being late), I ran into this recently while I was making a new Laravel 5.1 project for a simple website. I tried to debug it and was confused when I tried:
php artisan make:test homeTest
(具有默认测试,仅断言true为true)
(which has a default test that just asserts true is true)
看到了输出
No tests executed!
最终导致我遇到的问题与我的PHP安装有关–"phpunit"在全球范围内注册和配置不同,而Laravel安装随附的phpunit配置正确且运行完美.
What the problem ended up being for me was related to my PHP installation -- "phpunit" was globally registered and configured differently, whereas the phpunit that came with the Laravel installation was configured just right and ran perfectly.
因此,此修复程序正在运行供应商配置的phpunit(与app/和tests/相同的根目录):
So the fix is running the vendor's configured phpunit (from the same root directory as app/ and tests/):
./vendor/bin/phpunit
希望对别人有帮助!
这篇关于PHPUnit-使用配置文件时“未执行测试"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!