laravel 单元测试

建模型 含数据迁移文件

php artisan make:model Models/TopicKind --migration

创建修改模型迁移文件

php artisan make:migration alter_tablename_table --table=tablename

执行最新数据迁移

php artisan migrate

回滚一次(如果之前漏了写错

php artisan migrate:rollback

重新构建数据库表(别在线上按这个

php artisan migrate:refresh

数据类型相关

https://laravel.com/docs/7.x/migrations

创建单元测试

php artisan make:test Admin/TopicKindTest --unit

测试单个用例(单个文件执行

 vendor/phpunit/phpunit/phpunit --filter testTopicKindSave.php

创建工厂,虚拟数据用

php artisan make:factory PostFactory

修改字段的时候需要有还原 ,就是up和down都要写,rollback的时候就是执行down的东东,等于将up的倒过来做一次

/**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        //创表
        Schema::create('topic_kinds', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('content',128);
            $table->integer('type');
            $table->integer('rank')->default(1);
            $table->integer('status')->default(1);
            $table->timestamps();
        });

        //加字段
        Schema::table('topics', function (Blueprint $table) {
            //
            $table->integer('kind')->nullable();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        //删表
        Schema::dropIfExists('topic_kinds');

        //删字段
        Schema::table('topics', function (Blueprint $table) {
            //
            $table->dropColumn('kind');
        });
    }

初始化每个测试的数据(统一

public function setUp(): void
    {
        parent::setUp(); // TODO: Change the autogenerated stub
        DB::beginTransaction();
        factory(TopicKind::class)
            ->create(
                [
                    'content'=>'test3',
                    'type'=>1,
                    'rank'=>1,
                    'status'=>1
                ]
            );
        factory(TopicKind::class)
            ->create(
                [
                    'content'=>'test1',
                    'type'=>1,
                    'rank'=>3,
                    'status'=>1
                ]
            );
        factory(TopicKind::class)
            ->create(
                [
                    'content'=>'test1',
                    'type'=>1,
                    'rank'=>3,
                    'status'=>0
                ]
            );
        factory(TopicKind::class)
            ->create(
                [
                    'content'=>'test2',
                    'type'=>1,
                    'rank'=>2,
                    'status'=>1
                ]
            );
    }

//testcase 基类,不知道为毛他基类是错的

use Tests\TestCase;

如果执行说route class什么的找不到

boost/app.php
require_once __DIR__ . /helpers.php;

现有表生成

 composer require --dev "kitloong/laravel-migrations-generator"
php artisan migrate:generate

单个表

php artisan migrate:generate topics

参考文档
https://blog.csdn.net/aa19891204/article/details/100556225?depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2&utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2

干净数据

use DatabaseTransactions;

https://learnku.com/laravel/t/2588/unit-testing-to-ensure-the-database-clean

factory,虚拟数据的时候用

$factory->define(Record::class, function (Faker $faker) {
    return [
        'live_id' => factory(\App\Models\Live::class),
        'channel_id' => random_int(1, 100),
        'title' => function(array $post) {
            return \App\Models\Live::find($post['live_id'])->title;
        },
        'thumb' => random_int(1, 10000),
        'url' => $faker->url,
        'writer' => 'test',
        'source_id' => 0,
    ];
});

里面的post就是创建的record这个对象,所以title是搜索了一下这个id的live对应的title;另外这里希望是live_id是一个Live的id,factory(\App\Models\Live::class) 这样写live_id就已经是id了(不要问我为什么,我也奇怪,但是实际就是这样

如果需要创建多个对象,且有关联创建

   protected function setUp(): void
    {
        parent::setUp(); // TODO: Change the autogenerated stub

        //创建测试赛事
        $match = factory(Match::class)->create(['name'=>self::testLeague.'赛事']);

        //创建测试live和record数据
        factory(Live::class, 10)
            ->create(['match_id' => $match->id, 'title'=>self::testTeam."啦啦啦啦VS神马球队啦啦啦"])
            ->each(function ($live) {
                factory(Record::class)->create(['live_id'=>$live->id]);
            });
        factory(Live::class, 10)
            ->create(['match_id' => $match->id, 'title'=>"啦啦啦啦啦VS哈哈哈哈哈哈"])
            ->each(function ($live) {
                factory(Record::class)->create(['live_id'=>$live->id]);
            });
    }

也可以写到factory

$factory->define(Match::class, function (Faker $faker) {
    return [
        'name'=>$faker->unique()->name,
        'alias'=>$faker->unique()->word,//自动生成随机数据
        'intro'=>$faker->text,
        'display'=>$faker->randomElement([0,1]),
        'type_id'=>$faker->randomElement([0,1]),
        'writer'=>'test',
    ];
});

//match创建后,同时创建20条对应的,lives、records数据
//$factory->afterCreating(Match::class, function ($match, Faker $faker) {
//
//});

测试用例单独执行 group关键字

/**
     *@group AdminHouse
     * */
public function xxxxxx
 vendor/phpunit/phpunit/phpunit --group AdminHouse
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容