Question:
I have a PHP project using Yii2 and I'm testing the whole Codeception thing.
Everything would be fine, but tests using the database are unrealistically long . 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. Look at 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
Integration:
$ 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, maybe you can somehow upload all the data to the database, save the table file with all this data, and copy this file before each test, restoring the test data set. In SQLite, this is easier, but in my case it 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'm using the official method of 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 lies inside the vagrant environment. Perhaps the problem lies precisely in this. But I would not want to move the database from the guest to the host. Running tests inside the Vagrant machine does not provide any gain.
- Perhaps someone will advise Docker, but it doesn’t work for me somehow with it. Maybe practice is needed, but so far I don’t even have it on “You”, but on “It”
- Database used — MySQL
- Moki is cool. As long as they are relevant. But the problem is that in this case it is very important that everything together works well, and not just a single module. In addition, it cannot be denied that the creation of a large number of mocks makes the tests almost unreadable.
Answer:
For those who stumble upon the same problem, I'll write it here.
1) Please keep track of Mysql versions and their updates, even for a couple of versions An "obsolete" database can greatly affect code loading and debugging.
2) The main rule for the speed of the systems Avoid any possible "intermediary" 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 "allegedly" I emphasize "allegedly" make your life easier, treat them like steps on a straw ladder while you're standing on it at a height of 50 floors. Sooner or later, this "hope" of the 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 how you use it. How exactly did you connect – the question.
4) And of course the royal question. What exactly do you mean by "test" ? Describe as specifically as possible the results you want to get from the system, including asking questions on So.
Thank you for your attention.