简体   繁体   中英

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. 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. 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. 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

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. I can't assure. Author calls them acceptance tests but they are not since they are white-box tests (manipulating DB directly).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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