sqlite_test_cache
Test suites are slow because they set up everything before every test method. In large projects the setup methods, especially from base classes, are called thousands of times. With Sqlite as the database the results of the setup methods can be cached and this is what this module does.
How to use it
- Download the module. There's no need to enable it, the base class just needs to be there in the codebase.
- Set the
SIMPLETEST_DBenvironment variable to an sqlite file location (e.g.sqlite://localhost//dev/shm/test.sqlite- this stores the database in memory). - Create your test class extending
SqliteCachedKernelTestBase, e.g.class MyAppTestBase extends SqliteCachedKernelTestBase. - Do not override the
setUpmethod in any of your test classes that are based onSqliteCachedKernelTestBase. - Instead, use the two methods described below.
- Do not call
parent::setUpDatabaseorparent::setUpClassin any of your test cases. The parent methods will be called automatically by the base class. - After changing any of the
setUpDatabasemethods delete the cached files (their names start withcache-). If you're using docker and theSIMPLETEST_DBis in/dev/shmthen restarting the container will be enough.
setUpDatabase
Put all your installation code here, e.g. installConfig, installSchema, installEntitySchema, etc. This method will be fired only once for the whole testsuite.
Note
Don't assign values to any class properties in this method. They won't be there when the test runs.
setUpClass
This is the place for setting up the class. Things like $this->entityTypeManager = $this->container->get('entity_type.manager'); can be added here. The method is called before every test (like the regular setUp method).
Known issues
In some cases the first test in a class may fail with an error unrelated to the subject matter. That's caused by static caching in core. Adding an empty test method at the beginning of the class forces a re-load with the cached database and makes the next tests work as expected.