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

时间:2017-01-03 15:59:49

标签: php laravel unit-testing phpunit lumen

进行单元测试时应该考虑什么?什么步骤?什么案子?每个功能有多少个测试?等

我也很感激您的经历。我也在使用laravel phpunit。我做了一个例子并且有效:

document.styleSheets[0].insertRule(rule, 0)

我发送一个带有id的请求,它返回一个数组。你在这个测试中添加了什么更多的产品?

2 个答案:

答案 0 :(得分:0)

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

例如,您可以测试某些帖子表单的输入验证:

   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一样的东西。 实际上,洞穴的概念是,对于每个bug,你都应该编写新的测试!

答案 1 :(得分:0)

根据这个例子(来自 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实例)时,这些不是unitests,但可能是有用的。我无法保证。作者称他们为接受测试,但他们不是白盒测试(直接操作DB)。