Slideshow transcript
Slide 1: Intermediate Perl Testing (or, Let’s Not Make A Mess Of Things, Shall We?) Josh Heumann
Slide 2: Have you seen these tests? is_deeply( get_recipes(), [qw/ lemon_curd pancakes enchiladas /], ‘all of the recipes are there’ ); SKIP: { skip ‘No ingredients’, 2, if @ingredients < 1; is( $number, 23, ‘and the number is 23’ ); }; dies_ok( sub { eat_cake }, qr/Cake isn’t chocolate/, ‘only eat chocolate cake’ );
Slide 3: What this talk will do
Slide 4: What this talk won’t do
Slide 5: Why Have Altercations That Encourage Violent Excitable Rants?
Slide 6: Why Have Altercations That Encourage Violent Excitable Rants?
Slide 8: Make them easy to run
Slide 9: Run early, run often
Slide 10: Blackbox testing
Slide 11: Blackbox testing
Slide 12: Blackbox testing
Slide 13: Glassbox testing
Slide 14: Whatever.
Slide 15: Testing scripts #!/usr/bin/perl use strict; use warnings; get_jiggy(); sub get_jiggy { # details, details }
Slide 16: Testing scripts __FILE__
Slide 17: Testing scripts __FILE__ .pl gy g ji t_ ge in/ b
Slide 18: Testing scripts $0
Slide 19: Testing scripts $0 AKA $PROGRAM_NAME
Slide 20: Testing scripts $0 AKA $PROGRAM_NAME pl y. g jig _ et /g in b
Slide 21: Testing scripts $0 AKA $PROGRAM_NAME psl.t . ys ge jggin i ig j_ et t_ es g /bin/ t t
Slide 22: Testing scripts $0 __FILE__ != <> s.tne .pl es ≠ in gy igg g ji j t_ t_ ge es t in/ t/ b
Slide 23: Testing scripts $0 __FILE__ eq == .pl gy .pl g ji gy t_ g ji ge t_ in/ ge b in/ b
Slide 24: Testing scripts #!/usr/bin/perl use strict; use warnings; get_jiggy() if $0 eq __FILE__; sub get_jiggy { # details, details }
Slide 25: Testing scripts #!/usr/bin/perl use strict; use warnings; use Test::More tests => 42; require_ok( ‘bin/get_jiggy.pl’ ); can_ok( ‘main’, ‘get_jiggy’, ‘jigginess achieved’ ); # test the details
Slide 27: Code coverage
Slide 28: Code coverage cover -test
Slide 29: Code coverage cover -test ./Build testcover
Slide 30: Code coverage cover -test ./Build testcover HARNESS_PERL_SWITCHES=-MDevel::Cover make test
Slide 31: Demonstration time!
Slide 32: Version control
Slide 33: Version control
Slide 34: Version control
Slide 35: Testing and version control ?
Slide 36: Testing and version control
Slide 37: Testing and version control
Slide 38: Testing and version control
Slide 39: Testing and version control
Slide 40: Testing and version control
Slide 41: Testing and version control
Slide 42: Testing and version control
Slide 43: Testing and version control
Slide 44: Testing and version control
Slide 45: Testing and version control
Slide 46: Testing and version control
Slide 47: Version control
Slide 49: Reusing test code
Slide 50: Reusing test code for(qw/ is_hilarious is_annoying is_bewildering /) { can_ok( Child, $_ ); is( Child->$_, 1, “...and the child $_” ); }
Slide 51: Reusing test code sub _test_accessor { my ( $method ) = @_; my $getter = \"get_$method\"; my $setter = \"set_$method\"; can_ok( 'Cat', $getter ); can_ok( 'Cat', $setter ); is( Cat->new->$getter, undef, \"...and $method is undef to start with\" ); .... } _test_accessor( ‘sleep’ );
Slide 52: Reusing test code sub _test_accessor { my ( $method ) = @_; my $getter = \"get_$method\"; my $setter = \"set_$method\"; can_ok( 'Cat', $getter ); can_ok( 'Cat', $setter ); is( Cat->new->$getter, undef, \"...and $method is undef to start with\" ); .... } _test_accessor( $_ ) for(qw/ sleep eat purr /);
Slide 53: Whatever.
Slide 54: Custom test libraries
Slide 55: Custom test libraries $ prove t t/00_basics....ok All tests successful. Files=1, Tests=377, 1 wallclock secs ( 0.17 cusr + 0.02 csys = 0.19 CPU)
Slide 56: Custom test libraries package Test::Clowns; use Test::Builder; use Perl6::Junction qw/ any /; my $Test = Test::Builder->new; sub test_clowns { my($clown, $name) = @_; $clown->hop_out_of_a_very_small_car(); $Test->ok( $clown->can_juggle, $name ); $Test->ok( $clown->has_pies, $name ); if( any($clown->faces) eq ‘scary’ ) { $Test->fail( “clowns shouldn’t be scary” ); } $Test->ok( $clown->is_scary, $name ); }
Slide 57: Custom test libraries use TAP::Harness; my $harness = TAP::Harness->new( verbose => 1, lib => [ 'lib', 'blib/lib' ], ); $harness->runtests(@tests); +
Slide 58: Fixtures
Slide 59: our %clients = ( 1001 => { client_id => 1001, dob => '1926-05-25', fname => 'Miles', lname => 'Davis', sex => 'Male', race => 'Black', marital_status => 'Divorced', substance_abuse => 'Yes', alcohol_abuse => 'Yes', gambling_abuse => 'Unknown', religion => 'Muslim', },
Slide 60: 1002 => { client_id => 1002, dob => '1922-04-21', fname => 'Charles', lname => 'Mingus', sex => 'Male', race => 'Other', marital_status => 'Married', substance_abuse => 'No', alcohol_abuse => 'Yes', gambling_abuse => 'Unknown', religion => 'Buddhist', }, # etc, # etc, # etc. );
Slide 61: for my $client_id (keys %clients) { Test::Client->new( id => $clients{ $client_id } )->save; }
Slide 62: for my $client_id (keys %clients) { Test::Client->new( id => $clients{ $client_id } )->save; } Test::Client->new(1001)->save;
Slide 63: sub create_test_client { Test::Client->new( %args )->save; } create_test_client( id => 2, dob => undef, );
Slide 64: sub create_test_client { $args{ dob } ||= ‘1970-01-01’; Test::Client->new( %args )->save; } create_test_client( id => 2, dob => undef, );
Slide 65: sub create_test_client { $args{ dob } ||= ‘1970-01-01’; $args{ shrink } ||= create_test_shrink; Test::Client->new( %args )->save; } create_test_client( id => 2, dob => undef, );
Slide 67: Mocking
Slide 68: Mocking
Slide 69: Mocking Test::MockObject
Slide 70: Mocking Test::MockObject Test::MockClass
Slide 71: Mocking Test::MockObject Test::MockClass Sub::Override
Slide 72: Mocking Test::MockDBI Test::Mock::LWP Test::Mock::HTTP::Request Test::Mock::HTTP::Response
Slide 74: Selenium
Slide 75: How to win friends and convince others to test
Slide 76: Speeding up tests
Slide 77: Where to start?
Slide 78: Review
Slide 79: Review Make your tests easy to run
Slide 80: Review Make your tests easy to run Run them often
Slide 81: Review Make your tests easy to run Run them often Test your modules
Slide 82: Review Make your tests easy to run Run them often Test your modules Test your scripts
Slide 83: Review Make your tests easy to run Run them often Test your modules Test your scripts Improve your code coverage with Devel::Cover
Slide 84: Review Make your tests easy to run Run them often Test your modules Test your scripts Improve your code coverage with Devel::Cover Use version control, and check in often
Slide 85: Review Make your tests easy to run Run them often Test your modules Test your scripts Improve your code coverage with Devel::Cover Use version control, and check in often Reuse your test code
Slide 86: Review
Slide 87: Review Make a test library
Slide 88: Review Make a test library Use fixtures
Slide 89: Review Make a test library Use fixtures Mock when necessary
Slide 90: Review Make a test library Use fixtures Mock when necessary Test quietly if you don’t have support
Slide 91: Review Make a test library Use fixtures Mock when necessary Test quietly if you don’t have support Start with the most-changed part of the code
Slide 92: Questions?
Slide 93: Thanks! • Michael Schwern • Kirrily Robert • OSDC • realestate.com.au • you
Slide 94: Josh Heumann josh@joshheumann.com



Add a comment on Slide 1
If you have a SlideShare account, login to comment; else you can comment as a guest- Favorites & Groups
Showing 1-50 of 2 (more)