Quality control is a vital step in any manufacturing process. Can't imagine this being an easy job mentally. Hats off to this woman. pic.twitter.com/aVhSGCZG67
— Quantum Voyager (@QuantumVoyages) January 28, 2024
Harini aku nak ajar korang pasal testing or some people call it QA.
Ni steps yang kita akan buat
- Sediakan several simple Laravel API endpoints for testing
- Tambah test.
- Run the test.
Semua steps ni kita akan jayakan dengan menggunakan replit.
#1 Sediakan several simple Laravel API endpoints for testing #
First skali kita kena ada benda yang kita nak test dulu.
Now just imagine kita ada satu sistem utk lepaskan roket ke sasaran musuh.
So boss ckp,
ok Man, ko skang in caj utk make sure 3 feature ni berfungsi.
- Boleh senaraikan semua roket along with their status (preparing, ready to launch, hit, missed)
- Boleh tambah roket dalam pipeline
- Boleh lancarkan roket
Aku dah prepare endpoint ni korang just fork je. To do that kena create akaun replit dulu.
Create akaun replit.com #
So first skali, pergi ke replit.com dan klik Sign Up.
Lepas tu korang klik repo yang aku dah buatkan ni.
https://replit.com/@rsmn/rocketlauncher?v=1
Klik Fork & Run saja.
These are the endpoints for those yang curious.
// routes/api.php
// GET /api/rockets
Route::get('rockets', function () {
return App\Models\Rocket::all();
});
// POST /api/rockets
Route::post('rockets', function (Request $request) {
$rocket = new Rocket();
$rocket->name = $request->name;
$rocket->status = $request->status;
$rocket->save();
return response()->json($rocket, 201);
});
// PUT /api/rockets/{rocket}
Route::put('rockets/{rocket}', function (Request $request, Rocket $rocket) {
$rocket->status = $request->status;
$rocket->save();
return response()->json($rocket, 200);
});
#2 Tambah test #
Next kita tambah test. Kita start dengan first endpoint dulu
GET /api/rockets
- Boleh senaraikan semua roket along with their status (preparing, ready to launch, hit, missed)
Sebelum kita tambah test, kita buat manual test dulu.
Tekan Run jika belum. Dan klik butang New tab
Browser akan buka new tab. URL dia akan lebih kurang rupa macam ni
https://25c234b0-e762-4949-919d-853de8d2bd13-00-18j3aexydlz9p.sisko.replit.dev/
Tambah api/rockets
kat belakang dan klik Enter untuk pergi ke URL tersebut
https://25c234b0-e762-4949-919d-853de8d2bd13-00-18j3aexydlz9p.sisko.replit.dev/api/rockets
Success.
Skang kita nak automate proses “bukak browser dan klik URL” ni.
Generate test dulu. Run kat shell.
php artisan make:test RocketTest
Buka test tersebut ie tests/Feature/RocketTest.php
dan define it as follow. Jangan lupa bubuh use RefreshDatabase
tu ye. Benda tu make sure every time kita test kita run migration.
class RocketTest extends TestCase
{
use RefreshDatabase;
public function test_can_get_api_rockets(): void
{
$response = $this->get('/api/rockets');
$response->assertStatus(200);
}
}
Sebelum boleh test, adjust dulu phpunit.xml
dan make sure dua value ni seperti di bawah. Benda ni untuk bagi testing kita run guna SQLite dan in memory. So ringan dan pantas.
<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>
#3 Run the test #
Akhirnya saat yang ditunggu-tunggu.
Run the magic command. Buka shell dan run
vendor/bin/phpunit
Akhirnya 😭
Nota: Bilangan test success anda mungkin lain dengan screenshot sebab saya dah buang default test that comes with Laravel ie tests/Feature/ExampleTest.php
dan tests/Unit/ExampleTest.php
Ok selesai endpoint 1. Next post kita test endpoint ke 2 pulak.
Jumpa lagi ya harap tak perlu tunggu 3 tahun untuk posting seterusnya.