Migration only once with Laravel dusk

According to the documentation "Testing the database" I can reset the database after each test (first option). The second option is to run the test using transactions. It seems to me that this is the best approach to me, but if I want to work with a transaction, the migration is not performed.

Is there a way to migrate once for the entire testing process?

In other words, I want to start the migration, run all the tests with the transaction, and then rollback. I tried with the documentation, but I think something is missing.

+6
source share
4 answers

For some time this has been contrary, and current migrations combined with migrations seem to do the trick. A snapshot of my test looks like this:

<?php

namespace Tests\Browser;

use App\User;
use Tests\DuskTestCase;

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

class DefaultTest extends DuskTestCase
{
    use DatabaseMigrations, DatabaseTransactions;

    /**
     * A Dusk test example.
     *
     * @return void
     */
    public function test_something()
    {
        //Add test stuff here
    }
}

In my actual test, I have several factories, and they seem to go through migrations with data destroyed after the test, as expected.

+3
source

It is not possible to start DatabaseTransactions in combination with twilight at the moment.

https://github.com/laravel/dusk/issues/110

Creating a user record and using it in a browser are performed in two different processes. This means that the created user is part of a database transaction that is not committed and therefore not available in the browser process.

. . , , .

https://laravel.com/docs/5.4/dusk#environment-handling

Dusk , .env.dusk. {environment} . , dusk , .env.dusk.local.

Dusk .env .env. , .env .

, DatabaseMigrations. use DatabaseTransactions .

+2

, , , - , laravel.

phpunit ( setUp/createApplication), , . , ( ) , .

( , , ..), , , , phpunit.

Dusk :

  • .env.dusk.* ( , , )
  • phpunit (.. , )
  • phpunit , ( php-fpm php- ( nginx)) - , . db .

, DatabaseTransactions Foundation, Dusk, / Dusk.

, sqlite , .

+1

:

abstract class DuskTestCase extends BaseTestCase {

   ...

    /**
     * @param int $batchCounter
     * @param string $className
     * @param int $threshold
     */
    public function refreshDb(&$batchCounter, $className = '', $threshold = 0) {
        if ($batchCounter <= $threshold) {
            //TODO: Here is where you'll want to run migrations and seeds and whatnot.
            $batchCounter++;
            $this->consoleOutput(trim($className . ' called refreshAndSeedTestingDb and $batchCounter++. $batchCounter=' . $batchCounter));
        }
    }

   /**
    * @param string $msg
    */
   public function consoleOutput($msg) {
       Log::debug($msg);
       $output = new \Symfony\Component\Console\Output\ConsoleOutput();
       $output->writeln($msg);
   }

:

class ExampleBrowserTest extends DuskTestCase {

    protected static $countDbRefreshed = 0;

    public function setUp() {//runs before every test function in this class
        parent::setUp();
        $this->refreshDb(self::$countDbRefreshed, __CLASS__); //inside uses a property to run only once per class
    }
...
0

Source: https://habr.com/ru/post/1668517/


All Articles