PHPUnit | Laravel PHPUnit仅运行测试类中的第一个测试

3

目前,我正在尝试在 Laravel 项目上使用 PHPUnit 运行一些简单的单元测试,命令行为 phpunit tests/Unit/ExampleTest。但它只运行第一个方法。有没有解决方案?谢谢!

namespace Tests\Unit;

use PHPUnit\Framework\TestCase;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;

class ExampleTest extends TestCase
{
    /**
     * A basic test example.
     *
     * @return void
     */
    public function testBasicTest()
    {
        $this->assertTrue(true);
    }

    /**
     * An other basic test
     *
     * @return void
     */
    public function secondTestBasicTest()
    {
        $this->assertTrue(true);
    }

    public function basicTest(){
        $response = $this->action('GET', 'DepartementsController@display', ['id' => 1]);
        $view = $response->original;
        $this->assertEquals(1, $view['$dpt->iddepartement']);
    }
}
1个回答

9

PHPUnit默认只运行以test前缀开头的方法,所以secondTestBasicTestbasicTest不符合此条件。您可以在PHPDoc中显式注释测试用例为@test,以覆盖此行为:

/**
 * @test
 */
public function basicTest() {
    ...
}

1
谢谢,你是救命恩人! - Max Cabourg
1
另外,请记住你的类名应该以“Test”单词结尾。 - aPa

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接