Lectro Test

LectroTest is an automatic, specification-based testing tool for Perl. It is modeled after the QuickCheck module by Koen Claessen and John Hughes for the Haskell programming language.

Specification-based testing differs from the more common, case-based unit testing in the following way. In case-based testing, you must write individual test cases that together can be used to provide evidence for the correctness (or incorrectness) of your software. In specification-based testing, you instead declare properties that define the correct behavior of your software, and the computer – not you – writes the test cases automatically.

For example, the Test::More module provides support for case-based testing in Perl. Here is how one might approach testing a function that computes the square root of its argument:

  # case-based approach
  use Test::More tests => 4;
  is( sqrt(0), 0, "sqrt(0) == 0" );
  is( sqrt(1), 1, "sqrt(1) == 1" );
  is( sqrt(4), 2, "sqrt(4) == 2" );
  is( sqrt(9), 3, "sqrt(9) == 3" );
For comparison here is a LectroTest property that one could use to test the same function:

  # specification-based approach
  use Test::LectroTest;
  Property {
     ##[ x <- Float(range=>[0,1_000_000]) ]##
     sqrt( $x * $x ) == $x;
  }, name => "sqrt satisfies defn of square root";
The property says, "For all floating-point numbers X in the range 0 through 1,000,000, we assert that sqrt(X*X) equals X." (Here, the floating-point range is used to approximate the set of all non-negative numbers.)

For more information, see http://community.moertel.com/LectroTest


CategoryTesting CategoryPerl


EditText of this page (last edited February 16, 2005) or FindPage with title or text search