TDD 笔记

环境

Laravel8

步骤

  • 配置phpunit.xml
  • 建立模型工厂
  • 建立辅助函数
  • 建立单元测试
  • 建立功能测试

配置phpunit.xml

phpunit.xml 将注释的DB_CONNECTIONDB_DATABASE选项打开

TDD 笔记

建立模型工厂

// 方式一 单独建立模型工厂 工厂名称 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);
    }
}

参考

TDD 构建 Laravel 论坛笔记

本作品采用《CC 协议》,转载必须注明作者和本文链接
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!