note
Ovid
<p>It looks like you're using [mod://Test::Class]. That module is specifically designed to handle situations like this. In particular, every method is called with an invocant which in this case is a blessed, empty hashref. You're allowed (even encouraged!) to shove things in that hashref. For test classes, I like to call my invocant <tt>$test</tt> instead of the standard <tt>$self</tt>.
<code>
sub Start_timer : Test(startup) {
my $test = shift;
my $start = time();
cprint "#\x037Started at ", (strftime '%A %d %B %Y %H:%M:%S',localtime($start)), "\x030\n";
$test->{start} = $start;
}
sub End_timer : Test(shutdown) {
my $test = shift;
my $start = $test->{start};
my $end = time;
cprint "#\x037Ended at ", (strftime '%A %d %B %Y %H:%M:%S', localtime($end)),"\x030\n";
cprintf "#\x035 Total run time=>", $end-$start, " seconds\n";
cprintf ("%02d:%02d:%02d\x030\n",(gmtime($end-$start))[2,1,0]);
}
</code>
<p>However, this is a practice I discourage. First, it's easy to write something like <tt>$test->{strat};</tt> and wonder why you didn't get a value. Second, encouraging people to reach inside of objects like that is generally a bad idea.</p>
<p>Another solution would be to look at [mod://Test::Class::Moose] and use proper attributes.</p>
<code>
has 'start' => (
is => 'ro',
isa => 'Int',
default => sub { return time },
);
sub test_startup {
my $test = shift;
my $start = $test->start;
cprint "#\x037Started at ", (strftime '%A %d %B %Y %H:%M:%S',localtime($start)), "\x030\n";
}
sub test_shutdown {
my $test = shift;
my $start = $test->start;
my $end = time;
cprint "#\x037Ended at ", (strftime '%A %d %B %Y %H:%M:%S', localtime($end)),"\x030\n";
cprintf "#\x035 Total run time=>", $end-$start, " seconds\n";
cprintf ("%02d:%02d:%02d\x030\n",(gmtime($end-$start))[2,1,0]);
}
</code>
<p>Also, it's a bit out of date, but you might want to read <a href="http://www.modernperlbooks.com/mt/2009/03/organizing-test-suites-with-testclass.html">my Test::Class tutorial</a>. It's in five parts, but it's worth the effort.</p>
<!-- Node text goes above. Div tags should contain sig only -->
<div class="pmsig"><div class="pmsig-17000">
<p>Cheers,<br />
<a href="/index.pl?node=Ovid&lastnode_id=1072">Ovid</a></p>
<p><small><a href="http://www.allaroundtheworld.fr/">Hire me for training or software development</a>.</small></p>
</div></div>
1163428
1163428