intermediate perl testing (or, let's not make a mess of things, shall we?)

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

Upload: heumann

Post on 20-Jan-2015

1.673 views

Category:

Technology


1 download

DESCRIPTION

An overview of some methods of perl testing.

TRANSCRIPT

Page 1: 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?)

Josh Heumann

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

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’);

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

What this talk will do

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

What this talk won’t do

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

WhyHaveAltercationsThatEncourageViolent ExcitableRants?

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

WhyHaveAltercationsThatEncourageViolent ExcitableRants?

WhyHaveAltercationsThatEncourageViolent ExcitableRants?

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

Make them easy to run

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

Run early, run often

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

Blackbox testing

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

Blackbox testing

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

Blackbox testing

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

Glassbox testing

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

Whatever.

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

#!/usr/bin/perl

use strict;use warnings;

get_jiggy();

sub get_jiggy { # details, details}

Testing scripts

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

__FILE__

Testing scripts

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

__FILE__

bin/get_jig

gy.pl

Testing scripts

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

$0

Testing scripts

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

$0

AKA $PROGRAM_NAME

Testing scripts

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

$0

AKA $PROGRAM_NAME

Testing scripts

bin/get_

jiggy.pl

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

$0

AKA $PROGRAM_NAME

Testing scripts

bin/get_

jiggy.pl

t/test_jigg

iness.t

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

__FILE__

Testing scripts

bin/get_jig

gy.pl

$0

t/test_jigg

iness.t

!=<>ne≠

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

__FILE__

Testing scripts

bin/get_jig

gy.pl

$0

bin/get_jig

gy.pl

eq==

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

#!/usr/bin/perl

use strict;use warnings;

get_jiggy() if $0 eq __FILE__;

sub get_jiggy { # details, details}

Testing scripts

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

#!/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

Testing scripts

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

Code coverage

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

Code coverage

cover -test

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

Code coverage

cover -test

./Build testcover

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

Code coverage

cover -test

./Build testcover

HARNESS_PERL_SWITCHES=-MDevel::Cover make test

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

Demonstration time!

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

Version control

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

Version control

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

Version control

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

Testing and version control

?

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

Testing and version control

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

Testing and version control

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

Testing and version control

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

Testing and version control

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

Testing and version control

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

Testing and version control

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

Testing and version control

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

Testing and version control

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

Testing and version control

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

Testing and version control

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

Testing and version control

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

Version control

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

Reusing test code

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

Reusing test code

for(qw/ is_hilarious is_annoying is_bewildering /) { can_ok( Child, $_ ); is( Child->$_, 1, “...and the child $_” );}

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

Reusing test codesub _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’ );

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

Reusing test codesub _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 /);

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

Whatever.

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

Custom test libraries

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

$ prove tt/00_basics....ok All tests successful.Files=1, Tests=377, 1 wallclock secs ( 0.17 cusr + 0.02 csys = 0.19 CPU)

Custom test libraries

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

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 );}

Custom test libraries

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

use TAP::Harness;my $harness = TAP::Harness->new( verbose => 1, lib => [ 'lib', 'blib/lib' ],);$harness->runtests(@tests);

Custom test libraries

+

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

Fixtures

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

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', },

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

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. );

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

for my $client_id (keys %clients) { Test::Client->new( id => $clients{ $client_id } )->save;}

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

for my $client_id (keys %clients) { Test::Client->new( id => $clients{ $client_id } )->save;}

Test::Client->new(1001)->save;

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

sub create_test_client {

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

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

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

sub create_test_client {

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

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

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

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

sub create_test_client {

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

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

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

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

Mocking

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

Mocking

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

MockingTest::MockObject

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

MockingTest::MockObject

Test::MockClass

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

MockingTest::MockObject

Test::MockClass

Sub::Override

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

MockingTest::MockDBI

Test::Mock::LWP

Test::Mock::HTTP::RequestTest::Mock::HTTP::Response

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

Selenium

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

How to win friends and convince others to test

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

Speeding up tests

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

Where to start?

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

Review

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

ReviewMake your tests easy to run

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

ReviewMake your tests easy to run

Run them often

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

ReviewMake your tests easy to run

Run them often

Test your modules

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

ReviewMake your tests easy to run

Run them often

Test your modules

Test your scripts

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

ReviewMake your tests easy to run

Run them often

Test your modules

Test your scripts

Improve your code coverage with Devel::Cover

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

ReviewMake 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

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

ReviewMake 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

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

Review

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

ReviewMake a test library

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

ReviewMake a test library

Use fixtures

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

ReviewMake a test library

Use fixtures

Mock when necessary

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

ReviewMake a test library

Use fixtures

Mock when necessary

Test quietly if you don’t have support

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

ReviewMake 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

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

Questions?

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

Thanks!

• Michael Schwern• Kirrily Robert• OSDC• realestate.com.au• you