大家的测试用例都是怎么写的?

之前在测试用例方面实践的太少,一开始是觉得会迟滞开发进度,但当项目逐渐复杂之后,测试的紧迫性就提上来了,但这个时候团队往往会路径依赖,很难再去花成本去补之前落下的功课。

现在来看,之后有可能会重新组队了,加之 AI 助手的协助降低了一些编码的成本。我还是想把测试抓起来,但又缺乏相关的经验,想征询一下大家的实践经验。

我用需求例子讲一下自己的问题:

分类管理功能简述:向前端提供一组接口实现对分类数据的增删改查。

那么如果要写一个用例是对接口测试还是对服务测试?还是说两者都需要?

我一般会将业务处理抽象出来封装成服务类,比如:

接口 POST /api/goods/categories
经由控制器方法 CategoryController::store()
调用服务方法 CategoryStoreService::handle()

其中 CategoryStoreService::handle() 方法是之后会被通过导入、ERP数据同步等功能复用的,那么对这个方法的测试就显得很重要。可能有以下用例:

test('分类存储 冒烟测试', function ($name) {
    /** @var CategoryStoreService $service */
    $service    = app(CategoryStoreService::class);
    $category   = $service->handle($name);
    expect($category->name)->toBe($name);
})->with([
    '服装',
    '鞋帽',
    '家电',
]);
test('分类存储 恢复同名已删除', function () {
    /** @var CategoryStoreService $service */
  $service = app(CategoryStoreService::class);
    $category = $service->handle('服装');
    $id = $category->id;
    $category->delete();
    $category = $service->handle('服装');
    expect($category->name)->toBe('服装');
    expect($category->id)->toBe($id);
    expect($category->trashed())->toBeFalse();
});

test('分类存储 验证测试 空名称', function ($name) {
    /** @var CategoryStoreService $service */
  $service = app(CategoryStoreService::class);
    $service->handle($name);
})->with([
    '',
])->throws(InvalidArgumentException::class, '分类名称不能为空');

test('分类存储 验证测试 名称过长', function () {
    /** @var CategoryStoreService $service */
  $service = app(CategoryStoreService::class);
    $service->handle(str_repeat('a', 256));
})->throws(InvalidArgumentException::class, '分类名称不能超过 255 个字符');

test('分类存储 验证测试 重复名称', function () {
    /** @var CategoryStoreService $service */
  $service = app(CategoryStoreService::class);
    $service->handle('服装');
    $service->handle('服装');
})->throws(InvalidArgumentException::class, '分类名称已存在');

但对于前端团队来讲他们更关注与接口的交互,每次服务端的迭代是否对接口服务造成影响,可能有以下用例:

test('/api/goods/categories 存储后关键词检索', function (User $user) {
    $responseStore  = $this
        ->actingAs($user)
        ->postJson('/api/goods/categories', ['name' => '服装']);
    $responseStore->assertStatus(201);
    $responseIndex  = $this->get('/api/goods/categories?keyword=服装');
    $responseIndex->assertStatus(200);
    $responseIndex->assertJsonCount(1, 'data');
})->with([
    fn() => User::factory()->create(),
]);

test('/api/goods/categories 存储 空名称', function (User $user) {
    $responseStore  = $this
        ->actingAs($user)
        ->postJson('/api/goods/categories', ['name' => '']);
    $responseStore->assertStatus(422);
    $responseStore->assertJsonValidationErrors('name');
})->with([
    fn() => User::factory()->create(),
]);

test('/api/goods/categories 存储 名称过长', function (User $user) {
    $responseStore  = $this
        ->actingAs($user)
        ->postJson('/api/goods/categories', ['name' => str_repeat('a', 256)]);
    $responseStore->assertStatus(422);
    $responseStore->assertJsonValidationErrors('name');
})->with([
    fn() => User::factory()->create(),
]);

test('/api/goods/categories 存储 重复名称', function (User $user) {
    $responseStore      = $this
        ->actingAs($user)
        ->postJson('/api/goods/categories', ['name' => '服装']);
    $responseStore->assertStatus(201);
    $responseDuplicate  = $this
        ->actingAs($user)
        ->postJson('/api/goods/categories', ['name' => '服装']);
    $responseDuplicate->assertStatus(422);
    $responseDuplicate->assertJson(['message' => '分类名称已存在']);
})->with([
    fn() => User::factory()->create(),
]);

至少从目前的用例上看,对服务的测试逻辑基本上等效于对接口的测试。那么我的问题是:如果让大家来写,两套用例是否都有必要?还是说只保留其中一套即可?

《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
最佳答案

工期允许的话,多写个 API 的测试用例没有坏处……而且这部分结合 CI/CD 能提高开发效率和软件质量!

2个月前 评论
讨论数量: 10

根据覆盖率来决定,前端api覆盖不到的地方,就需要单独写测试用例~

2个月前 评论

从来不写,开发周期不等人,交给测试去验。 :grimacing:

2个月前 评论
sanders (楼主) 2个月前
lovewei (作者) 2个月前
sanders (楼主) 2个月前
lovewei (作者) 2个月前

我们后端是针对服务类写单元测试就可以了,如果要测交互不如用Selenium,我们测试的同事就在用

2个月前 评论

工期允许的话,多写个 API 的测试用例没有坏处……而且这部分结合 CI/CD 能提高开发效率和软件质量!

2个月前 评论

服务类一般写单元测试,api 接口写功能测试

2个月前 评论

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