Laravel框架测试方法:搭建测试环境、单元和功能测试

2022-02-1420:52:47后端程序开发Comments1,391 views字数 5620阅读模式

Laravel 内核早已继承了 PHPUnit 单元测试组件。PHPUnit 是 PHP 社区里使用最广泛、最受欢迎的测试框架之一。PHPUnit 同时支持「单元测试」和「功能测试」两种特性。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/23158.html

我们会简单介绍 PHPUnit 「单元测试」和「功能测试」的基本使用方法。继而,讲解如何在 Laravel 项目中创建「单元测试」和「功能测试」用例。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/23158.html

不过本篇我们假定你已经对 PHPUnit 测试框架有了基本的了解,所以让我们把焦点放到 Laravel 中使用 PHPUnit 进行测试这个主题中。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/23158.html

单元测试和功能测试文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/23158.html

如果您已经接触过 PHPUnit 框架,那么您应该知道,它支持两种类型特性 -- 「单元测试」和「功能测试」。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/23158.html

「单元测试」的目的是用于测试函数或方法的正确性。更重要的是,我们可以轻松实现代码逻辑的正确性。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/23158.html

如果您在开发过程中发现某个功能包含多个逻辑处理,那么最好将每个处理逻辑拆分到不同的方法里,这样以确保单个方法和代码块可测试。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/23158.html

我们以一个理想的方法来窥探单元测试的奥秘。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/23158.html

public function getNameAttribute($value){    return ucfirst($value);}

如你所见,这个方法仅处理一个业务逻辑,方法内部通过 ucfirst 函数将字符转换成首字母大写格式。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/23158.html

单元测试是为了保证每个独立单元的代码正确性;功能测试则是为了保证一个功能的正确性。一言以蔽之,就是通过特定的测试用例模拟用户访问应用的行为验证系统的正确性。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/23158.html

例如,我们可以为包含如下步骤的登录功能实现一个功能测试用例:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/23158.html

  • 发起一个访问登录页面的 GET 请求;
  • 判断我们是否处在登录页面;
  • 生成用于采用 POST 请求方式登录的登录数据;
  • 判断是否创建登录会话数据成功。

这就是应该如何创建「功能测试」用例的秘密。接下来我们将创建具体的测试用例,来讲解如何在 Laravel 中使用「单元测试」和「功能测试」。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/23158.html

搭建测试环境文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/23158.html

创建测试模型文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/23158.html

在开始创建测试用例前,我们需要先构建起用于测试的项目依赖。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/23158.html

先执行下面的 artisan 命令创建一个 Post 模型及其对应的迁移文件。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/23158.html

$ php artisan make:model Post --migration

上面的命令将为我们创建一个 Post 模型类和数据库迁移文件。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/23158.html

Post 模型代码如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/23158.html

// app/Post.phpnamespace App;use Illuminate\Database\Eloquent\Model;class Post extends Model{    //}

数据库迁移文件 YYYY_MM_DD_HHMMSS_create_posts_table.php 将创建在 database/migrations 目录中。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/23158.html

Post 数据表会存储一篇文章的标题。修改后 Post 数据库迁移文件代码如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/23158.html

use Illuminate\Support\Facades\Schema;use Illuminate\Database\Schema\Blueprint;use Illuminate\Database\Migrations\Migration;class CreatePostsTable extends Migration{    /**     * Run the migrations.     *     * @return void     */    public function up(){        Schema::create('posts', function (Blueprint $table) {            $table->increments('id');            $table->string('name');            $table->timestamps();        });    }    /**     * Reverse the migrations.     *     * @return void     */    public function down(){        Schema::dropIfExists('posts');    }}

如你所见,我们通过新增的 $table->string('name') 来存储文章的标题。接下来,执行数据库迁移命令就回在数据库中创建对应的数据表了。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/23158.html

$ php artisan migrate

在创建完数据表之后,我们需要向 Post 模型类中加入如下代码文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/23158.html

namespace App;use Illuminate\Database\Eloquent\Model;class Post extends Model{    /**     * Get the post title.     *     * @param  string  $value     * @return string     */    public function getNameAttribute($value){        return ucfirst($value);    }}

我们刚刚添加了 accessor 方法,它的功能是修改文章的标题,这正是我们在单元测试用例中要测试的。以上就是 Post 模型所需要修改的内容。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/23158.html

创建测试控制器文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/23158.html

接下来,我们需要创建一个文件名为 app/Http/Controllers/AccessorController.php 的控制器,它将被用于后续功能测试。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/23158.html

// app/Http/Controllers/AccessorController.phpnamespace App\Http\Controllers;use App\Post;use Illuminate\Http\Request;use App\Http\Controllers\Controller;class AccessorController extends Controller{    public function index(Request $request){        // get the post-id from request params        $post_id = $request->get("id", 0);        // load the requested post        $post = Post::find($post_id);        // check the name property        return $post->name;    }}

在 index 方法中,我们通过请求中的 id 参数,从 Post 模型中查询一篇文章。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/23158.html

最后,在 routes/web.php 路由配置文件里定义相关路由。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/23158.html

Route::get('accessor/index', 'AccessorController@index');

设置完路由后就可以通过 http://your-laravel-site.com/accessor/index 访问该路由是否能够正常访问了。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/23158.html

单元测试文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/23158.html

上一节我们搭建了用于测试的环境。本节我们会在 Laravel 中编写单元测试用例对 Post 模型进行测试。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/23158.html

幸运的是,Laravel 同样为我们提供了创建测试用例模版文件的命令工具。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/23158.html

通过在命令行里执行下面的命令来创建 AccessorTest 单元测试用例类。注意我们需要通过 --unit 参数选项来表明这个命令创建一个单元测试用例。单元测试用例文件被创建在 tests/Unit 目录内。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/23158.html

$ php artisan make:test --unit

执行上面创建测试用例命令会创建文件名为 tests/Unit/AccessorTest.php 文件。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/23158.html

// tests/Unit/AccessorTest.phpnamespace Tests\Unit;use Tests\TestCase;use Illuminate\Foundation\Testing\DatabaseMigrations;use Illuminate\Foundation\Testing\DatabaseTransactions;class AccessorTest extends TestCase{    /**     * A basic test example.     *     * @return void     */    public function testExample(){        $this->assertTrue(true);    }}

将 testExample 方法替换成更有实际意义的方法:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/23158.html

// tests/Unit/AccessorTest.phpnamespace Tests\Unit;use Tests\TestCase;use Illuminate\Foundation\Testing\DatabaseMigrations;use Illuminate\Foundation\Testing\DatabaseTransactions;class AccessorTest extends TestCase{    /**     * Test accessor method     *     * @return void     */      public function testAccessorTest(){        $db_post = DB::select('select * from posts where id = 1');        $db_post_title = ucfirst($db_post[0]->title);        $model_post = Post::find(1);        $model_post_title = $model_post->title;        $this->assertEquals($db_post_title, $model_post_title);    }}

我们可以看到更新后的代码和 Laravel 代码编码风格完全一致。在类的开始我们引入了相关依赖类文件。在 testAccessorTest 方法里,我们希望验证定义在 Post 模型里面的 getNameAttribute 方法的正确性。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/23158.html

为了实现这样的测试功能,我们通过 DB 类使用原生 SQL 查询到一篇文章,并将文章的标题赋值给 $db_post_title 变量。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/23158.html

之后,我们通过 Post 模型获取经过 getNameAttribute 方法处理过后的同一篇文章的标题赋值给 $model_post_title。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/23158.html

最后,通过 assertEquals 方法比较两个变量是否相等。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/23158.html

以上就是如何在 Laravel 中使用单元测试的使用方法。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/23158.html

功能测试文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/23158.html

这一节我们将学习如何创建功能测试用例来对先前创建的控制器进行「功能测试」。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/23158.html

通过下面给出的命令,我们将创建一个名为 AccessorTest 的功能测试用例。注意这边我们没有使用 --unit 命令行选项,所以命令会在 tests/Feature 目录下创建一个「功能测试」用例。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/23158.html

$ php artisan make:test AccessorTest

命令会创建文件名为 tests/Feature/AccessorTest.php 的测试类。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/23158.html

// tests/Feature/AccessorTest.phpnamespace Tests\Feature;use Tests\TestCase;use Illuminate\Foundation\Testing\WithoutMiddleware;use Illuminate\Foundation\Testing\DatabaseMigrations;use Illuminate\Foundation\Testing\DatabaseTransactions;class AccessorTest extends TestCase{    /**     * A basic test example.     *     * @return void     */    public function testExample(){        $this->assertTrue(true);    }}

同样我们替换掉 testExample 方法。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/23158.html

// tests/Feature/AccessorTest.phpnamespace Tests\Feature;use Tests\TestCase;use Illuminate\Foundation\Testing\WithoutMiddleware;use Illuminate\Foundation\Testing\DatabaseMigrations;use Illuminate\Foundation\Testing\DatabaseTransactions;class AccessorTest extends TestCase{    /**     * A basic test example.     *     * @return void     */    public function testBasicTest(){        $db_post = DB::select('select * from posts where id = 1');        $db_post_title = ucfirst($db_post[0]->name);        $response = $this->get('/accessor/index?id=1');        $response->assertStatus(200);        $response->assertSeeText($db_post_title);    }}

熟悉功能测试的朋友应该对上面的代码不会太陌生。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/23158.html

首先,我们还是通过 DB 类使用原生 SQL 查询到一篇文章,并将文章的标题赋值给 $db_post_title 变量。接着我们模拟一个访问 /accessor/index?id=1 URI 的 GET 请求,并通过 $response 变量接收响应。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/23158.html

然后,我们去匹配请求响应的状态码是否为 200。在我们的测试用例中的这个 GET 请求响应状态码应该是 200。此外,测试后还将获取到一个首字母大写的标题,判断标题是否与 $db_post_title 相对的方法是 assertSeeText。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/23158.html

编写完成所有的测试用例后。接下来需要去执行这些测试用例。在 Laravel 项目中运行 PHPUnit 测试用例,仅需在项目更目录执行下面的命令。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/23158.html

$ phpunit

这个命令会运行项目中的所有测试用例。测试中的断言会以标准的 PHPUnit 输出显示在控制台。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/23158.html

总结文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/23158.html

今天,我们探讨了 Laravel 内置测试组件 PHPUnit 的测试用例实现方法。本文仅涉及 PHPUnit 「单元测试」和「功能测试」的基础知识,工作中我们还需要结合实际出发,对 PHPUnit 测试进行深入研究才行。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/23158.html

此外,我们还学习了通过使用 artisan 命令行工具创建分别创建了用于单元测试和功能测试的测试用例的方法。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/23158.html

  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/bc/23158.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定