cooperino's avatar

Is there a way to clone dev db data to test db?

I am just now starting to learn about testing

At first I ran php artisan test and it showed me that I am missing the test database so I simply created an empty db. Then the tests started running and I saw that the test database have the same tables as the app's database.

Was what I did correct? i.e to create an empty database and then let php artisan test create the app's db. And, is there a simple way to copy the current development database data and use it? Or I have to manually insert dummy data now?

Or I can somehow use the dev db instead (And should I?)

0 likes
8 replies
Sinnbeck's avatar

You normally use seeders to populate the tables, but apart from that, it sounded spot on. it will run your migrations which will create the tables :) Dont clone the data from the dev db as it should be set up by the tests

1 like
cooperino's avatar

@Sinnbeck And that is the correct workflow when testing? To use the test database and seed it? Or, you might set the php artisan test command to use a different database such as the dev one, instead of working on a different one?

Because it feels like the dev db is the actual test db and now it's like doing the work twice, because what I want to test right now is an endpoint and see if I get the correct JSON. I already have all the data in the dev DB.

But I think my misunderstanding is deeper than that, I'd like if you can help me clarify it:

I have an endpoint https://my-app/endpoint which returns a JSON based on data in the database and I want to make sure that this endpoint returns the correct data and does not crash for several scenarios and edge cases.

Is it HTTP testing: https://laravel.com/docs/8.x/http-tests ?

is it database testing: https://laravel.com/docs/8.x/database-testing ?

Is it both? And then should I also get better experience with factories and seeding since that's that correct flow instead of relying on my dev db?

Sinnbeck's avatar

@cooperino Well there are multiple ways of testing.

  1. Add everything you ever need to the database in one big seeder. The problem with this approach is speed as you need to redo it between tests if the test changes the data
  2. Seed only what is needed for a certain test. This will make it quite fast. So lets say you want to test an endpoint. You seed 40 random items in the database and your test makes sure that 40 are returned and the response is ok. Or you add 1 random item, but make sure that the test user does not own it, and test that the user cannot edit it.
1 like
Sinnbeck's avatar

@Snapey Oops! 🤦. Thanks for catching that

It should also be a list, but formatting is broken currently it seems.

1 like

Please or to participate in this conversation.