phpunit.el: A PHPUnit interface for Emacs

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.

Usage

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.

Customizing

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.

Minor Mode Bindings

Code

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.

2008/12/23

Discussion

[...] Emacs [...]

PHP开发环境的搭建流程 « 博耘生物
2009/06/04

Participate