Laravel测试用例编写

测试全部
php artisan test –stop-on-failure

只测试Feature
php artisan test –group=feature –stop-on-failure

只测试某一个文件
php74 vendor/bin/phpunit tests/Feature/ServiceProductTest.php

<?php

namespace Tests\Feature;

use App\Common\ServiceRequestParam;
use App\Services\ProductService;
use App\Models\TagLangModel;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Tests\TestCase;

class SearchTest extends TestCase
{
    // 警告!!!!!!
    // 不要用这个啊,用了数据会被清空
    // use RefreshDatabase; 

    // 使用下面这个,完成后会回滚数据
    // use DatabaseTransactions;


    public function testSearch()
    {
        // TagLangModel::create(['tag_id'=>1, 'name'=>'sdfssfsdf', 'type'=>'NORMAL', 'lang'=>'en-US']);
        $tagService = resolve('App\Services\SearchService');
        $req = ServiceRequestParam::make([
            'word' => 'Farfetch',
            'limit' => 5
        ]);
        $ret = $tagService->searchProduct($req);
        $this->assertTrue($ret->isSuccess());
    }


}

 

注意事项

  • 不要用use RefreshDatabase,用了数据会被清空。
  • 可以用use DatabaseTransactions,用了数据会回滚。
  • Feature相当于集成测试,一些关联比较多的功能需要写在这里面,不然调用不了