Here at Digg, we’re getting unit testing religion. We had some older code which had PHPT coverage, but we’ve moved to PHPUnit as our test platform.
I’ve been writing a lot of unit tests lately. In fact, I’ve written more test code than non-test code. So I’ve natually been running phpunit fifty times a day, to the point where I started wishing I had a better way to test. There wasn’t, so I had no choice but to write one.
Enter phpunit.el.
Simple use is, well, simple: M-x phpunit-run-test RET. This brings up a command line in the miniuffer for you to enter the command. You can re-run the test with phpunit-run-test-or-retest, or by pressing R in the *phpunit* buffer.
It’s derived from compilation-mode, so C-x ` will jump you to the first error listed in the output.
There are a few options to customize, like the path to the PHPUnit binary, the default arguments, and such. Poke around with M-x customize-group RET phpunit RET.
More advanced features are enabled by customizing phpunit-testable-list, which tells phpunit.el how to map a source file to a test file. It’s format is:
( (SOURCE-REGEX TEST-CLASS TEST-FILE) (SOURCE-REGEX TEST-CLASS TEST-FILE) … )
Which is to say, if a source file matches SOURCE-REGEX, the name of the unit test class is TEST-CLASS and the path to the file it exists in is TEST-FILE.
Some features and functions depend on this, notably phpunit-test-this and phpunit-find-test-file.
If you open a PHP file and a corresponding test file is found, the phpunit-minor-mode is enabled, as indicated by “Test” in the modeline. This turns on a few bindings to help you run your tests.
C-c C-l r (phpunit-run-test-or-retest) – Run a test, or re-run the last test.C-c C-l f (phpunit-find-test-file) – Open the unit test corresponding to the currently viewed source.C-c C-l t (phpunit-test-this) – Run the test associated with this source.The code is under the GPL3, and it’s available at GitHub. It’s still a work in progress. Feel free to drop me a line if you have problems or patches.
Discussion