Wednesday, June 29, 2011

PHPUnit and Working Directories

I'm using the PHPUnit framework for unit testing some code I'm writing for a couple of websites. To avoid figuring out the seemingly convoluted API for building a suite of tests, I had to rearrange my directory structure. All of my test classes are in a Tests directory that is a child of the directory where the working code is located. I also had to split out each test class into its own separate file (similar to Java).

In moving the tests to a subdirectory, I had to change my require_once statements to point to the parent directory via a relative path. At least, I thought I did.

It turns out that require_once operates on the current working directory. I was using the following command:
  C:\Source\> phpunit Tests
The current working directory was C:\Source, so when the require_once attempted to resolve the path, it was looking in C:\.

There are two solutions:
  1. Remove the relative path markers
  2. Run phpunit from within the Tests directory

I chose option 2.