Summer Sale! All accounts are 50% off this week.

orest's avatar
Level 13

When and why would you write integration/unit tests

In what case ( what is the criteria ) would you write integration and unit tests, when you can write a feature test that ensures that you get the expected result, without writing integration and unit tests for all the classes that are involved for that feature ?

0 likes
3 replies
martinbean's avatar

@orest Tests are meant to test your application and give you confidence it works.

Feature tests will give you a lot of code coverage so if you’re happy with this then yes and confident it’s testing your application, there’d be little point dropping down a level to write integration and unit tests.

Sinnbeck's avatar

I work alot with statistical data and the likes and there I use unit tests to ensure my base data is correct. In the data that gets pushed to the frontend it might be rounded off (decimals) or converted to percentages, so a feature test is nice, but I don't feel confident with it.. So unit tests it is

Tray2's avatar

Feature tests are usually a bit slower than a unit test so if you run a lot of slow tests it might be better to test that functionality only with one feature test and several unit tests.

Take this unit test as an example

 /**
    * @test
    */
    public function the_title_must_start_every_word_with_an_upper_case_letter()
    {
        $book = Book::factory()->make([
            'title' => 'THis iS tHe titLE'
        ]);
        $this->assertEquals('This Is The Title', $book->title);
    }

It never hits the database so it shaves of a few milliseconds every time it's ran.

 public function setTitleAttribute($value)
    {
        $this->attributes['title'] = ucwords(strtolower($value));
    }

If you are a solo developer then it might be less important to do unit tests but if you are part of a team the need to test in isolation might be greater since you all might be working with the same feature but in different classes.

Please or to participate in this conversation.