繁体   English   中英

进行单元测试时应该考虑什么?

[英]What should I consider when doing unit tests?

进行单元测试时应该考虑什么? 采取什么步骤? 什么情况 每个功能要进行多少次测试? 等等

我也很感谢您的经验信息。 另外,我正在与laravel phpunit合作。 我做了一个例子,它的工作原理是:

public function test_for_clientUser() {
    $this->json('POST', 'clientUser', ['id'=>'232421'])->seeJsonStructure([[]]);
}

我发送一个带有ID的请求,它返回一个数组。 您还要将此测试添加到哪些方面?

您可以将测试分为每个控制器几个动作(列表,存储,显示,更新,删除等)。

例如,您可以测试您的输入验证是否符合某些发布要求:

   public function testStoringCityAsOwnerWithNotExistingCountryId()
{
    $input = [
        'country_id' => 0,
        'translations' => [
            [
                'name' => 'Варна',
                'lang' => 'bg'
            ]
        ]
    ];

    $response = [
        'errors' => [
            'country_id' => [
                trans(
                    'validation.exists',
                    ['attribute' => 'country id']
                )
            ]
        ]
    ];

    $this->asOwner()
        ->post('/cities', $input, $this->headers)
        ->seeStatusCode(ResponseCode::PERMISSIONS_DENIED)
        ->dontSeeJson();
}

您还可以测试列表信息,分页以及许多其他类似bug的情况。 实际上,漏洞的概念是,对于每个错误,您都应该编写新的测试!

根据此示例(来自Lumen编程指南 ): https : //github.com/Apress/lumen-programming-guide/blob/master/tests/app/Http/Controllers/BooksControllerTest.php

您将对此进行或多或少的测试:

GET /index
    - status code is 200
    - returns a collection of (well-formed) records

GET /show
    - status code is 200
    - returns a valid (well-formed) resource 
    - should fail with a non existing id (404)
    - should not respond with 200 if id is not numeric. Maybe 404

POST /store
    - the resource stores in DB
    - returns code 201 CREATED
    - returns a valid json qith a resource id

PUT /update
    - status code is 204
    - the resource has not the new value in DB
    - the resource now has updated data in DB
    - modified date was updated
    - should fail with a non existing id (404)
    - should not respond with 204 if id is not numeric. Maybe 404

DELETE /destroy
    - returns 204
    - should fail with a non existing id (404)
    - should not respond with 204 if id is not numeric. Maybe 404

在修改数据库(而不是测试数据库,就像在内存上运行的SQLite实例)时,这些不是唯一的,但可能是功能正常的。 我不能保证。 作者称它们为验收测试,但之所以不是,因为它们是白盒测试(直接操作数据库)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM