我的 PHPUnit 是通过 Composer 安装的 (PHPUnit 3.7.21)。
我有以下目录结构:
.
├── Code
├── Test
│ ├── Php
│ │ └── PlanningModuleTest.php
│ └── bootstrap.php
└── phpunit.xml
当我执行时
$ phpunit
从项目根目录,我得到以下输出:
PHPUnit 3.7.21 by Sebastian Bergmann.
Configuration read from D:\Development\...\phpunit.xml
Time: 40 ms, Memory: 4.00MB
No tests executed!
我的 phpunit.xml
看起来像这样:
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
colors="true"
bootstrap="Test/bootstrap.php"
>
<testsuites>
<testsuite name="pms">
<directory suffix="Test.php">./Test/Php/</directory>
</testsuite>
</testsuites>
<php>
<ini name="display_errors" value="true"/>
</php>
</phpunit>
我有一个文件PlanningModuleTest.php
,其中包含以下内容:
记录一下我的 PHP 文件的第一部分:
<?php
use jamesiarmes\PhpEws\Enumeration\UnindexedFieldURIType;
use PHPUnit\Framework\TestCase;
class PlanningModuleTest extends TestCase
{
public function setUp()
{
$_SESSION = array();
require_once('Code/Config.php');
parent::setUp();
}
public function testExchangeCalendarItemCreation()
{
$this->assertInstanceOf(ExchangeCalendarItem::class, new ExchangeCalendarItem());
}
public function testExchangeCalendarItem()
{
// ...
}
}
所以这应该是正确的,因为 PHPUnit 检查文件和类名是否以 Test.php 结尾。
为什么 phpunit
不执行我的测试?
编辑
我尝试直接执行测试
$ phpunit --verbose --debug Test\Php\PlanningModuleTest.php
它返回:
Class 'Test\Php\PlanningModuleTest' could not be found in 'D:\Development\Git\projectmanagement\Test\Php\PlanningModuleTest.php'.`
最佳答案
正如评论中已经建议的,在我们开始调试与使用全局安装的 phpunit
版本相关的问题之前,该版本与您随项目安装的版本明显不同,请尝试运行
$ ./vendor/bin/phpunit
相反。
首先引起注意的是测试类缺少命名空间,而它似乎应该有一个命名空间:
<?php
namespace Test\Php;
use jamesiarmes\PhpEws\Enumeration\UnindexedFieldURIType;
use PHPUnit\Framework\TestCase;
class PlanningModuleTest extends TestCase
{
// ...
}
然后还要确保在 composer.json
中配置并记录测试代码的命名空间要求:
例如,您的自动加载已在 composer.json
中正确设置
{
"autoload-dev": {
"psr-4": {
"Test\\": "Test/"
}
}
}
由于您尚未与我们共享,请确保 Test/bootstrap.php
包含
<?php
require_once __DIR__ . '/../vendor/autoload.php';
正确设置自动加载。
关于PHPUnit 找不到我的测试文件或直接执行它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45547842/