The 4 Steps That Nearly Tripled My PHPUnit Test Efficiency
Hi, recently on my commercial project, I tried to optimize the runtime of my PHPUnit tests. These tests mainly consist of API feature tests and some unit tests. I made the decision to optimize them because I don’t like waiting for a long time when the tests pass successfully, and then deploying the code to production.
Before optimalization:
I used to run all of my tests on Github Actions. It’s one of the best ways to run tests in the cloud.
Step #1 App Debug
Check your phpunit.xml file. When running tests on the cloud, you need to include the debug option. I recommend disabling this option:
<php>
<server name="APP_DEBUG" value="false"/>
<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>
<env name="MAIL_MAILER" value="array"/>
<env name="QUEUE_CONNECTION" value="sync"/>
</php>
Step #2 Config Cache
When running tests, try to optimize your configuration. This action is recommended on the production environment.
- name: Config cache
run: php artisan config:cache
Step #3 Route Cache
Similar like config cache:
- name: Route cache
run: php artisan route:cache
Step #4 Xdebug off
When running tests in Github Actions, the Xdebug package is enabled by default. In my case, I don’t need this feature and can disable it by using XDEBUG_MODE=off, like this:
- name: Run tests
run: XDEBUG_MODE=off ./vendor/bin/phpunit --stop-on-failure
Additionally, if one test fails, I don’t need to run the rest of the tests.
Result:
Awesome My tests was optimized with using 4 simple steps.
Try my tips in your project :)
Don’t forget about follow my account ❤