mysql – Dump database files during testing or very slow tests


I have a PHP project using Yii2 and I'm testing this whole Codeception case.

Everything would be fine, but tests using the database take an unrealistically long time . In particular, due to the large number of fixtures with a large amount of data.

I even divided the tests into unit and integration – with and without a database. See the difference:

$ time ./vendor/bin/codecept run unit
Tests.unit Tests (67) 
Time: 4.22 seconds, Memory: 71.25MB

OK (67 tests, 380 assertions)

real    0m6.044s
user    0m1.292s
sys     0m0.208s


$ time ./vendor/bin/codecept run integration
Tests.integration Tests (35) 
Time: 4.76 minutes, Memory: 54.25MB
Tests: 35, Assertions: 58, Failures: 1.

real    4m46.052s
user    4m35.880s
sys     0m0.584s

That is, about 50 times longer.

The question is this: instead of loading and unloading rows from the database, it may be possible to somehow unload all the data into the database, save the table file with all this data and copy this file before each test, restoring the set of test data. In SQLite, this is easier, but in my case, the substitution will not work.

If this is not possible or does not make sense, then how can I speed up the execution of tests using the DB?

Additional Information:

  • I am using the official method for loading fixtures in Yii2 via the _fixtures method
  • There can be a lot of data in fixtures, and usually they are all needed to maintain integrity and emulate combat conditions.
  • The database for tests is located inside the vagrant environment. Perhaps this is the problem. But I would not want to move the database from the guest system to the host. Running tests inside a Vagrant machine does not provide any gain.
  • Perhaps someone will advise Docker, but I can't do it somehow with it. Practice may be needed, but so far I have with him not even on "You", but on "It"
  • Used DB – MySQL
  • Moki is cool. As long as they are relevant. But the problem is that in this case it is very important that everything works well together , not just a separate module. In addition, it cannot be denied that creating a large number of mocks makes tests practically unreadable.


For those who stumble upon the same problem, I'll write it down here.

1) Please, stay tuned for Mysql versions and their updates, even for a couple of versions "Outdated" database can greatly influence loading and debugging of the code.

2) The main rule of the speed of the systems Avoid any possible "intermediaries" of programs in your code, the more there are, the longer the code is executed by default. Be paranoid about including third-party scripts and environments that "supposedly" I emphasize "supposedly" make your life easier, treat them like the steps of a straw staircase, while you are standing on it at a height of 50 floors. Sooner or later, this "hope" of intermediaries will let you down. The more intermediaries, the lower the speed of the system and the more it is susceptible to hacking. In your example question, I counted at least 3 of them.

3) vagrant environment and the speed of work through it also depends on the way of use. How exactly did you connect is the question.

4) And of course the royal question. What exactly do you mean by "test"? Be as specific as possible about the results you want from the system, including by asking questions on So.

Thank you for your attention.

Scroll to Top