Laravel Feature Tests
A feature test calls a route and checks the answer. It proves the app behaves as the lesson claims.
A first route
The test calls the route with get.
Route::get('/l46/hello', function () {
return 'Hello from Laravel';
});A route with a name
The route reads the name from the address.
Route::get('/l46/greet', function (Request $request) {
return 'Hello, '.$request->query('name');
});A short status route
A status route answers with one word.
Route::get('/l46/status', function () {
return 'OK';
});Example
use Tests\TestCase;
class Lesson46Test extends TestCase
{
public function test_hello_page_loads(): void
{
$this->get('/l46/hello')->assertOk()->assertSee('Hello from Laravel');
}
}Key points
- Feature test: a test that calls a route and checks the answer.
- Use a feature test to prove a route returns the right page.
- Use assertOk to check for a 200 answer.
- Use assertSee to check that the page holds some text.
In this example, the test calls the hello route. assertOk checks for a 200 answer. assertSee checks that the page holds the greeting. A feature test calls a route and checks the answer. Run the test with php artisan test to see the output.
Note
A feature test runs the real app, so a pass means the route works.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
What does assertSee check?