单元测试:我把同一个接口但是不同的参数请求都写到一个方法里面合理吗?

class InvoiceTest extends TestCase
{
    /**
     * 申请发票
     *
     * @return void
     */
    public function testApplyInvoice()
    {
        $response = $this->post('/car/gas/v1/detail_v2', [
            'user_id' => 5000000,
            'gas_id' => 1001,
        ]);

        $response->assertStatus(200)
            ->assertJsonStructure([
                'ciphertext',
            ])
            ->dump();


        $response = $this->post('/car/gas/v1/detail_v2', [
            'xxx' => 'xxx'
        ]);

        $response->assertStatus(200)
            ->assertJsonStructure([
                'ciphertext',
            ])
            ->dump();
    }
}
让PHP再次伟大
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
最佳答案

测试而已 当然合理

也可以用数据供给器 代替

    public function gasIds()
    {
        return [
            [1,5000000],
            [2,5000001],
        ];
    }

    /**
     * @dataProvider gasIds
     */
 public function testApplyInvoice(int $gas_id,int $user_id)
{
$this->post('/car/gas/v1/detail_v2', [
            //'user_id' => $user_id,
            'gas_id' => $gas_id,
        ])->assertStatus(200)
            ->assertJsonStructure([
                'ciphertext',
            ])
            ->dump();
}
1年前 评论
讨论数量: 4

可以的吧,参数的各种测试

1年前 评论

理论上没什么问题 但是建议分开 比如 用户开发票(成功)/ 用户开发票(失败)等,一个场景对应一个函数

1年前 评论

测试而已 当然合理

也可以用数据供给器 代替

    public function gasIds()
    {
        return [
            [1,5000000],
            [2,5000001],
        ];
    }

    /**
     * @dataProvider gasIds
     */
 public function testApplyInvoice(int $gas_id,int $user_id)
{
$this->post('/car/gas/v1/detail_v2', [
            //'user_id' => $user_id,
            'gas_id' => $gas_id,
        ])->assertStatus(200)
            ->assertJsonStructure([
                'ciphertext',
            ])
            ->dump();
}
1年前 评论

变成流程测试而已,没啥问题的

1年前 评论

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