colombo/autohttptests

Autogenerate http tests for laravel

v1.0.5 2020-08-06 09:50 UTC

This package is auto-updated.

Last update: 2024-11-06 19:44:31 UTC


README

Latest Version on Packagist Software License Total Downloads

No more writing tests by hand =D

Just execute the command to record your actions as http tests

php artisan autohttptest:create

imagen

The command will intercept your requests and translate the response as a test.

When finished, your test will be saved in tests/Feature/

Demo in video

autohttptests-gif

What does it test?

  • Request acting as same user
  • Make request using the same verb (GET,PUT,POST) with same arguments
  • Assert http response code
  • Assert errors
  • Assert redirection

Example code

<?php

namespace Tests\Feature;
use Tests\TestCase;

class SomethingTest extends TestCase
{
    public function testAutoHttpTest()
    {
        $this
        ->actingAs(\App\Models\User::find(1))
        ->post('home/something', [
            'name'         => 'a',
            'lastname'     => 'a',
            'city'         => '',
            'hobbies'   => '',
            'twitter_username' => 'a',
        ])
        ->assertStatus(302)
        ->assertSessionHasErrors([
            'name',
            'country_id',
            'twitter_username',
        ]);

        $this
        ->actingAs(\App\Models\User::find(1))
        ->post('home/something', [
            'name'              => 'asdfa',
            'lastname'          => 'asdfa',
            'country_id' => '1',
            'city'              => '',
            'hobbies'        => '',
            'twitter_username'      => 'asdfa',
        ])
            ->assertStatus(302)
            ->assertRedirect('home/something');
    }
}

Note

Here we capture an unsuccessful post, with errors. Then, a successful post with redirection

Install

Via Composer

$ composer require eduardoarandah/autohttptests

If you are using laravel < 5.4 add to providers in config/app.php

EduardoArandaH\AutoHttpTests\AutoHttpTestsServiceProvider::class,

Usage

php artisan autohttptest:create

Credits

License

The MIT License (MIT). Please see License File for more information.