Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Test::LectroTest and Test::MockObject.

by pernod (Chaplain)
on Sep 14, 2004 at 18:48 UTC ( [id://390942]=note: print w/replies, xml ) Need Help??


in reply to RFC: Tutorial on Testing

Great tutorial, and brilliant work! I completed your tutorial and read the rest of the documentation before I went beyond your celebrational espresso. The following is a condensed and simplified example of how I put your module to use in combination with chromatic's excellent Test::MockObject to test one of my own modules. Note that the enclosed snippet is untested, as i stripped away quite a bit of detail that I thought complicated the example.

In the web app I work on at $firm, we channel all database access through a single component. The module I want to test queries the database and ranks entities according to certain criteria, using a simple formula. What I want to test here is the method that does the ranking.

#! /usr/bin/perl use strict; use Test::LectroTest; use Test::MockObject; use List::Util qw( reduce ); use Score; BEGIN { # Faking the database component Test::MockObject->fake_module( 'My::Database' ); } # Making a mockobject of the database my $mock = Test::MockObject->new(); $mock->fake_new( 'My::Database' ); my $scorer = Score->new(); # The Score module expects the database results as hashes. # This closure generator allows me to mock the database # access method. sub hash_sequence { my @sequence = @_; my $index = 0; return sub { return %{ $sequence[ $index++ ] }; } } Property { ##[ input <- List( Int( range=>[50,100], sized=>1), length=>5 ) ]## # Calculate some 'constants' my $questions = reduce { $a + $b } @{ $input }; # Sum the result set my @data; for( my $i = 0; $i < 5; $i++ ) { # Feed the mocker some mock push( @data, { 1 => $input->[ $i ] } ); } # Setup mockobject $mock->mock( 'getHash', &hash_sequence( @data ) ); $scorer->setDatabase( $mock ); # Calculate expected result my $raw = ( ( $input->[ 2 ] + ( 2 * $input->[ 3 ] ) - $input->[ 0 ] ) + $questions ) / ( 3 * $questions ); my %result = $scorer->score(); # The method to test $raw == $result{ 1 }; }, name => "score holds.";

I should perhaps be a bit sceptical that this test passes. I haven't twiddled and played much with the generators yet, so raising my confidence in the module demands some more playing around with numbers.

I may of course have abused both Test::LectroTest and Test::MockObject. Abusing Test::LectroTest in that I actually calculate the expected score in the Property itself. This is of course error-prone, in I might introduce errors in the 'verification' code. When it comes to Test::MockObject, I must admit admit that I lack the experience to see the flaws in my logic. I'm sure some wiser monk will find something, though :)

When it comes to answering your question about oversights in the tutorial, then I agree with stvn that you should give the generators more focus. Especially some help on how to limit ranges, and combining generators into complex structures. I found the answers I needed in the the Test::LectroTest::Generator manual, though, so it worked great as a quick start.

I wondered where the name came from too, but after I found:

The result is LectroTest, a horribly named, automatic, specification-based testing tool for Perl.

on your homepage, I thought it better not to ask ;)

All in all, I believe Test::LectroTest will become a valuable tool at work. Thank you very much :)

pernod
--
Mischief. Mayhem. Soap.

Replies are listed 'Best First'.
Re: Test::LectroTest and Test::MockObject.
by tmoertel (Chaplain) on Sep 14, 2004 at 20:26 UTC
    Thanks for your feedback on LectroTest, and your example with Test::MockObject is a cool use. (BTW, I don't think that calculating the expected score in the Property is an abuse at all.)

    If you don't mind a suggestion, you could simplify the data-building portion of your code by letting LectroTest build the more-complicated data structure for you:

    #!/usr/bin/perl use strict; use Data::Dumper; use Test::LectroTest trials => 10; use List::Util qw( reduce ); $Data::Dumper::Terse = 1; Property { ##[ data <- List( Hash( Unit(1) , Int( range=>[50,100], sized=>0 ) , length=>1 ) , length=>5 ) ]## my @input = map { values %$_ } @$data; my $questions = reduce { $a + $b } @input; print STDERR Dumper( { data => $data , input => \@input , questions => $questions } ), "\n"; 1; # always passes trials }, name => "shell property that holds a generator example";
    Also, for the Int generator, you shouldn't specify a range that does not contain zero if the generator is sized. The reason for this rule is that at run time, the intersection of the sizing-guidance range and your given range can be empty, thus making it impossible to generate a valid value. (I have updated the LectroTest::Generator docs to reflect this, and LectroTest will now complain if you try to combine these settings.)

    Thanks again for taking the time to provide feedback!

    Cheers,
    Tom

      Thank you! I will incorporate your suggestions, as they are both more concise and more elegant, not to say easier to read. Perhaps a cookbook section with examples on different standard structures might be an idea?

      Anyhow, this is great input for future fiddling with the settings. I'll study the docs for sizing-guidance, as I haven't quite figured those out yet.

      pernod
      --
      Mischief. Mayhem. Soap.

        Wow! A subject near and dear to my heart. I look forward to working my way thru this pup - specially since I am a QA automation guy. I applaud your effort and will provide my two cents worth shortly. spiderman

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://390942]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2024-04-19 12:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found