Symfony 4 PHP单元测试-不执行测试

时间:2018-07-27 09:05:28

标签: php symfony phpunit

在我的symfony 4应用程序中,我尝试从symfony r docs(https://symfony.com/doc/current/testing.html)运行第一个单元测试。但是它返回以下消息。

// tests/Util/CalculatorTest.php
namespace App\Tests\Util;

use App\Util\Calculator;
use PHPUnit\Framework\TestCase;

public class CalculatorTest extends TestCase
{
    public function testAdd()
    {
        $calculator = new Calculator();
        $result = $calculator->add(30, 12);

        // assert that your calculator added the numbers correctly!
        $this->assertEquals(4, $result);
    }
}PHPUnit 5.7.27 by Sebastian Bergmann and contributors.



Time: 17 ms, Memory: 2.00MB

No tests executed!

当我尝试使用以下命令直接运行该测试时,也会出现以下错误。

`./bin/phpunit tests/Util/CalculatorTest.php`

#!/usr/bin/env php
// tests/Util/CalculatorTest.php
namespace App\Tests\Util;

use App\Util\Calculator;
use PHPUnit\Framework\TestCase;

public class CalculatorTest extends TestCase
{
    public function testAdd()
    {
        $calculator = new Calculator();
        $result = $calculator->add(30, 12);

        // assert that your calculator added the numbers correctly!
        $this->assertEquals(4, $result);
    }
}PHP Fatal error:  Uncaught PHPUnit\Runner\Exception: Class 'tests/Util/CalculatorTest' could not be found in '/home/username/server/tests/Util/CalculatorTest.php'. in /home/username/server/bin/.phpunit/phpunit-6.5/src/Runner/StandardTestSuiteLoader.php:102

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,对我来说,解决方案是在项目的根目录中添加一个phpunit.xml配置文件。

<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
         bootstrap="vendor/autoload.php"
         colors="true"
>
    <testsuites>
        <testsuite name="Unit">
            <directory suffix="Test.php">./tests</directory>
        </testsuite>
    </testsuites>
</phpunit>

致谢