Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

shanely's avatar

what is the difference of Unit test and Feature Test

Hi I have this version of laravel in my compose "laravel/framework": "^6.2",

In test folder there are 2 folders inside test and feature folder. can I ask what is the difference of this two and what should I test for test folder and feature test folder.

Thank you in advance

0 likes
7 replies
Tray2's avatar

A unit test should just test one method like checking that a sum method returns the sum of two numbers while a feature test can test the functionality of a chain of methods.

Unit test example

    /**
    * @test
    */
    public function name_property_returns_the_authors_last_name_and_first_name()
    {
        $author = factory(Author::class)->create([
            'first_name' => 'Robert',
            'last_name' => 'Jordan'
        ]);

        $this->assertEquals('Jordan, Robert', $author->name);
    }

Feature test example

    /**
    * @test
    */
    public function when_users_update_a_book_they_are_redirected_to_the_book_index_and_are_shown_a_success_message()
    {
        $this->createForeignKeys();
        $this->signIn();
        $book = factory(Book::class)->create();
        $book->title = 'Kalle';

        $response = $this->patch('/books/' . $book->id, $book->toArray());

        $response->assertStatus(302);
        $response->assertLocation('/books');

        $response = $this->get('/books');
        $response->assertSee(e($book->title) . ' successfully updated.');
    }
2 likes
shanely's avatar

@tray2 ,

Is this built in method in laravel ?

$this->createForeignKeys(); $this->signIn();

1 like
shanely's avatar

also where I could find in the documentation the command for testing like assertsee ?

1 like
Tray2's avatar

Yes those are not part of standard Laravel.

The $this->createForeignKeys(); is a method that creates all the necessary foreign keys for that and other tests. I didn't want to use the regular setUp()method since I don't want them in all my tests in that test class.

The this->signIn() method creates a user and signs that user in. Jeffrey uses that one in a couple of his series on TDD.

assertSee is a Laravel specific assertion just like @sinnbeck stated.

1 like

Please or to participate in this conversation.