How can I speedup the unittests for CakePHP

704 Views Asked by At

I am developing with CakePHP 2.4.3 and use the Unittest a lot. At the moment mostly on models.

Is there a possibility to shorten the time these test need to run? What makes them so slow? The db insertions of the fixtures?

I notice that I don't have the patience to wait for the tests to run and while waiting I start doing other things and then when I come back I lost track of what problem I was testing.

Thanks for any hints!

CalamityJane

1

There are 1 best solutions below

3
On BEST ANSWER

I strongly disagree here with marks comment:

Unittests are not supposed to be "speedy"

Technically they're not that's true but it can become annoying. If you use CI on a large project testing can become horrible slow. You don't want to wait 30min until all tests are done. We had this case in a project with ~550 tables.

The bottleneck is in fact the fixture loading. Because for each test all fixtures have to be created again over and over. It is slow.

We use an internal plugin to copy a test database template to the test database instead of using fixtures. This dropped the time to run the tests on this project from 30+ minutes down to a few minutes.

An open source plugin that should be capable of doing this as well is https://github.com/lorenzo/cakephp-fixturize. You can load fixtures from SQL files or load them from a template database as well, see this section of the readme.md.

If you just have to test a single method there is no need to run all tests, you can filter the tests:

cake test <file> --filter testMyMethod