Test::...torial(3pm)
π Test::LectroTest::Tutorial(3pm) π
okay bestie, so this is a page from the User Contributed Perl Documentation collection, and it's literally your girl's guide to Test::LectroTest::Tutorial β aka how to use LectroTest to test your software. pull up a chair, grab a boba, we're doing this together π§β¨
πΈ VERSION πΈ
version 0.5001 β yes she's been around a minute, but she's iconic and she still slays π
β¨ SYNOPSIS β¨
LectroTest is an automated, specification-based testing system. To use it, you declare properties that specify the expected behavior of your software. Then you invoke LectroTest to test whether those properties hold. no cap, that's the whole vibe.
LectroTest does this by running repeated random trials against your software. If LectroTest finds that a property doesn't hold, it emits the counterexample that "broke" your software. You can then plug the counterexample into your software to debug the problem. (It's also a good idea to add the counterexample to your list of regression tests.) π«Ά
π¦ OVERVIEW π¦
Think of your software's behavior as a haystack that you're searching for needles. Each error is a needle. You want to find the needles and remove of them. LectroTest will search the haystack for you β it's nice that way β but first you must tell it about the shape of the haystack and how to recognize a needle when it sees one. it's giving very "give me the map and I'll find the treasure" π₯
πΎ The Haystack πΎ
The shape of the haystack is defined by a set of "generator bindings," in which variables are bound to the output of value generators:
x <- Int, c <- Char( charset=>"A-Z" )
The above can be read, "For all integers x and all characters c in the range A through Z." The idea is that each unique instance of the pair (x, c) specifies a point in the haystack that we can search for needles. every point in the haystack is a whole vibe check, lowkey π
πͺ‘ The Needle Recognizer πͺ‘
The "needle recognizer" is defined by a snippet of code that uses the bound variables to inspect a given point in the haystack. It returns a "thumbs up" (true) if the point is free of needles or a "thumbs down" (false) if it finds a needle:
the_thing_we_are_testing($x, $c) >= 0;
The above asserts for each point in the haystack that the output of the function the_thing_we_are_testing must be non-negative. periodt π―
π Put them together to make a Property π
The generator bindings and needle recognizer are combined to make a property:
Property {
##[ x <- Int, c <- Char( charset=>"A-Z" ) ]##
the_thing_we_are_testing($x, $c) >= 0;
}, name => "the_thing_we_are_testing(...) is non-negative";
You'll note that we also added a meaningful name. Although not strictly required, it's an excellent practice that makes life easier. (You'll also note that we placed the generator bindings inside of the magic delimiters ##[ ]##. This tells Perl that our bindings are bindings and not regular Perl code.) main character energy fr fr π
We can read the above property like so: "For all integers x and all characters c in the range A through Z, we assert that the_thing_we_are_testing is non-negative." π
π Testing whether your Properties hold π
After you define properties for your software, just add them to a small Perl program that uses the Test::LectroTest module:
# MyProperties.l.t
use MyModule; # provides the_thing_we_are_testing
use Test::LectroTest;
Property {
##[ x <- Int, c <- Char( charset=>"A-Z" ) ]##
the_thing_we_are_testing($x, $c) >= 0;
}, name => "the_thing_we_are_testing(...) is non-negative";
Then you can test your properties simply by running the program:
$ perl MyProperties.l.t
If your properties check out, you'll see something like this:
1..1
ok 1 - 'the_thing_we_are_testing(...) is non-negative' (1000 attempts)
If something goes wrong, however, LectroTest will tell you where it happened:
1..1
not ok 1 - 'the_thing_we_are_testing(...) is non-negative' \
falsified in 23 attempts
# Counterexample:
# $x = 4
# $c = "R"
What this says is that at the point (x=4, c="R") in the haystack, there is a needle (i.e., your property doesn't hold). With this information, you can examine your code to determine the cause of the error. it literally hands you the receipt ππ§Ύ
π₯ LET'S DO IT! π₯
Now that we have big-picture understanding of "LectroTesting," let's try a few examples together. bestie study session incoming πβ¨
[TODO: write the step-by-step tutorial examples. For now, take a look at the slides from my LectroTest talk for two such examples. The slides are available at the LectroTest Home.]
(yeah she left us a lil note in the drafts, we love an unfinished chapter π but the vibes are still immaculate)
π SEE ALSO π
Test::LectroTestgives a quick overview of automatic, specification-based testing with LectroTest.Test::LectroTest::Propertyexplains in detail what you can put inside of your property specifications.Test::LectroTest::Generatordescribes the many generators and generator combinators that you can use to define the shapes of the haystacks you encounter during your testing adventures. πΎTest::LectroTest::TestRunnerdescribes the objects that check your properties and tells you how to turn their control knobs. You'll want to look here if you're interested in customizing the testing procedure. ποΈ
π«Ά AUTHOR π«Ά
Tom Moertel (tom@moertel.com)
π INSPIRATION π
The LectroTest project was inspired by Haskell's QuickCheck module by Koen Claessen and John Hughes:
http://www.cs.chalmers.se/~rjmh/QuickCheck/
π COPYRIGHT and LICENSE π
Copyright (c) 2004-13 by Thomas G Moertel. All rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
and that's the whole page, bestie!! π this tutorial came to you via perl v5.34.0, dated 2022-06-21 β so if you ever need to cite your source in a group chat, you know exactly what to say π mwah, go write some properties, you've got this π β¨
π¦ where this page comes from
- debian bookworm: libtest-lectrotest-perl 0.5001-5 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libtest-lectrotest-perl 0.5001-5 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libtest-lectrotest-perl 0.5001-5 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libtest-lectrotest-perl 0.5001-5 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libtest-lectrotest-perl 0.5001-5 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms