Generic Unit Tests

heusserm on 2003-12-04T16:35:47

Someone on JoelonSoftware wanted to figure out how to do automated unit tests in TCL, so I wrote up this post:

Mike Scwern wrote test::More for Perl as well as test::Tutorial.

http://magnonel.guild.net/user/schwern/talks/Test_Tutorial/Test-Tutorial.pdf

Basically, I'd suggest making a simple library with functions like this:

sub tests(int); sub eq(string, int, int); sub eq(string, bool, bool); sub eq(string, double, double); sub eq(string, string, string); sub ok(string, bool);

The psuedo code looks something like this:

GLOBAL iTestNum int; sub tests(i int) { printf("1..%d\n", i); iTestNum = 1; }

sub ok(s string, b bool) { if (b) { print("ok %d - %s\n", iTestNum, s); } else { print("not ok %d - %s\n", iTestNum, s); } iTestNum++; }

sub eq(s string, i int, i2 int) { if (i==i2) { print("ok %d - %s\n", iTestNum, s); } else { print("not ok %d - %s\nGot %d\nExpected %d\n", iTestNum, s, i, i2); } iTestNum++; }

//---End Psuedo Code

So your output to STDIO looks like this:

1..5 ok 1 - Foo with valid input ok 2 - Foo with invalid group number ok 3 - Error Message for invalid group number not ok 4 - Foo with invalid date not ok 5 - Error Message for invalid date Expected - "Date 10/35/2003 is not a valid date" Got - ""

Obviously, your function is something like:

func foo(iGroupNum int, sDate string) returns BOOL;

--All this is pseduo code you can write up in any langauge.

If you write it correctly, you can pump it through Test::Harness or Prove automatically.

That's the 5 minute version, anyway. You can write test harnesses in any language. (If you die after test 3 because of an unhandled exception, you know you SHOULD have had 5 tests because of the 1..5, so that too is a test.)

Thoughts? I think there's some interesting potential stuff about a "Generic Unit Test Framework", but it needs more work ...


Yay!

petdance on 2003-12-04T19:36:06

Cross-language testing is one of my explicit goals. As long as that format matches the spec, there shouldn't be any reason prove can't be run on it.

Re:Yay!

Bernhard on 2003-12-04T20:09:17

This is yet another of these "But it's already there!" comments. I checked http://www.xprogramming.com/software.htm and found TclTkUnit, http://park.ruru.ne.jp/ando/work/tclTkUnit/