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

GodziLaravel's avatar

Start unti testin for an existing Laravel project

Hello everyone,

I'm currently working on a Laravel project that doesn't have unit tests set up yet. The database is a critical component for my application, but I've run into an issue.

When I try to create a test database using php artisan migrate, I encounter numerous errors. Because of this, I'm looking for an alternative method to generate the database without relying on the usual migration process.

Additionally, I'm considering duplicating the existing database on a test server and using this copy for unit testing. Is this approach feasible?

Any advice or suggestions would be greatly appreciated!

Thanks!

0 likes
9 replies
tisuchi's avatar

@godzilaravel Why you are considering Unit test/testing for database migration?

Just simply use seeder for replicating testing data locally.

1 like
GodziLaravel's avatar

@tisuchi Hi thanks for your answer . But in unit testing for some tests we need to delete some data and create others ... also I want to implement the CI/CD

tisuchi's avatar
tisuchi
Best Answer
Level 70

@GodziLaravel Here is my thought:

  • Make sure you have a seeder for seeding the entire application's data.

  • (Make sure) you have all the migration files.

  • Then write tests slowly for your project.

    • If you want to use automated (paid) service, then you can try this
    • Alternatively, you can ask Github co-pilot to write test for a function. You just need to confirm the test based on the co-pilot's suggestion.
  • A free package blueprint might be helpful for you to generate the test but needs some adjustment/tweaking. You can get some starting point here also.

BTW, also you can check out this discussion to get some idea.

tisuchi's avatar

@GodziLaravel

is it possible to use some services to generate migration like...

Of course, it's possible.

But, my opinion is to create a completely new branch that contains the fix of the migrations because this migration will be used in local project setup, CICD and ....

Why not use the extra package?

  • Because it's very trivial to fix the migration.
  • Reducing the dependency of the package (arguable)!
  • ...
1 like
Tray2's avatar

@GodziLaravel I would start by running php artisan schema:dump that would give a copy of the current database structure, then you can use that to migrate your test database.

Please or to participate in this conversation.