Tips to improve and speed up the running of PHPUnit

Tram Ho

Save 50% – 70% of the time running the PHPUnit test for your project with just a few incredibly simple tips.

Use –filter

I have seen a lot of newbie friends in the process of development, every time I ran a test, I tested all the tests. If you do not know, when adding the --filter <Callable> flag to the command line it will help you to only run tests that have names that match Callable as class names, testcase (method) or even just a namespace . This will save you a lot of time when not having to run all the tests.

PHPUnit Caching

This is a feature of PHPUnit that allows us to cache the previous test result to a file called .phpunit.result.cache . This file will only contain all failed assertions. And will only rerun previously failed assertions next time. This is very useful in the process of developing flow in TDD (Test-Driven Development), when we have to run testcase again and again after each code repair, obviously this case, we only want to run again but assertion failed.

To enable, simply add the attribute cacheResult="true" to <phpunit> in the phpunit.xml configuration file, with the following pattern:

Also remember to add the file name .phpunit.result.cache to .gitignore ! ? To run the test again without using the cache result, add the optional --do-not-cache-result to the command!

Turn off XDebug

If you are turning on X-Debug during the test, this is one of the big reasons why the test speed is very slow. Especially when you have to perform regular tests during development when complying with TDD. Below is an example of running PHPUnit with XDebug.

You can see, my XDebug runtime is up to 12.02 minutes in this case! ?

So the question is, what if you need to use XDebug. The simplest example is when using PHPUnit, if you want to generate code coverage report then you will need to make sure that you have enabled XDebug. Go to the next section, I will reveal! ?

Use PHPDBG instead of XDebug

An alternative, though not perfect solution, is to use PHPDBG instead of XDebug. In particular, in addition to you can generate code coverage report with PHPDBG, the test performance will be significantly improved. The test run time will be reduced from 50% – 70% according to the test I have applied. Below is an example that I have applied, still the above 12.02 minutes testcase when running with PHPDBG will produce the result:

Yes, it is 3.74 minutes. Indeed, when running with PHPDBG, the test runtime has decreased:

T first T 2 T first = 12.02 3.74 12.02 100 = 68.89 % frac {T1 – T2} {T1} = frac {12.02 – 3.74} {12.02} * 100 = 68.89 %

We will save 70% of the test run time when using PHPDBG instead of XDebug; After the trade off, the amount of memory we need to perform will increase significantly. ? Therefore, you should consider using PHPDBG appropriately. In my case, a 70% reduction in runtime is a great option! ?

“Group” of testcase is slow

PHPUnit allows us to group slow test cases in many different files through the @group @group . For example:

When running the test, we can exclude the test slowtests from the above slowtests group via:

Not running slowtests will help run the test faster, but if you are using CI / CD that supports running multiple job at the same time, we can setup CI / CD to split the slowtests section to run separately. Another benefit is that we are documenting test cases that are slow, which also helps us read the code to know which one is slow and will improve it later.

In addition, PHPUnit has support for aliases for groups that you can use briefly as follows:

  • @large <=> @group large
  • @medium <=> @group medium

Fix the testcase is slow

Yes, when the test case in your project is slow, keep in mind that improvements must be made to make them faster. We will look for slow test cases and find out why they are slow and then fix them. In addition to reading the testcase’s documentation and you see the @group @group , you can find them via the PHPUnit D3 Report.

D3 Report will help us visualize the report into graph as shown above, the round of the test means the more time it takes to run. Our job now is to find that big guy to kill ?

References

Share the news now

Source : Viblo