• Like

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.

Test builder

  • 748 views
Uploaded on

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.

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
748
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
2
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide
















Transcript

  • 1. Test::Builder Writing your own test module? by jonasbn for Nordic Perl Workshop 2008 1
  • 2. Test::* modules • Test::Harness • Test::More • Test::Benchmark • Test::Pod 2
  • 3. Problem! • Slow back-end • XML • Over HTTP • Tomcat/Java implementation 3
  • 4. Problem! 3
  • 5. Solution! 4
  • 6. Solution! • So what if we get the response times defined as requirements and we can stick the timings into our test suite! 4
  • 7. *.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
  • 8. *.t 5
  • 9. *.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
  • 10. Problem! • That is a lot of code, I need sugar - syntactic sugar! 6
  • 11. Problem! 6
  • 12. Solution! • So what if we build our own Test::* module • Oh, Test::Builder lets us do exactly that • thanks chromatic! 7
  • 13. Test::Timer 8
  • 14. 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
  • 15. *.t 9
  • 16. *.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
  • 17. 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
  • 18. Problem! 10
  • 19. Solution! • Test::Tester? • Oh, Test::Tester let’s us do exactly that • Thanks Fergal! 11
  • 20. Test::Tester 12
  • 21. 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
  • 22. For free and automagical • Test::Tester gives you tests automagically and for free (6 per test statement). 13
  • 23. 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
  • 24. 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