Testing Doesn't Require a Test Framework


Fri Mar 20 13:32:43 -0700 2009

You can practice TDD/BDD anywhere, whether or not you have a test framework available to you. For example, here’s a fragment of test.c from an Nginx module I wrote a few years ago:

#define fail(msg)     { puts(msg); return 1; }

int main()
   if (in_heroku_domain("abc.example.com") != 0)
      fail("in_heroku_domain should return false for abc.example.com");

   if (in_heroku_domain("abc.heroku.com") != 1)
      fail("in_heroku_domain should return true for abc.heroku.com");

   puts("All tests passed.");
   return 0;

Build and run this, and it prints out the test successes or failure, and returns a failure code in the latter case. The makefile was set to prevent you from building and installing the main module unless all the tests passed. Later, when I needed some database fixtures, I wrote a short shell script that created a test database and loaded it with some very simple data via direct SQL statements.

Another example is my Lua pong game:

function describe(class, func)
function it(descr, func)
  if (func()) then
    print("- ", descr)
    print("[fail] ", descr)
function all_tests()
  describe("Rect", function()
    r1 = Rect:new{x = 0, y = 0, w = 3, h = 1}
    r2 = Rect:new{x = 1, y = 0, w = 2, h = 2}
    it("intersects two rectangles", function()
      return r1:intersects(r2) == true

It doesn’t take much to test code. Just a program you can run that puts the code through its paces without requiring any user input. A good framework is nice to have, but don’t get hung up on it - you can write automates tests anywhere, framework or not. TATFT.