【问题标题】:Symfony 4 php unit test - no tests executedSymfony 4 php 单元测试 - 没有执行测试
【发布时间】:2019-01-04 09:00:14
【问题描述】:

在我的 symfony 4 应用程序中,我尝试从 symfony doc 运行第一个单元测试。但它会返回以下消息。

    // 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);
        }
    }

Sebastian Bergmann 及其贡献者的 PHPUnit 5.7.27。

时间:17 毫秒,内存:2.00MB

没有执行任何测试!

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

$ ./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 致命错误:未捕获的 PHPUnit\Runner\Exception:在“/home/username/server/tests/Util/CalculatorTest.php”中找不到类“tests/Util/CalculatorTest”。在/home/username/server/bin/.phpunit/phpunit-6.5/src/Runner/StandardTestSuiteLoader.php:102

我们将不胜感激。

【问题讨论】:

  • 检查您是否正在运行正确的 php 单元。不确定 symfony 4,但在 symfony 3 中,我不得不使用供应商目录中的 phpunit。它与无法检测到 symfony 的单元测试配置的全局安装的 phpunit 混淆了。
  • @SimasJoneliunas 我也试过了。但还是一样的结果。当我尝试直接运行该测试时,我还会编辑问题以显示错误。
  • 如果命名空间是App\Tests\Util,类名是CalculatorTest,为什么要找类:tests/Util/CalculatorTest?您遇到了自动加载问题。
  • 这是一个愚蠢的错误。我忘了添加

标签: php symfony phpunit


【解决方案1】:

我遇到了同样的问题,我的解决方案是在我的项目的根目录中添加一个 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>

问候

【讨论】:

    猜你喜欢
    • 2020-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-08
    • 1970-01-01
    • 2017-03-22
    • 2013-05-22
    • 2019-06-04
    相关资源
    最近更新 更多