the binary canary testing pattern

<img style="cursor:pointer; cursor:hand;" src="http://4.bp.blogspot.com/_Fa_U5q7fBBY/SOKz6Lj7UTI/AAAAAAAAABA/BOQGrVtVFic/s320/canary_coal_mine_0.gif" border="0" alt=""id="BLOGGER_PHOTO_ID_5251957927584682290" />

I think I just invented a new testing pattern - The Binary Canary.

Basically, I was grouping my PHPUnit tests into a test suite and I realized that my TestCase super-classes were "failing" because they had no tests in them. Obviously this is intentional - only the specific sub-classes would have tests.

I guess I could have made the TestCase super-classes abstract, but instead I added this to the highest-level TestCase class:


/*
* global test plumbing here
*/
class Sfx_TestCase extends PHPUnit_Framework_TestCase
{
public function setUp()
{
// more global test plumbing here
}
public function test_Binary_Canary()
{
$this->assertEquals(
"Binary Canary says test plumbing is working.",
"Binary Canary says test plumbing is working."
);
}
}

My little binary canary serves two purposes:

  1. It adds an "always-pass" test to each of my TestCase classes so they don't throw up any more PHPUnit warnings.
  2. Because my TestCase classes set up context-specific test plumbing, the binary canary test inherited by each of them now alerts me if I screw up any of my test plumbing - and tells me the specific area.
  3. </ol>

    For example:


    class Sfx_Db_TestCase extends Sfx_TestCase
    {
    public function setUp()
    {
    parent::setUp();
    // Db-specific test plumbing
    }
    }

    And:


    class Sfx_Controller_TestCase extends Sfx_TestCase
    {
    public function setUp()
    {
    parent::setUp();
    // Controller-specific test plumbing
    }
    }

    Just like the coal-miner canaries of old, this mechanism gives me a simple yes/no signal as to whether or not my test plumbing will soon kill me, and which plumbing code is the culprit.

Question or comment about this post? Tell me on GitHub.

the binary canary testing pattern / groovecoder by groovecoder is licensed under a Creative Commons Attribution-ShareAlike CC BY-SA
the binary canary testing pattern / groovecoder by groovecoder is licensed under a Creative Commons Attribution-ShareAlike CC BY-SA