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

bor1904's avatar

Testing laravel apps practices

Hello, I have a questions to you about methods using by you in small private Laravel projects and testing practices in your software companies.

Do you write tests for small web apps or only for mid/large systems? Always phpunit or always dusk? Or it depends?

Do you develop your projects in TDD or usually you write test after coding?

You normaly cover whole app by tests or only critical area/flow?

Do you write unit or future tests more often? Or always both?

I start long project In small team and havent too mach practical knowladge about todays best practices in this area.

Thank you in advance! K

0 likes
9 replies
DarrellFranklin's avatar

To understand the logic of the unit testing process as much as possible, you can look at the ucfirst function, which is needed to quickly convert one heading to another, which comes from uppercase.

While we use the unit test solely to verify that the logical unit of the program code is correctly built, the functional test makes it possible to check whether the correct option is indicated correctly. If we analyze this interpretation in more detail, then a functional check allows the tester to simulate a set of actions that the client can potentially perform inside the utility in order to launch the option of action he needs.

1 like
bugsysha's avatar

Always TDD. Only PHPUnit. Feature tests to cover the user interaction and unit tests to cover the details.

2 likes
automica's avatar

I’m working on an api that will be consumed by a react front end.

We’re using TDD for both sides: PHPUnit feature tests to test the end points - Unit tests to test the methods and Cypress tests to test the React front end for end to end testing.

1 like
martinbean's avatar
Level 80

Do you write tests for small web apps or only for mid/large systems?

@bor1904 Why do you think you would only write tests for “mid” or “large” systems? Is a “small” app not worthy of being tested? Do you not care if a “small” app works or not?

Testing is exactly that: testing something works. It makes no claims about how “big” an app has to be before it warrants testing. You write tests if you want to be sure the code you’re writing works, and you omit them if you’re not bothered.

Always phpunit or always dusk? Or it depends?

Personally, PHPUnit-based tests. I’ll write feature tests that essentially make HTTP requests and then make assertions on the responses, i.e. whether I’m expecting a successful response or say, validation errors if I intentionally submit incorrect data.

I’ll then create another tests/Integration directory and put tests in their that test the actual classes of my application. So things like service classes, jobs, etc. Classes that I can test together but without invoking them through a HTTP request or CLI command.

I write very little unit tests as I find feature and integration tests give me enough coverage. I also can’t remember the last time I used Dusk. I’d maybe use it if I was doing a lot of UI-heavy work, but find it slow and would probably use something else for automated browser testing like Cypress if I needed to do these types of tests.

Do you develop your projects in TDD or usually you write test after coding?

TDD is nice, but never works in practice for me. It can work for feature tests where you know you want an endpoint to receive some data and return a particular response and then you can use your test to drive the implementation, but I find it impossible to do TDD at the unit level, as you kinda need to already know the code you’re writing ahead of time to be able to write granular tests like that.

So I’ll add tests as I code. I’ll get something working, as I may go through several iterations of renaming things the more code I write. Once I get something tangible (i.e. a controller action that does something) then I’ll write a test for that.

You normaly cover whole app by tests or only critical area/flow?

I cover the parts I need covered and want to be confident work. Too many people chase 100% code coverage but then you ended up testing useless things like the HTTP kernel class or form request classes, and end up with tests for the sake of tests. Why are you ever going to use a form request classes on its own? You wouldn’t. So its logic would be covered by the feature test for the controller that makes use of it. That request class is then covered by that test; no need to write a separate “unit” test for that form request class.

As mentioned above, I’ll also test validation by intentionally passing invalid data to endpoints. So omitting fields that are required, passing out-of-range values for things like min and max rules, etc. This can obviously get laborious quickly, so you can use PHPUnit data providers to simplify these tests:

public function testFieldIsRequired(string $field): void
{
        $data = [
            'headline' => 'Test Headline',
            'summary' => 'Test summary.',
            'body' => 'Test body.',
        ];

        // Remove the required field
        Arr::forget($data, $field);

        $response = $this->post('/articles', $data);

        // Assert validation failure for given field
        $response-> assertSessionHasErrors($field);
}

public function requiredFieldsDataProvider(): array
{
    return [
        'headline' => ['headline'],
        'summary' => ['summary'],
        'body' => ['body'],
    ];
}

Do you write unit or future tests more often? Or always both?

I pretty much covered this above.

I start long project In small team and havent too mach practical knowladge about todays best practices in this area.

The crux is, you can get quite far with feature tests. Start with feature tests, make HTTP requests to your application and then assert the side effects. Check models were created that you expect to be created; check expected validation errors are thrown when you submit intentionally bad data; test queue jobs and mails are dispatched if they need be. This will quickly give you a lot of coverage and confidence in your applications. If you’re writing complicated business logic with lots of classes, you can then drop down to writing integration or unit tests that cover these classes in isolation.

5 likes
bugsysha's avatar

If there is any JavaScript framework being used on the project, then using proper testing tools for it like VueTestUtils.

1 like
gletsos's avatar

Hello, excuse me if this isn't the best place to ask a further question, let me know if I should move it elsewhere.

I'd like to ask about the time when you build nested resources (like Photos with Comments in Laravel docs) and you are about to write feature tests for hitting the endpoints of GET / POST /photos/{photo}/comments: alongside checking that the correct changes have been made to the child (Comment) in the database, would you also check that nothing has changed about the parent (Photo) in the database as well?

automica's avatar

@gletsos start another thread for your question as this one has already been answered.

1 like

Please or to participate in this conversation.