簡體   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