Skip to main content

QA dan Testing dengan Laravel dan PHPUnit - Bahagian 2

·525 words·3 mins
qa
rsmn
Author
rsmn
Software Engineer in Bangi

Dalam episod sebelum ni kita dah setup our Laravel endpoint dan jugak our first test.

Kali ni kita nak tambah test untuk endpoint kita yang ke-2

POST /api/rockets

  1. Boleh tambah roket dalam pipeline

Bukak tests/Feature/RocketTest.php yang kita dah generate haritu dan tambah the following test

class RocketTest extends TestCase
{
    use RefreshDatabase;

    // ..previous tests

    public function test_can_post_api_rockets(): void
    {
        // Send a POST request to the endpoint
        $response = $this->post('/api/rockets', [
            'name' => 'Test Rocket',
            'status' => 'preparing',
        ]);

        // Checks if it returns 201 status code aka success
        $response->assertStatus(201);

        // Second check if in the database there is a rocket with the name we gave earlier
        $this->assertDatabaseHas('rockets', ['name' => 'Test Rocket']);
    }

}

Cuba run test. Boleh guna --filter untuk only run test yang kita nak

vendor/bin/phpunit --filter test_can_post_api_rockets

Success.

phpunit

Next kita tambah pulak test untuk the final endpoint

PUT /api/rockets/{rocket}

  1. Boleh lancarkan roket

Lagi sekali bukak tests/Feature/RocketTest.php dan tambah the following test pulak

class RocketTest extends TestCase
{
use RefreshDatabase;

    // ..previous tests

    public function test_can_put_api_rockets(): void
    {
        // Setup test rocket with initial status of "preparing"
        $rocket = Rocket::factory()->create([
            'name' => 'Test Rocket',
            'status' => 'preparing',
        ]);

        // Send a PUT request to the endpoint and change status to "ready to launch"
        $response = $this->put("/api/rockets/{$rocket->id}", [
            'status' => 'ready to launch',
        ]);

        // Checks if it returns 200 status code aka success
        $response->assertStatus(200);

        // Second check if in the database there is a rocket with the new status
        $this->assertDatabaseHas('rockets', ['status' => 'ready to launch']);
    }

}

Run test for this endpoint pulak

vendor/bin/phpunit --filter test_can_put_api_rockets

Green.

phpunit 2

Finally run semua test.

vendor/bin/phpunit

Semua hijau. Syukurr.

phpunit all

Now tinggal satu je benda. Kita nak matchkan the original instruction dengan nama-nama yang kita guna untuk test method kita.

Ideally each “acceptance test” should have some kind of index. Something like F01, F02 or something.

Tengok balik senarai acceptance test yang kita dapat dari boss kita haritu.

  1. Boleh senaraikan semua roket along with their status (preparing, ready to launch, hit, missed)
  2. Boleh tambah roket dalam pipeline
  3. Boleh lancarkan roket

Ni pulak senarai test method yang aku dah siap rename so that next time nak cari hubungkait tak berapa nak struggle.

public function test_01_boleh_senaraikan_semua_roket_along_with_their_status {}
public function test_02_boleh_tambah_roket_dalam_pipeline {}
public function test_03_boleh_lancarkan_roket {}

Any document selalunya is a living document. So most probably wording untuk acceptance test tu akan berubah. Tapi at least numbering tu tak berubah so kita boleh refer numbering dia je.

Mostly test kita so far only cover whether an endpoint returns benda yang sepatutnya dia return.

Ada banyak lagi benda kita boleh test:

  1. Authorization Test - Adakah endpoint tu boleh diakses oleh Role yang sepatutnya
  2. Validation Test - Adakah endpoint tu boleh handle kepelbagaian input yang tidak sepatutnya dan yang sepatutnya
  3. Performance Test - Adakah endpoint boleh handle high workload. Ni pun boleh pecah lagi kalau nak. Test whether system boleh meet minimum performance standard (load test). Test max out workload nak tengok break kat mana (stress test). Test kalau spike tiba-tiba boleh handle tak (spike test). Test kalau normal workload tapi long running (endurance test).

Ok itu sahaja untuk posting kali ni. Hopefully kita boleh cover test-test lain lepas ni. Jumpa lagi.