R
R
Romi2022-04-10 01:00:43
PHPUnit
Romi, 2022-04-10 01:00:43

Testing (PHPUnit) databases in Laravel - completely confused about the setup - how to figure it out?

For some reason, the Laravel documentation https://laravel.com/docs/8.x/database-testing assumes that I've only been doing database testing for two years now. But it's not. The topic of testing is quite fresh for me. So I don't understand at all:

1. Why should I do "Resetting The Database After Each Test"?

2. Why do I need to specify 'sqlite' and ':memory:' in phpunit.xml settings?

Even in version 5.4 there is such a feature:

use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;

this also applies to paragraph 1.

Can someone simply and clearly explain in two sentences why all this is needed at all?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Romi, 2022-04-10
@romicohen

In general, it turns out that the guide writers are too clever again :)
It is quite enough to put the line in the class definition: Then the records in the database are simply rolled back - in the DatabaseTransactions trait this is implemented through a hook: you can do without other frills. In addition to this, you can use another copy of the main database purely for testing purposes.
use DatabaseTransactions;
beforeApplicationDestroyed()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question