Intermediate Perl Testing
(or, Let’s Not Make A Mess Of Things, Shall We?)




                 Josh Heumann
Have you seen these tests?
is_deeply(
    get_recipes(),
    [qw/ lemon_curd pancakes enchiladas /],
    ‘all of the recip...
What this talk will do
What this talk won’t do
Why
Have
Altercations
That
Encourage
Violent
Excitable
Rants?
Why
Have
Altercations
That
Encourage
Violent
Excitable
Rants?
Make them easy to run
Run early, run often
Blackbox testing
Blackbox testing
Blackbox testing
Glassbox testing
Whatever.
Testing scripts

#!/usr/bin/perl

use strict;
use warnings;

get_jiggy();

sub get_jiggy {
    # details, details
}
Testing scripts
__FILE__
Testing scripts
__FILE__


                 .pl
              gy
            g
          ji
       t_
     ge
 in/
b
Testing scripts
$0
Testing scripts
$0
AKA $PROGRAM_NAME
Testing scripts
$0
AKA $PROGRAM_NAME


                         pl
                      y.
                    g
        ...
Testing scripts
$0
AKA $PROGRAM_NAME


                        psl.t
                      .
                     ys
     ...
Testing scripts
     $0                    __FILE__


                      !=
                      <>
                  ...
Testing scripts
    $0                 __FILE__
                  eq
                  ==


                 .pl
         ...
Testing scripts
#!/usr/bin/perl

use strict;
use warnings;

get_jiggy() if $0 eq __FILE__;


sub get_jiggy {
    # details...
Testing scripts
#!/usr/bin/perl

use strict;
use warnings;
use Test::More tests => 42;

require_ok( ‘bin/get_jiggy.pl’ );
...
Code coverage
Code coverage

cover -test
Code coverage

cover -test

./Build testcover
Code coverage

cover -test

./Build testcover

HARNESS_PERL_SWITCHES=-MDevel::Cover make test
Demonstration time!
Version control
Version control
Version control
Testing and version control




        ?
Testing and version control
Testing and version control
Testing and version control
Testing and version control
Testing and version control
Testing and version control
Testing and version control
Testing and version control
Testing and version control
Testing and version control
Testing and version control
Version control
Reusing test code
Reusing test code

for(qw/ is_hilarious is_annoying is_bewildering /) {
    can_ok( Child, $_ );
    is( Child->$_, 1, “.....
Reusing test code
sub _test_accessor {
    my ( $method ) = @_;

    my $getter = quot;get_$methodquot;;
    my $setter = ...
Reusing test code
sub _test_accessor {
    my ( $method ) = @_;

    my $getter = quot;get_$methodquot;;
    my $setter = ...
Whatever.
Custom test libraries
Custom test libraries
$ prove t
t/00_basics....ok
All tests successful.
Files=1, Tests=377, 1 wallclock secs ( 0.17 cusr +...
Custom test libraries
package Test::Clowns;
use Test::Builder;
use Perl6::Junction qw/ any /;

my $Test = Test::Builder->n...
Custom test libraries
use TAP::Harness;
my $harness = TAP::Harness->new(
    verbose => 1,
    lib     => [ 'lib', 'blib/l...
Fixtures
our %clients = (
    1001 => {
        client_id         =>   1001,
        dob               =>   '1926-05-25',
        f...
1002 => {
        client_id         =>   1002,
        dob               =>   '1922-04-21',
        fname             =>  ...
for my $client_id (keys %clients) {
    Test::Client->new(
        id => $clients{ $client_id }
    )->save;
}
for my $client_id (keys %clients) {
    Test::Client->new(
        id => $clients{ $client_id }
    )->save;
}

Test::Clie...
sub create_test_client {




    Test::Client->new( %args )->save;
}

create_test_client(
    id => 2,
    dob => undef,
);
sub create_test_client {

    $args{ dob } ||= ‘1970-01-01’;


    Test::Client->new( %args )->save;
}

create_test_client...
sub create_test_client {

    $args{ dob } ||= ‘1970-01-01’;
    $args{ shrink } ||= create_test_shrink;

    Test::Client...
Mocking
Mocking
Mocking
Test::MockObject
Mocking
Test::MockObject

Test::MockClass
Mocking
Test::MockObject

Test::MockClass

Sub::Override
Mocking
Test::MockDBI

Test::Mock::LWP

Test::Mock::HTTP::Request
Test::Mock::HTTP::Response
Selenium
How to
win friends
and
convince
others to
test
Speeding up tests
Where to start?
Review
Review
Make your tests easy to run
Review
Make your tests easy to run

Run them often
Review
Make your tests easy to run

Run them often

Test your modules
Review
Make your tests easy to run

Run them often

Test your modules

Test your scripts
Review
Make your tests easy to run

Run them often

Test your modules

Test your scripts

Improve your code coverage with ...
Review
Make your tests easy to run

Run them often

Test your modules

Test your scripts

Improve your code coverage with ...
Review
Make your tests easy to run

Run them often

Test your modules

Test your scripts

Improve your code coverage with ...
Review
Review
Make a test library
Review
Make a test library

Use fixtures
Review
Make a test library

Use fixtures

Mock when necessary
Review
Make a test library

Use fixtures

Mock when necessary

Test quietly if you don’t have support
Review
Make a test library

Use fixtures

Mock when necessary

Test quietly if you don’t have support

Start with the most-...
Questions?
Thanks!


    • Michael Schwern
    • Kirrily Robert
    • OSDC
    • realestate.com.au
    • you
Josh Heumann
josh@joshheumann.com
Intermediate Perl Testing (or, Let's Not Make A Mess Of Things, Shall We?)
Intermediate Perl Testing (or, Let's Not Make A Mess Of Things, Shall We?)
Intermediate Perl Testing (or, Let's Not Make A Mess Of Things, Shall We?)
Intermediate Perl Testing (or, Let's Not Make A Mess Of Things, Shall We?)
Intermediate Perl Testing (or, Let's Not Make A Mess Of Things, Shall We?)
Upcoming SlideShare
Loading in …5
×

Intermediate Perl Testing (or, Let's Not Make A Mess Of Things, Shall We?)

2,011 views

Published on

An overview of some methods of perl testing.

Published in: Technology, Business
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,011
On SlideShare
0
From Embeds
0
Number of Embeds
36
Actions
Shares
0
Downloads
78
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Intermediate Perl Testing (or, Let's Not Make A Mess Of Things, Shall We?)

  1. 1. Intermediate Perl Testing (or, Let’s Not Make A Mess Of Things, Shall We?) Josh Heumann
  2. 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’ );
  3. 3. What this talk will do
  4. 4. What this talk won’t do
  5. 5. Why Have Altercations That Encourage Violent Excitable Rants?
  6. 6. Why Have Altercations That Encourage Violent Excitable Rants?
  7. 7. Make them easy to run
  8. 8. Run early, run often
  9. 9. Blackbox testing
  10. 10. Blackbox testing
  11. 11. Blackbox testing
  12. 12. Glassbox testing
  13. 13. Whatever.
  14. 14. Testing scripts #!/usr/bin/perl use strict; use warnings; get_jiggy(); sub get_jiggy { # details, details }
  15. 15. Testing scripts __FILE__
  16. 16. Testing scripts __FILE__ .pl gy g ji t_ ge in/ b
  17. 17. Testing scripts $0
  18. 18. Testing scripts $0 AKA $PROGRAM_NAME
  19. 19. Testing scripts $0 AKA $PROGRAM_NAME pl y. g jig _ et /g in b
  20. 20. Testing scripts $0 AKA $PROGRAM_NAME psl.t . ys ge jggin i ig j_ et t_ es g /bin/ t t
  21. 21. Testing scripts $0 __FILE__ != <> s.tne .pl es ≠ in gy igg g ji j t_ t_ ge es t in/ t/ b
  22. 22. Testing scripts $0 __FILE__ eq == .pl gy .pl g ji gy t_ g ji ge t_ in/ ge b in/ b
  23. 23. Testing scripts #!/usr/bin/perl use strict; use warnings; get_jiggy() if $0 eq __FILE__; sub get_jiggy { # details, details }
  24. 24. 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
  25. 25. Code coverage
  26. 26. Code coverage cover -test
  27. 27. Code coverage cover -test ./Build testcover
  28. 28. Code coverage cover -test ./Build testcover HARNESS_PERL_SWITCHES=-MDevel::Cover make test
  29. 29. Demonstration time!
  30. 30. Version control
  31. 31. Version control
  32. 32. Version control
  33. 33. Testing and version control ?
  34. 34. Testing and version control
  35. 35. Testing and version control
  36. 36. Testing and version control
  37. 37. Testing and version control
  38. 38. Testing and version control
  39. 39. Testing and version control
  40. 40. Testing and version control
  41. 41. Testing and version control
  42. 42. Testing and version control
  43. 43. Testing and version control
  44. 44. Testing and version control
  45. 45. Version control
  46. 46. Reusing test code
  47. 47. Reusing test code for(qw/ is_hilarious is_annoying is_bewildering /) { can_ok( Child, $_ ); is( Child->$_, 1, “...and the child $_” ); }
  48. 48. Reusing test code sub _test_accessor { my ( $method ) = @_; my $getter = quot;get_$methodquot;; my $setter = quot;set_$methodquot;; can_ok( 'Cat', $getter ); can_ok( 'Cat', $setter ); is( Cat->new->$getter, undef, quot;...and $method is undef to start withquot; ); .... } _test_accessor( ‘sleep’ );
  49. 49. Reusing test code sub _test_accessor { my ( $method ) = @_; my $getter = quot;get_$methodquot;; my $setter = quot;set_$methodquot;; can_ok( 'Cat', $getter ); can_ok( 'Cat', $setter ); is( Cat->new->$getter, undef, quot;...and $method is undef to start withquot; ); .... } _test_accessor( $_ ) for(qw/ sleep eat purr /);
  50. 50. Whatever.
  51. 51. Custom test libraries
  52. 52. 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)
  53. 53. 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 ); }
  54. 54. Custom test libraries use TAP::Harness; my $harness = TAP::Harness->new( verbose => 1, lib => [ 'lib', 'blib/lib' ], ); $harness->runtests(@tests); +
  55. 55. Fixtures
  56. 56. 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', },
  57. 57. 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. );
  58. 58. for my $client_id (keys %clients) { Test::Client->new( id => $clients{ $client_id } )->save; }
  59. 59. for my $client_id (keys %clients) { Test::Client->new( id => $clients{ $client_id } )->save; } Test::Client->new(1001)->save;
  60. 60. sub create_test_client { Test::Client->new( %args )->save; } create_test_client( id => 2, dob => undef, );
  61. 61. sub create_test_client { $args{ dob } ||= ‘1970-01-01’; Test::Client->new( %args )->save; } create_test_client( id => 2, dob => undef, );
  62. 62. 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, );
  63. 63. Mocking
  64. 64. Mocking
  65. 65. Mocking Test::MockObject
  66. 66. Mocking Test::MockObject Test::MockClass
  67. 67. Mocking Test::MockObject Test::MockClass Sub::Override
  68. 68. Mocking Test::MockDBI Test::Mock::LWP Test::Mock::HTTP::Request Test::Mock::HTTP::Response
  69. 69. Selenium
  70. 70. How to win friends and convince others to test
  71. 71. Speeding up tests
  72. 72. Where to start?
  73. 73. Review
  74. 74. Review Make your tests easy to run
  75. 75. Review Make your tests easy to run Run them often
  76. 76. Review Make your tests easy to run Run them often Test your modules
  77. 77. Review Make your tests easy to run Run them often Test your modules Test your scripts
  78. 78. Review Make your tests easy to run Run them often Test your modules Test your scripts Improve your code coverage with Devel::Cover
  79. 79. 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
  80. 80. 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
  81. 81. Review
  82. 82. Review Make a test library
  83. 83. Review Make a test library Use fixtures
  84. 84. Review Make a test library Use fixtures Mock when necessary
  85. 85. Review Make a test library Use fixtures Mock when necessary Test quietly if you don’t have support
  86. 86. 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
  87. 87. Questions?
  88. 88. Thanks! • Michael Schwern • Kirrily Robert • OSDC • realestate.com.au • you
  89. 89. Josh Heumann josh@joshheumann.com

×