php PHPUnit - 使用配置文件时“未执行测试”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29299206/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHPUnit - 'No tests executed' when using configuration file
提问by Muyiwa Olu
The Problem
问题
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.
为了提高我的代码质量,我决定尝试学习如何使用单元测试来测试我的代码,而不是我的平庸的最佳测试解决方案。
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:
我决定使用composer为个人库安装PHPUnit,该库允许我实现常见的数据库功能。起初我没有 PHPUnit 的配置文件,当我运行如下命令时:
$ phpunit tests/GeneralStringFunctions/GeneralStringFunctionsTest
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.
请注意,这是一个终端命令,所以我没有包含.php
扩展名。上面提到的 GeneralStringFunctionsTest 实际上是一个GeneralStringFunctionsTest.php
文件。
The output is what I expected:
输出是我所期望的:
Time: 31 ms, Memory: 2.75Mb
OK (1 test, 1 assertion)
时间:31 毫秒,内存:2.75Mb
OK(1 个测试,1 个断言)
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:
然后我尝试使用配置文件来自动加载测试套件,而不必每次都手动输入文件。我phpunit.xml
在我的根目录中创建了一个文件,并在文件中输入以下内容: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
I get the following output:
我得到以下输出:
PHPUnit 4.5.0 by Sebastian Bergmann and contributors.
Configuration read from /Users/muyiwa/Projects/DatabaseHelper/phpunit.xml
Time: 16 ms, Memory: 1.50Mb
No tests executed!
PHPUnit 4.5.0 由 Sebastian Bergmann 和贡献者编写。
从 /Users/muyiwa/Projects/DatabaseHelper/phpunit.xml 读取的配置
时间:16 毫秒,内存:1.50Mb
未执行任何测试!
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 srcfolder)
vendor - Composer third party files
如果有帮助,我的目录结构如下:
src - 顶级目录(包含我所有的源代码)
tests - 顶级目录(包含我的所有测试,结构与我的src文件夹相同)
vendor - Composer 第三方文件
I also have the composer json and lock file, as well as the phpunit xml file in the top level as files.
我还有 composer json 和 lock 文件,以及顶层的 phpunit xml 文件作为文件。
Things I've Tried
我尝试过的事情
- 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.
- 将目录更改
phpunit.xml
为tests/GeneralStringFunctions
- 将目录更改
phpunit.xml
为./tests
- 将
phpunit.xml
文件移动到tests
目录,然后将目录更改./
为tests
. - 向目录标记中添加后缀属性以
phpunit.xml
指定“Tests”作为显式后缀。
回答by RoboBear
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:
对于它的价值(迟到),我最近在为一个简单的网站制作一个新的 Laravel 5.1 项目时遇到了这个问题。我尝试调试它,但在尝试时感到困惑:
php artisan make:test homeTest
(which has a default test that just asserts true is true)
(它有一个默认测试,只是断言 true 是真的)
and saw the output
并看到输出
No tests executed!
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.
问题最终对我来说与我的 PHP 安装有关——“phpunit”是全局注册和不同配置的,而 Laravel 安装附带的 phpunit 配置得恰到好处并且运行完美。
So the fix is running the vendor's configured phpunit (from the same root directory as app/ and tests/):
所以修复是运行供应商配置的 phpunit(来自与 app/ 和 tests/ 相同的根目录):
./vendor/bin/phpunit
Hope that helps someone else!
希望能帮助别人!
回答by Lionel
Your XML file is fine as it is. However, you have to make sure that the PHP files in your tests/
folder are named as follows:
您的 XML 文件没有问题。但是,您必须确保文件tests/
夹中的 PHP 文件命名如下:
tests/Test.php<--- Note the uppercase "T"
tests/userTest.php
tests/fooBarTest.php
etc.
测试/ Test.php<--- 注意大写的“T”
测试/用户Test.php
测试/fooBar Test.php
等。
The filenames must end with "Test.php". This is what PHPUnit is looking for within directories.
文件名必须以 "Test.php" 结尾。这就是 PHPUnit 在目录中寻找的内容。
Furthermore, every test method must have a name that starts with "test":
此外,每个测试方法都必须有一个以“test”开头的名称:
public function testFooBar()
{
// Your test code
}
Hope that helps!
希望有帮助!
回答by Ndong Akwo
On windows use the following command on terminal
在 Windows 上,在终端上使用以下命令
.\vendor\bin\phpunit
that's if the command
那是如果命令
phpunit
returns "No tests executed!"
返回“没有执行测试!”
while on Mac
在 Mac 上时
./vendor/bin/phpunit
Hope it helps.
希望能帮助到你。
回答by igors
I had the same problem after PHPUnit on our virtual machines updated to version 6. Even --debug and --verbose said nothing useful, just "No tests executed". In the end it turned out that classes and namespaces were changed in the new version and it just didn't want to execute the files that contained references to old classes. The fix for me was just to replace in every test case this:
在我们的虚拟机上的 PHPUnit 更新到版本 6 后,我遇到了同样的问题。即使 --debug 和 --verbose 也没有说任何有用的,只是“没有执行测试”。最后发现新版本中的类和命名空间发生了变化,它只是不想执行包含对旧类的引用的文件。对我来说,修复只是在每个测试用例中替换这个:
class MyTestCase extends \PHPUnit_Framework_TestCase {...}
with:
和:
use PHPUnit\Framework\TestCase;
class MyTestCase extends TestCase {...}
回答by AlmostPitt
I realize this is super old, but it just happened to me too. Hopefully this will help someone.
我意识到这太老了,但它也发生在我身上。希望这会帮助某人。
My problem was that I forgot the '@' symbol in /** @test */
我的问题是我忘记了 /** @test */ 中的“@”符号
WRONG:
错误的:
/** test */
function a_thread_can_be_deleted()
{
...
}
RIGHT:
对:
/** @test */
function a_thread_can_be_deleted()
{
...
}
回答by chebaby
I pulled my hair for 10 minutes before i decided to use --debug(good way to go by the way) to discover the simple fact that file name didn't respect the naming convention, i had an extra "s" at the end.
在我决定使用--debug(顺便说一句好方法)之前,我拉了我的头发10分钟,以发现文件名不遵守命名约定的简单事实,最后我有一个额外的“s” .
wrong
错误的
CreateAdminTests
right
对
CreateAdminTest
hope this note could help for someone
希望这篇笔记可以对某人有所帮助
回答by Hamza Zymawy
You Need Just To Call It From Vendor File
你只需要从供应商文件中调用它
vendor\bin\phpunit
Notice \ Not /
vendor\bin\phpunit
注意\不/
回答by Arun Ganesan
Instead of run phpunit
而不是运行 phpunit
use
用
vendor\bin\phpunit
供应商\bin\phpunit
回答by Jan Polzer
Check phpunit.xmlfile, look inside testsuites.
检查phpunit.xml文件,查看内部测试套件。
My version of phpunit (2019) is looking for files ending (suffix) *Test.php . So, make sure all the test files are named properly (ex.: BookTest.php is correct, BookTests.php is not, BookTestCase.php is not).
我的 phpunit (2019) 版本正在寻找以(后缀) *Test.php 结尾的文件。因此,请确保所有测试文件都正确命名(例如:BookTest.php 正确,BookTests.php 不正确,BookTestCase.php 不正确)。
回答by Tahseen Alaa
if you are using PHPSTORMgo to Settings then goto
如果您使用的是PHPSTORM,请转到“设置”,然后转到
Test Frameworks
and click + and choose
PHPUnit Local
thenUse Composer Auto Loader
then paste this like in path to script fieldC:\{YOUR PROJECT NAME}\vendor\autoload.php
- click OK
HAPPY TESTING
Test Frameworks
然后单击 + 并选择
PHPUnit Local
然后Use Composer Auto Loader
然后将其粘贴到脚本字段的路径中C:\{YOUR PROJECT NAME}\vendor\autoload.php
- 单击确定
HAPPY TESTING