phpunit 运行 class 名称中没有 "test"

phpunit run class without "test" in the name

如何向 phpunit 表明 class 是一个测试 class?

/** @test */ 注释似乎只适用于名称中没有附加 test 字符串的方法

那么如何在不将 test 字符串附加到其名称的情况下 运行 测试 class?

这是class

<?php

namespace Tests\Feature;

use Tests\TestCase;
/** @test */
class RepoPost extends TestCase
{
    /** @test */
    public function postSave()
    {
        $this->assertTrue(true);
    }

    /** @test */
    public function anotherOne()
    {
        $this->assertTrue(true);
    }
}

运行

vendor/bin/phpunit --filter RepoPost

产出

No tests executed!

更新

这是我的 phpunit.xml 配置

<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
         backupStaticAttributes="false"
         bootstrap="tests/bootstrap.php"
         colors="true"
         processIsolation="false"
         stopOnFailure="false">
    <testsuites>
        <testsuite name="Feature">
            <directory>./tests/Feature</directory>
        </testsuite>
    </testsuites>
    <php>
        <server name="APP_ENV" value="testing"/>
    </php>
</phpunit>

虽然可以通过完整路径 运行 class,正如 @Alister 指出的那样

vendor/bin/phpunit tests/Feature/RepoPost.php

对每个 class 重复执行此操作并不方便,尤其是作为 CI 过程的一部分
理想情况下,class 将在

的完整测试套件中 运行
vendor/bin/phpunit

它不读取 class 级别的 /** @test */ 注释,但默认的 phpunit.xml 文件确实也有用文件名后缀定义的测试套件:

 <testsuites>
     <testsuite name="default">
         <directory suffix="Test.php">tests</directory>
     </testsuite>
 </testsuites>

如果你 运行 phpunit 在特定文件上:vendor/bin/phpunit tests/RepoPost.php 它会 运行 测试,即使文件名与后缀不匹配。

您可以使用后缀:

 <testsuite name="Feature">
        <directory suffix="Test.php">./tests/Feature</directory>
    </testsuite>

然后您应该将文件重命名为 RepoPostTest.php

在您的 phpunit.xml 中,将后缀属性添加到标签中。该示例将 运行 所有 .php 文件,如果您在其中有任何 none TestCase classes/files,这可能会带来风险,这就是为什么它的约定要在 类 后缀Test.

<testsuite name="Feature">
    <directory suffix=".php">./tests/Feature</directory>
</testsuite>

希望对您有所帮助!