Adding unit tests - McNamara84/ladis GitHub Wiki
Step 1: Generate unit test template
Run php artisan make:test TESTCASENAMETest in console to generate a new Template.
Naming convention for TESTCASENAME: Name of the class you want to test or were the method for testing is.
Step 2: Open the test file
Navigate to tests/Feature/ and open your newly created test file:
<?php
namespace Tests\Feature;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
class InstitutionTest extends TestCase
{
/**
* A basic feature test example.
*/
public function test_example(): void
{
$response = $this->get('/');
$response->assertStatus(200);
}
}
Step 3: Create test methods
Test method naming convention: Start with `test_, use descriptive names in snake_case, describe what the test is verifying.
Example:
public function test_get_types_returns_all_expected_values(): void
{
$expected = [
Institution::TYPE_CLIENT,
Institution::TYPE_CONTRACTOR,
Institution::TYPE_MANUFACTURER,
];
$this->assertEquals($expected, Institution::getTypes());
}
[!NOTE]
- Don't forget to delete the example method
test_example()from the new test file.- Naming convention for test methods is
test_DEXCRIPEWHATYOUARETESTING()and snake_case.
This is our first test case in this unit test. It checks whether the getTypes() method from the Institution class returns all expected types for our concept Institution.
Step 4: Execute tests
Run php artisan test --coverage to run all unit tests in one batch and get the coverage.