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.
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.
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 .
Dusk , .env.dusk. {environment} . , dusk , .env.dusk.local.
Dusk .env .env. , .env .
, DatabaseMigrations. use DatabaseTransactions .
use DatabaseTransactions
, , , - , laravel.
phpunit ( setUp/createApplication), , . , ( ) , .
setUp
createApplication
( , , ..), , , , phpunit.
Dusk :
.env.dusk.*
, DatabaseTransactions Foundation, Dusk, / Dusk.
DatabaseTransactions
, sqlite , .
:
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 } ...
Source: https://habr.com/ru/post/1668517/More articles:How to run a single test or all tests in a specific folder in Visual Studio? - unit-testingGradle Could not resolve org.springframework.boot: spring-boot-gradle -plugin: 1.4.2.RELEASE (repo1.maven.org: Nome o servizio sconosciuto) - javapandas dataframe dtypes compare equality - pythonInvalid provider for DynamicTestModule NgModule module when testing a service in Angular 2 - javascriptActiveRecord :: StatementInvalid PG :: UndefinedColumn: ERROR - ruby-on-railsHow to show error message on input text - react-nativeFirebird.NET Provider and Embedded Server 3 - c #replacing a complex template with sed - regexTOAuth2Authenticator: How to update an expired token? - restCombining Mongodb with future documents - mongodbAll Articles