Loading…

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

Like this presentation? Why not share!

Test builder

on

  • 1,020 views

Introduction to the Perl module Test::Builder. It was used to implement Test::Timer which is also available on CPAN.

Introduction to the Perl module Test::Builder. It was used to implement Test::Timer which is also available on CPAN.

Statistics

Views

Total Views
1,020
Views on SlideShare
1,019
Embed Views
1

Actions

Likes
0
Downloads
2
Comments
0

1 Embed 1

http://www.linkedin.com 1

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

CC Attribution-ShareAlike LicenseCC Attribution-ShareAlike License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />

Test builder Test builder Presentation Transcript

  • Test::Builder Writing your own test module? by jonasbn for Nordic Perl Workshop 2008 1
  • Test::* modules • Test::Harness • Test::More • Test::Benchmark • Test::Pod 2
  • Problem! • Slow back-end • XML • Over HTTP • Tomcat/Java implementation 3
  • Problem! 3
  • Solution! 4
  • Solution! • So what if we get the response times defined as requirements and we can stick the timings into our test suite! 4
  • *.t use Test::More qw(no_plan); use Benchmark; $t0 = new Benchmark; postXML2slowJavaBackendOverHTTP_today_plz(); $t1 = new Benchmark; $td = timediff($t1, $t0); my ($time) = timestr($td) =~ m/^(d+)/; ok($time < 30, ‘We barely made it!’) 5
  • *.t 5
  • *.t use Test::More qw(no_plan); use Benchmark; $t0 = new Benchmark; postXML2slowJavaBackendOverHTTP_today_plz(); $t1 = new Benchmark; $td = timediff($t1, $t0); my ($time) = timestr($td) =~ m/^(d+)/; ok($time < 30, ‘We barely made it!’) 5
  • Problem! • That is a lot of code, I need sugar - syntactic sugar! 6
  • Problem! 6
  • Solution! • So what if we build our own Test::* module • Oh, Test::Builder lets us do exactly that • thanks chromatic! 7
  • Test::Timer 8
  • Test::Timer my $test = Test::Builder->new; sub time_atmost { my ( $code, $upperthreshold, $name ) = @_; my $ok = _runtest( $code, 0, $upperthreshold, $name ); if ($ok == 1) { $test->ok( $ok, $name ); } else { $test->ok( $ok, $name ); $test->diag( 'Test exceeded specified threshold' ); } return $ok; } 8
  • *.t 9
  • *.t use Test::Timer; time_ok( sub { doYourStuffButBeQuickAboutIt(); }, 1, 'threshold of one second'); time_atmost( sub { doYourStuffYouHave10Seconds(); }, 10, 'threshold of 10 seconds'); time_between( sub { doYourStuffYouHave5-10Seconds(); }, 5, 10, 'lower threshold of 5 seconds and upper threshold of 10 seconds'); time_nok( sub { sleep(2); }, 1, 'threshold of one second'); time_atleast( sub { sleep(2); }, 2, 'threshold of one second'); #Will fail after 5 (threshold) + 2 seconds (default alarm) time_ok( sub { while(1) { sleep(1); } }, 5, 'threshold of one second'); $test::Timer::alarm = 6 #default #Will fail after 5 (threshold) + 6 seconds (specified alarm) time_ok( sub { while(1) { sleep(1); } }, 5, 'threshold of one second'); 9
  • Problem! • It looks like it is working! • It feels like it is working! • But would it not be better if we test it, so we can be really sure? 10
  • Problem! 10
  • Solution! • Test::Tester? • Oh, Test::Tester let’s us do exactly that • Thanks Fergal! 11
  • Test::Tester 12
  • Test::Tester check_test( sub { time_atleast( sub { sleep(1); }, 3, 'Failing test' ); }, { ok => 0, name => 'Failing test', depth => 1 }, 'Failing test of time_atleast' ); check_test( sub { time_atleast( sub { sleep(3); }, 1, 'Passing test' ); }, { ok => 1, name => 'Passing test', depth => 1 }, 'Passing test of time_atleast' ); 12
  • For free and automagical • Test::Tester gives you tests automagically and for free (6 per test statement). 13
  • So roll your 0wn! with lots of syntactic sugar added! • Readable code • Maintainable code • A uniform test suite • Domain specific tests, like Test::XML • and it is actually fun and it has a lazy feel to it! 14
  • Thanks! jonasbn@logicLAB.dk Test::Timer is available on CPAN Slides will be available on the NPW2008 website Article(s) might be coming in Perl Review, if I can open the editor and get passed the human editor 15