D
D
Daniel2017-10-30 22:05:27
PHPUnit
Daniel, 2017-10-30 22:05:27

Laravel 5. How to properly delete data from the database after tests?

Good to everyone!
After executing the tests, all data is deleted from the working mysql database.
phpunit.xml:

<env name="APP_ENV" value="testing"/>
<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="QUEUE_DRIVER" value="sync"/>

Test example:
class PostTest extends TestCase
{
    use RefreshDatabase;

    public function setUp()
    {
        parent::setUp();

        $this->post = factory('App\Post')->create();
    }

    public function testPosts()
    {
        $this->get('/posts')->assertSee($this->post->title);
    }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Daniel, 2017-10-31
@Valtasaar

The problem turned out to be that the phpunit.xml settings file was not loaded.
I had to move the phpunit and phpunit.bat executables to the root of the application.
In the phpunit.bat file, write the path to the phpunit class.
Create environment configuration file .env.testing

A
Alexander Aksentiev, 2017-10-30
@Sanasol

Either delete the created data immediately after each method in the test, or it’s easier to use another clean database and force it as you like, and before and after the fresh migration tests, do it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question