Wednesday, May 27, 2015

Shaking out dependencies in PHPUnit unit tests

Isolation is a unit test ideal. You should, in theory, be able to run them in a random order, or in parallel, with no effect on the results.  PHPUnit provides no options to do this, but, fortunately, two neat GNU command line utilities can help.

Suppose you run your unit tests with phpunit tests

To run them in parallel, shuffling their order, you can switch the command line to:

find tests -type f -name '*Test.php' | shuf | parallel --gnu -L 1 phpunit

Here's what that pipeline does:
  1. Finds all files ending in Test.php (via "find")
  2. Shuffles the order of the files (via "shuf")
  3. Then runs as many in parallel as CPU cores (via "parallel")
Give it a whirl and see how ideal your unit tests are!

0 comments:

Post a Comment

Share your thoughts!