Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

strengthening a fragile test

by hesco (Deacon)
on Feb 12, 2011 at 03:34 UTC ( [id://887724] : perlquestion . print w/replies, xml ) Need Help??

hesco has asked for the wisdom of the Perl Monks concerning the following question:

I published another module the other day (WWW-Scrape-Mailman-RSS-0.12) and am now responding to the feedback I'm getting from the smoke testers. Fixing the first round of issues (attempts to create a file at a path which existed only in my local environment) was easy. But I can see that the code below is about to blow up in my face.

my %args = ( 'info_url' => 'http://ga.greens.org/mailman/listinfo/gpga-news', 'base_url' => 'http://ga.greens.org/pipermail/gpga-news', 'list_name' => 'gpga-news', 'audience' => 'Greens', 'description' => 'News by, about and for Greens', 'cycles' => 2, 'output_file' => 't/tmp/gpga_news_feed.html', 'rss_output' => 't/tmp/gpga_news_feed.rss', 'template' => 't/tmpl/gpga_news_feed.tmpl', ); my $news_feed = $feed->render_feed(\%args); # this is fragile and will break in two weeks my $latest_month = '2011-February'; my $next_latest_month = '2011-January'; like($news_feed,qr/$latest_month/,'Feed includes latest month'); like($news_feed,qr/$next_latest_month/,'it also includes the second mo +st recent month');
In fact, even if I could generate the $latest_month and $next_latest_month programmatically, this test would still fail from midnight the morning of the 1st of each month until the first post was archived to the test list each month.

Can someone please advise how I might make this test less fragile, please? Mocking it seems not quite right, because part of what I need to test is my ability to give this object an $args{'base_url'} and get back an rss feed.

Thanks,
-- Hugh

if( $lal && $lol ) { $life++; }
if( $insurance->rationing() ) { $people->die(); }

Replies are listed 'Best First'.
Re: strengthening a fragile test
by Corion (Patriarch) on Feb 12, 2011 at 09:10 UTC

    I'm not sure whether your module has to create things with "today's date", but if not, why not ship canned HTML files with a fixed date, and use these in your tests?

Re: strengthening a fragile test
by Anonymous Monk on Feb 12, 2011 at 06:19 UTC
    Hi,

    There are any number of date modules you can use to sort out the needed values. I prefer Date::Calc myself, but others have different views.

    I don't see why the code should fall over if you calculate the months just before you use them. That's what I usually do.

    J.C.
Re: strengthening a fragile test
by budman (Sexton) on Feb 13, 2011 at 08:24 UTC
    What about sampling dates and then checking to see if the requested date is available.
    use strict; use Date::Calc qw(Today Add_Delta_YM Month_to_Text); my @today=Today(); my $months_back; my @fetchDates; while ( @fetchDates < 2 ) { my @calcDate = Add_Delta_YM(@today[0,1],1,0,$months_back); my $date_str = $calcDate[0] .'-'. Month_to_Text($calcDate[1]); if ( &dateAvailable($date_str) ) { push @fetchDates, $date_str } $months_back--; } print "Retrieving dates:\n".join("\n",@fetchDates)."\n"; sub dateAvailable { my $date = shift; # check site of date can be downloaded return int(rand(2)); }
    I used rand just to show how it would work
    2011-February
    2011-January
    
    2011-February
    2010-December
    
    2010-December
    2010-September