TDD 笔记
环境
Laravel8
步骤
- 配置
phpunit.xml
- 建立模型工厂
- 建立辅助函数
- 建立单元测试
- 建立功能测试
配置phpunit.xml
phpunit.xml
将注释的DB_CONNECTION
和DB_DATABASE
选项打开
建立模型工厂
// 方式一 单独建立模型工厂 工厂名称 ThreadFactory 对应的模型是 Thread
php artisan make:factory ThreadFactory --model=Thread
// 方式二 建立 Thread 模型时同时生成模型工厂和迁移文件
php artisan make:model Thread -mf
定义ThreadFactory
生成的数据
namespace Database\Factories;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
class ThreadFactory extends Factory
{
public function definition()
{
// 对应数据库表里的数据
return [
'user_id' => function () {
return User::factory()->create()->id;
}, // 所属用户
'title' => $this->faker->sentence, // 标题
'body' => $this->faker->paragraph, // 内容
];
}
}
建立辅助函数
TestCase.php
namespace Tests;
use App\Models\User;
+ use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
abstract class TestCase extends BaseTestCase
{
use CreatesApplication;
+ use DatabaseMigrations;
+ // 登录用户
+ protected function signIn($user = null)
+ {
+ $user = $user ?: User::factory()->create();
+ $this->actingAs($user);
+ return $this;
+ }
}
建立单元测试
php artisan make:test ThreadTest --unit
ThreadTest.php
替换继承的类TestCase
删除测试例子
<?php
namespace Tests\Unit;
- use PHPUnit\Framework\TestCase;
+ use Tests\TestCase;
class ThreadTest extends TestCase
{
- /**
- * A basic unit test example.
- *
- * @return void
- */
- public function test_example()
- {
- $this->assertTrue(true);
- }
}
测试用例写法有两种:
- 注释里加
@test
- 方法名
test
开头
TheadTest.php
<?php
namespace Tests\Unit;
use App\Models\Thread;
use Tests\TestCase;
class ThreadTest extends TestCase
{
protected $thread;
protected function setUp(): void
{
parent::setUp();
$this->thread = Thread::factory()->create();
}
/**
* @test
* 注释里加 @test 测试用例一
*/
public function thread_has_a_creator()
{
// 检测是不是返回的是User模型
$this->assertInstanceOf('App\Models\User', $this->thread->creator);
}
// 方法名 test 开头 测试用例二
public function test_a_thread_has_a_creator()
{
// 检测是不是返回的是User模型
$this->assertInstanceOf('App\Models\User', $this->thread->creator);
}
}
Thread.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Thread extends Model
{
use HasFactory; // 这里关联工厂类
// 帖子创建者
public function creator()
{
return $this->belongsTo(User::class, 'user_id'); // 使用 user_id 字段进行模型关联
}
}
测试
php artisan test // 运行全部测试
php artisan test --filter ThreadTest // 按文件名测试
php artisan test --filter thread_has_a_creator // 按方法名测试
php artisan test --filter test_a_thread_has_a_creator // 按方法名测试
建立功能测试
php artisan make:test CreateThreadTest
CreateThreadTest.php
测试用例例子
namespace Tests\Feature;
use App\Models\Thread;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
class CreateThreadTest extends TestCase
{
/**
* @test
* 登录用户可以发布帖子
*/
public function an_authenticated_user_can_create_new_forum_threads()
{
$this->signIn(); // 登录用户
$thread = Thread::factory()->make();
// 创建帖子检测状态码是不是201
$this->post('/threads', $thread->toArray())->assertStatus(201);
}
}
参考
本作品采用《CC 协议》,转载必须注明作者和本文链接