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

tnort's avatar
Level 4

Laravel writing tests for in prod apps

Hello,

I am new to a project that is already deployed in prod and I can see there are no tests written yet. I would like to start writing future tests to ensure further development of features doesn't break the current functionality.

As I am new to testing as well and I would appreciate some guidance. As far as I understand there are Unit and Future tests in PHP and they can further be divided into tests that are run in memory and tests performed over local DB.

Maybe some of these questions would seem dumb but please be patient with me :D

  1. If I test the application on the local machine before deployment and all works, I guess there is no need to run these tests in prod, do we? Or put it simpler do we ever run tests in prod?

  2. I have this application which has already seeded and can populate my database with data that are used in prod, does it make sense to write in memory tests?

  3. I have this test but I cannot figure out how to test notifications in Feature testing, in prod, the functionality seems to work and the email is queued before being sent.

public function test_reset_password_link_can_be_requested()
    {
        Notification::fake();

        $user = $this->getUserAdmin();

        $this->post(route('password.email'), ['email' => $user->email]);

        Notification::assertSentTo($user, ResetPassword::class);
    }

Your answers are highly appreciated. Thank you!

0 likes
6 replies
Sinnbeck's avatar

Ok before trying to debug the problem, let's handle your 2 questions

  1. It's a good idea to test your code locally. This makes it fast to make your tests green. But once you have a good test coverage, you might not want to tests locally every time. This is where your test server comes in. Personally I use github actions but there are several other alternatives. Once those tests are green you can deploy. You never test in production.
  2. Personally I prefer to use factories to generate the exact data I need for a test. Laravel is build for this and can do this very fast. I suggest using a real database. Just add a testing database to your local sql server. Sqlite is fast but can give false positives

Any questions?

2 likes
OussamaMater's avatar

@Sinnbeck The Sqlite giving false positives is something new to me, can you elaborate or give a simple example? thanks in advance

tnort's avatar
Level 4

@Sinnbeck,

Thank you so much for your reply.

Answer 1. Completely makes sense. It would add some extra expenses on the company bill I guess to have a testing server as well (test domain, test DB, maybe test host). But it is what it is :). Tho, I think with Nginx I can configure a testing domain to point to a version of the web app that is intended for tests. But, in that case, it would use the production DB which adds another variable to take care of, hence, I'll need a testing DB.

Answer 2.

I can even use the local DB for that matter I guess. Why would I want to have two distinct DBs?

Sinnbeck's avatar

@tudosm let me explain a bit more

  1. Github actions gives you quite a few free minutes (2000 a month). I haven't spend a dime on testing so far. And they set up a docker container with php and mysql. This is why you need your tests to add the data themselves. And you should never ever use your actual production database for testing

  2. Tests should not use your dev data. Tests should be 100% predictable. Also tests should should start from a clean database. You wipe it and run your migrations. Each tests then add data, which it removes after it was completed (laravel does this for you)

Please or to participate in this conversation.