Teszteket a következő paranccsal generálhatunk:
php artisan make:test ValamiTest
A teszt neve után a végződés kötelezően Test.
<?php namespace Tests\Feature; use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\WithFaker; use Tests\TestCase; class Valami extends TestCase { /** * A basic feature test example. * * @return void */ public function test_example() { $response = $this->get('/'); $response->assertStatus(200); } }
Javítsuk így
<?php namespace Tests\Feature; use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\WithFaker; use Tests\TestCase; class Valami extends TestCase { /** * A basic feature test example. * * @return void */ public function test_example() { $response = $this->get('/api/employees'); $response->assertStatus(200); } }
A tesztfüggvények neve tetszőleges, de a test szóval kell kezdődnie.
A teszt futtatása:
php artisan test
Vagy:
./vendor/bin/phpunit
php artisan config:clear php artisan cache:clear php artisan config:cache
A teszteket a phpunit.xml fájlban konfigurálhatjuk.
Csak adott teszt futtatása:
php artisan test --filter=EmpTest
<?php namespace Tests\Feature; use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\WithFaker; use Tests\TestCase; class EmployeeTest extends TestCase { /** * A basic feature test example. * * @return void */ public function test_getemps() { $response = $this->get('/api/employees'); $response->assertStatus(200); } public function test_addemp() { $response = $this->post('/api/employees', [ 'name' => 'Arany Ede', 'city' => 'Miskolc', 'salary' => 655 ]); $response->assertStatus(201); } }
Vegyünk fel egy kapcsolat típust:
'sqlite_memory' => [ 'driver' => 'sqlite', 'database' => ':memory:', 'prefix' => '' ],
Állítsuk be teszteléshez:
<server name="DB_CONNECTION" value="sqlite_memory"/>
php artisan config:cache
Adjuk hozzá csoportot:
/**
* @group eredeti
*/
A teljes kód:
<?php namespace Tests\Feature; // use Illuminate\Foundation\Testing\RefreshDatabase; use Tests\TestCase; class ExampleTest extends TestCase { /** * A basic test example. * @group eredeti */ public function test_the_application_returns_a_successful_response(): void { $response = $this->get('/'); $response->assertStatus(200); } }
Futtatás kizárással:
./vendor/bin/phpunit --exclude-group eredeti