PHPUnit - 使用配置文件时“未执行测试"
为了提高我的代码质量,我决定尝试学习如何使用单元测试而不是我平庸的最佳测试解决方案来测试我的代码.
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 毫秒,内存:2.75Mb
Time: 31 ms, Memory: 2.75Mb
OK(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 - 顶级目录(包含我所有的源代码)
tests - 顶级目录(包含我的所有测试,结构与我的 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
中的目录标签中添加后缀属性,指定Tests"为显式后缀.
- 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
希望对其他人有所帮助!
Hope that helps someone else!
相关文章