简体   繁体   English

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

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

What should I consider when doing a unit test? 进行单元测试时应该考虑什么? What steps? 采取什么步骤? What cases? 什么情况 How many tests per function? 每个功能要进行多少次测试? etc. 等等

I would also appreciate information about your experiences. 我也很感谢您的经验信息。 Also I'm working with laravel phpunit. 另外,我正在与laravel phpunit合作。 I did an example and it worked: 我做了一个例子,它的工作原理是:

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

I send a request with an id and it returns an array. 我发送一个带有ID的请求,它返回一个数组。 What more prodrice do you add to this test? 您还要将此测试添加到哪些方面?

You can separate your tests into several actions(List, Store, Show, Update, Delete, etc) for each controller. 您可以将测试分为每个控制器几个动作(列表,存储,显示,更新,删除等)。

For example you can test your input validation for some post form: 例如,您可以测试您的输入验证是否符合某些发布要求:

   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();
}

Also you can test your listing information, pagination and many many other cases that you can find like bug. 您还可以测试列表信息,分页以及许多其他类似bug的情况。 Actually the hole conception is that for every bug you should write new test! 实际上,漏洞的概念是,对于每个错误,您都应该编写新的测试!

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

You would test more or less this: 您将对此进行或多或少的测试:

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

As you are modifying a DB (than should be a testing DB, like a SQLite instance running on memory) these are not unitests but maybe functional. 在修改数据库(而不是测试数据库,就像在内存上运行的SQLite实例)时,这些不是唯一的,但可能是功能正常的。 I can't assure. 我不能保证。 Author calls them acceptance tests but they are not since they are white-box tests (manipulating DB directly). 作者称它们为验收测试,但之所以不是,因为它们是白盒测试(直接操作数据库)。

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

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