Read my book

I wrote books about Webpack and React. Check them out!

Tuesday, June 2, 2009

TinyTest - test runner for PHP

About a week and a half ago I had one day and nothing particularly useful to do. As I had just spent around a month developing PHP (not my favorite language, I'm more of a Pythonista :) ) extensions for MediaWiki at my current job I had accumulated certain amount of frustration. I figured that my future work would be easier if I had some nice and simple testing tool at my disposal.

There are many testing tools available for PHP already. SimpleTest provides a good example of one. I got used to py.test during my earlier development work (user interface library Scocca, I will post more about this later) and I quite like the simple concept. Essentially py.test does not have an API. This means that all you need to do as a developer is to write tests and test files following given naming conventions and use assert statements inside actual tests. That's about it.

Besides being without API py.test provides a way to run tests in a looping manner so that as you develop your code and tests it runs tests automatically and you get instant feedback on whether or not you have broken something. I find this extremely nice way to develop. Essentially it cuts your testing cycle next to nothing. :)

Another cool feature of py.test is that all you need to do is to invoke it and it finds the tests automatically based on where it was invoked. Of course it's possible to determine in more specific manner which files to run and so on.

The above features gave me a good idea of what my test runner, hopefully aptly named TinyTest, should be about. It took around a day to get the basic functionality done. I also made some tweaks following weekend. First release, 0.26, is available at https://launchpad.net/tinytest/trunk/tinytest0.26 .

So what do TinyTest tests look like at the moment? Basically you have to do two simple things: name your files using test suffix (ie. utils_test.php) and write tests using test prefix (ie. test_sum). If you wish to use classes name them with Test prefix (ie. TestAnimal) and name methods using test prefix as in the case of functions.

The contents of test files may look something like:



So quite simple, eh? Of course I still have to give proper functional testing to the tool yet (also it might be useful to write proper tests for the runner :) ) but I believe it might be useful in small scale development at the moment. Note that the release includes more examples and documentation (-h and --help flags as usual).

On longer term it would be extremely useful to have means to handle mocks, code coverage and perhaps acceptance testing even. On grand scale it would be awesome to have a development environment handling all these aspects in a smooth way (ie. write requirements -> figure out higher level tests -> figure out lower level tests + generate code as possible and fill in technical details).