php artisan make:test -- unit UnitTestClassName
使用扩展创建的单元测试\Tests\TestCase
功能测试创建php artisan make:test FunctionalTestClassName
也扩展\Tests\TestCase
\Tests\TestCase
延伸Illuminate\Foundation\Testing\TestCase
但是,Laravel Dusk 浏览器测试用例使用php artisan dusk:make DuskTestClassName
extend创建\Tests\DuskTestCase
,而后者又扩展Laravel\Dusk\TestCase
似乎没有通用类,我可以向其中添加要测试的用户之类的内容(至少在供应商文件夹之外)。
为了允许共享代码,我在 app\Traits\TestData.php 中创建了一个特征:
<?php
namespace App\Traits;
trait TestData {
static $testUserId = 40;
public static function checkTestData() {
// returns an array of strings describing any problems with the test data setup.
...
}
...
}
我use
在我的测试中具有这种特征。有没有更好的办法?具体来说,我缺少一个常见的测试类吗?
我遇到的具体问题是我的特征没有扩展任何TestCase,所以我不能调用像
$this->assertNotNull($testUser,"Test user not found");
在我的特质中,所以我需要在 TestCase 和 DuskTestCase 中复制该代码。