Automated Sprinkle unit tests (phpUnit 7+)


#1

Hello,

I am looking to start writing tests for my sprinkle’s controllers, and I need some advice to set up an optimal workflow. I would like to use PHPUnit 7+ (I am on PHP 7.1 currently, moving to 7.2 soon).

I see that UF4 already uses an older version of phpunit 5.x, though I’ve not used/executed any of UF’s tests.

Questions:

  1. Where are UF’s tests and how do I execute them?

  2. Can I use my own PHPUnit setup for my own sprinkle, or do I have to update UF’s core phpunit requirement?

  3. Is there a recommended workflow to auto-execute the tests (locally) after an update? Either manually executing a script, or by having an automated script that tests on file changes (my favourite method).

Any other tips on this appreciated, thanks!


#2
  1. Tests are in app/sprinkles/{sprinkle}/tests/ and run using the php bakery test command.

  2. UF use an old PHPUnit version because of PHP 5.6 support. You can still write your own tests for your sprinkle and use PHPUnit 5.x. I’ve not yet encountered something that absolutely required PHPUnit 7.x

  3. It depends. On MacOS I did it once using an app that ran a command when a file is updated, but I spend more time getting it to work than running the tests manually. Obviously, I recommend setting up Travis CI on your repo for automated testing when you push to git. I use a while true loop to run tests when I press enter :

while true; do clear && printf '\e[3J'; php bakery test; read -p "Press [Enter] key to continue or ctrl+c to quit..."; done

Note tests are improved in the upcoming UF 4.2, including an option to run a only a Sprinkle tests (and not all the tests) and better support for controller tests. 4.2-beta1 should be out soon.

FYI, I’m planning to add a whole testing chapter in the doc once 4.2 is out :wink:


#3

Great news about v4.2!

Is it not possible to use PHPUnit 7 in my Sprinkle, separately? And execute the sprinkle tests separately from the UF tests?


#4

Not if you use Composer to load PHPUnit. You could do it using a globally installed version of PPHUnit 7.

To run only your sprinkle tests, you can look at my Travis setup for example :