About 2 years ago, we’ve begin to add some unit tests in the GLPI project; thanks to Valentin and Remi Collet. The PHPUnit framework has been used, since this is the most “common” choice for a PHP project.
Unfortunately, many parts of the code are not tested yet… This is a huge job; but we’re working on it. Nowadays, each new feature implemented in GLPI is unit tested; and we plan to spend time this summer to work on all of this.
Why now?
I personally appreciate atoum since its very beginning, and I’ve proposed to switch a while ago. Since we plan to add more and more tests; switching while there are still a few tests was easier. That’s why it’s been done now.
So, why to switch?
Well… Speaking of PHP unit testing frameworks, there are not so many possible choices. PHPunit is probably the oldest unit testing framework available for PHP; and it does the job, really. atoum is a more recent and modern choice, and it has some interesting capabilities:
- testing variables types (if you test an integer and get a string, this is not correct),
- wonderful mocking system (that permit to mock php native functions, constants, …),
- use closure to test outputs, exceptions, … (way more interesting than phpunit’s annotations),
- concurrent run of tests (even if it have been disabled for GLPI),
- fully isolated tests,
- chained calls,
- more natural way to write tests (this is maybe just my point of view – but this is really more simple to me).
Also, GLPI is a French project, most core team members are French, just as atoum! 😉
On a technical note
Switching from PHPUnit to atoum is not something very difficult, especially if you did not yet use advanced features (which is the case for GLPI); but it is not really something that can be scripted. In facts, it could for some common cases, but reviewing tests is not something bad globally. Also, re-writing tests is a very good start point to learn new syntax, asserters and possibilities.
For the GLPI project, we’ve spent two days working on the rewrite. What we had to take care of:
- atoum is not permissive at all. Every notice in the code will make tests fail per default (which is a good point); we had to fix them;
- we’re doing kind of functional tests and not only unit tests. Some existing tests used to change existing values in the database, before resetting them. That cannot be done running tests concurrently;
- atoum does not support dependencies across tests; which phpunit does. It should seem a bad point, but the point is “tests are isolated”. So… They cannot be dependent, we had to find another way;
- session usage: GLPI uses many data in the session; and that is really a problem for testing… We had to disable concurrent runs mainly because of that :’(
The future
In a close future, we plan to improve our unit tests; since many parts of the code are not tested at all. We also plan to split our unique directory into several ones, making possible to disable concurrent runs only on functional tests, not on the whole tests suite.
Johan Cwiklinski.