phpunit运行类名称中没有“ test”

时间:2019-09-23 18:54:10

标签: php laravel annotations phpunit

如何向phpunit表示该课程是测试课程?

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

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

这是课程

<?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

输出

  

未执行测试!

更新

这是我的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>

虽然可以像@Alister这样通过完整路径运行类

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

对于每个类一遍又一遍地这样做是不方便的,特别是作为CI流程的一部分
理想情况下,该类将在

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

3 个答案:

答案 0 :(得分:4)

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

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

希望这会有所帮助!

答案 1 :(得分:3)

它不会读取类级别的/** @test */批注,但是默认的phpunit.xml文件也确实定义了带有文件名后缀的测试包:

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

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

答案 2 :(得分:0)

您可以使用后缀:

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

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