Figure out which parts of the function/method you need to think about, and which ones you want to ignore. Then replace the sections you want to ignore with known values. One way to do that is to write a simpler program for each test, which replaces the complex functions with simpler ones for testing purposes.

What I did was write a little module that let me re-define and restore complex sections of code on the fly. For example, if I wanted to test "spiffy_little_function":

sub spiffy_little_function { my ($x,$y) = @_; my @params; # parameters to Foo::new my $o; # an object of class Foo my $z; # computed result before taxes my $t; # computed result after taxes @params=get_huge_list_of_parameters($x); $o = Foo->new(@params); $z=$o->compute_result($y); $t = $z * 0.10; return($t) }

I'd probably do something like this in my test code:

# test spiffy_little_function with options 'foo' and 'bar' <P> # make test run faster by eliminating slow function call redefine_function("main::get_huge_list_of_params", sub {} ); <P> # use minimal constructor redefine_function("Foo::new",sub { return bless {},"Foo" }); <P> # This method should be called with "bar", and return 42, # for the purposes of this test. redefine_function("Foo::compute_result", sub { 42 } ); <P> $result = spiffy_little_function("foo","bar") is($result,4.2,"spiffy_little_function:foo bar test"); <P> restore_all_functions();

If I wanted to, and felt the time was worth it, I could write better tests by checking the input paramters in my stub functions, as well. For example, I could have written:

redefine_function(Foo::compute_result, sub { is($_[0],"bar","spiffy_little_function passed right value to c +ompute_result") return(42); }

and so forth.

This way, I don't have to create a lot of different versions of my program, with different stub modules for each test: I can just redefine the complex bits to be simple, on a temporary basis.

My redefine_function() just manipulates the symbol table entry, and saves the old code reference in a hash: the restore_function() and restore_all_functions restore the original functions again.

--
Ytrew


In reply to Re: Testing with non-trivial input by Ytrew
in thread Testing with non-trivial input by loris

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.