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

Post on 20-Jan-2015

1673 Views

Category:

Technology

1 Downloads

Preview:

Click to see full reader

DESCRIPTION

An overview of some methods of perl testing.

Transcript

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

What this talk will do

What this talk won’t do

WhyHaveAltercationsThatEncourageViolent ExcitableRants?

WhyHaveAltercationsThatEncourageViolent ExcitableRants?

WhyHaveAltercationsThatEncourageViolent ExcitableRants?

Make them easy to run

Run early, run often

Blackbox testing

Blackbox testing

Blackbox testing

Glassbox testing

Whatever.

#!/usr/bin/perl

use strict;use warnings;

get_jiggy();

sub get_jiggy { # details, details}

Testing scripts

__FILE__

Testing scripts

__FILE__

bin/get_jig

gy.pl

Testing scripts

$0

Testing scripts

$0

AKA $PROGRAM_NAME

Testing scripts

$0

AKA $PROGRAM_NAME

Testing scripts

bin/get_

jiggy.pl

$0

AKA $PROGRAM_NAME

Testing scripts

bin/get_

jiggy.pl

t/test_jigg

iness.t

__FILE__

Testing scripts

bin/get_jig

gy.pl

$0

t/test_jigg

iness.t

!=<>ne≠

__FILE__

Testing scripts

bin/get_jig

gy.pl

$0

bin/get_jig

gy.pl

eq==

#!/usr/bin/perl

use strict;use warnings;

get_jiggy() if $0 eq __FILE__;

sub get_jiggy { # details, details}

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

Testing scripts

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, “...and the child $_” );}

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

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

Whatever.

Custom test libraries

$ 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

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

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

Custom test libraries

+

Fixtures

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

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

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::Client->new(1001)->save;

sub create_test_client {

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

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

sub create_test_client {

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

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

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

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;

Mocking

Mocking

MockingTest::MockObject

MockingTest::MockObject

Test::MockClass

MockingTest::MockObject

Test::MockClass

Sub::Override

MockingTest::MockDBI

Test::Mock::LWP

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

Selenium

How to win friends and convince others to test

Speeding up tests

Where to start?

Review

ReviewMake your tests easy to run

ReviewMake your tests easy to run

Run them often

ReviewMake your tests easy to run

Run them often

Test your modules

ReviewMake your tests easy to run

Run them often

Test your modules

Test your scripts

ReviewMake your tests easy to run

Run them often

Test your modules

Test your scripts

Improve your code coverage with Devel::Cover

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

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

Review

ReviewMake a test library

ReviewMake a test library

Use fixtures

ReviewMake a test library

Use fixtures

Mock when necessary

ReviewMake a test library

Use fixtures

Mock when necessary

Test quietly if you don’t have support

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

Questions?

Thanks!

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

Josh Heumannjosh@joshheumann.com

top related