32
Test::Kit 2.0 custom test modules with the features you want Alex Balhatchet @ London.pm Technical Meeting, July 2014

Test::Kit 2.0 (London.pm Technical Meeting July 2014)

Embed Size (px)

DESCRIPTION

A 15 minute introduction to using Test::Kit to reduce boilerplate in your Perl tests.

Citation preview

Page 1: Test::Kit 2.0 (London.pm Technical Meeting July 2014)

Test::Kit 2.0custom test modules with the features you want

Alex Balhatchet @ London.pm Technical Meeting, July 2014

Page 2: Test::Kit 2.0 (London.pm Technical Meeting July 2014)

Hi there!

Page 3: Test::Kit 2.0 (London.pm Technical Meeting July 2014)

Hi there!

● Alex Balhatchet

● CTO at Nestoria

● Big fan of London.pm technical meetings

Page 4: Test::Kit 2.0 (London.pm Technical Meeting July 2014)

Test::Kit

Page 5: Test::Kit 2.0 (London.pm Technical Meeting July 2014)

Creating your Kit

Page 6: Test::Kit 2.0 (London.pm Technical Meeting July 2014)

Creating your Kitpackage MyProject::Test;

use Test::Kit;

Page 7: Test::Kit 2.0 (London.pm Technical Meeting July 2014)

Creating your Kitpackage MyProject::Test;

use Test::Kit;

# Combine multiple modules' behaviour into one include 'Test::More';include 'Test::LongString';

Page 8: Test::Kit 2.0 (London.pm Technical Meeting July 2014)

Creating your Kitpackage MyProject::Test;

use Test::Kit; # Combine multiple modules' behaviour into one include 'Test::More';include 'Test::LongString'; # Exclude or rename exported subs include 'Test::Warn' => { exclude => [ 'warning_is' ], renamed => { 'warning_like' => 'test_warn_warning_like' },};

Page 9: Test::Kit 2.0 (London.pm Technical Meeting July 2014)

Creating your Kitpackage MyProject::Test;

use Test::Kit; # Combine multiple modules' behaviour into one include 'Test::More';include 'Test::LongString'; # Exclude or rename exported subs include 'Test::Warn' => { exclude => [ 'warning_is' ], renamed => { 'warning_like' => 'test_warn_warning_like' },}; # Pass parameters through to import() directly include 'List::Util' => { import => [ 'min', 'max', 'shuffle' ],};

Page 10: Test::Kit 2.0 (London.pm Technical Meeting July 2014)

Creating your Kitpackage MyProject::Test;

use Test::Kit; # Combine multiple modules' behaviour into one include 'Test::More';include 'Test::LongString'; # Exclude or rename exported subs include 'Test::Warn' => { exclude => [ 'warning_is' ], renamed => { 'warning_like' => 'test_warn_warning_like' },}; # Pass parameters through to import() directly include 'List::Util' => { import => [ 'min', 'max', 'shuffle' ],};

Page 11: Test::Kit 2.0 (London.pm Technical Meeting July 2014)

Using your Kit

Page 12: Test::Kit 2.0 (London.pm Technical Meeting July 2014)

Using your Kituse strict;use warnings; use MyProject::Test tests => 4;

Page 13: Test::Kit 2.0 (London.pm Technical Meeting July 2014)

Using your Kituse strict;use warnings; use MyProject::Test tests => 4; ok 1, "1 is true"; like_string( `cat /usr/share/dict/words`, qr/^ kit $/imsx, "kit is a word");

Page 14: Test::Kit 2.0 (London.pm Technical Meeting July 2014)

Using your Kituse strict;use warnings; use MyProject::Test tests => 4; ok 1, "1 is true"; like_string( `cat /usr/share/dict/words`, qr/^ kit $/imsx, "kit is a word"); test_warn_warning_like { warn "foo";}qr/FOO/i,"warned foo";

Page 15: Test::Kit 2.0 (London.pm Technical Meeting July 2014)

Using your Kituse strict;use warnings; use MyProject::Test tests => 4; ok 1, "1 is true"; like_string( `cat /usr/share/dict/words`, qr/^ kit $/imsx, "kit is a word"); test_warn_warning_like { warn "foo";}qr/FOO/i,"warned foo"; is max(qw(1 2 3 4 5)), 5, 'maximum is 5';

Page 16: Test::Kit 2.0 (London.pm Technical Meeting July 2014)

Using your Kituse strict;use warnings; use MyProject::Test tests => 4; ok 1, "1 is true"; like_string( `cat /usr/share/dict/words`, qr/^ kit $/imsx, "kit is a word"); test_warn_warning_like { warn "foo";}qr/FOO/i,"warned foo"; is max(qw(1 2 3 4 5)), 5, 'maximum is 5';

Page 17: Test::Kit 2.0 (London.pm Technical Meeting July 2014)

2.0

Page 18: Test::Kit 2.0 (London.pm Technical Meeting July 2014)

2.0

● 0.101 was written by Ovid

● Really cool idea!

● Unfortunately broken…○ Didn’t work with Test::Aggregate

○ Only worked in package main

○ Complicated “features” architecture

Page 19: Test::Kit 2.0 (London.pm Technical Meeting July 2014)

2.0

● Saw Matt Trout talk about Import::Into at LPW 2013

● Realised it probably solved half the problems with Ovid’s Test::Kit

● Rewrote it from scratch as Test::Kit2

● Got COMAINT from Ovid so I could release it as Test::Kit 2.0

Page 20: Test::Kit 2.0 (London.pm Technical Meeting July 2014)

Why use Test::Kit?

● Reduce boilerplate

● Be more consistent

● Easily add behaviour to all your tests

● 1322 files changed,2325 insertions(+),7549 deletions(-)

Page 21: Test::Kit 2.0 (London.pm Technical Meeting July 2014)

Nestoria Kit

Page 22: Test::Kit 2.0 (London.pm Technical Meeting July 2014)

The Nestoria Kit

# basicsinclude 'Test::More';

# outputs, warnings and exceptionsinclude 'Test::FailWarnings', 'Test::Warn', 'Test::Exception', 'Test::Output';

# files, data and data structuresinclude 'Test::File', 'Test::LongString', 'Test::JSON';include 'Test::Deep' => { 'exclude' => [ qw(all any) ] };

Page 23: Test::Kit 2.0 (London.pm Technical Meeting July 2014)

The Nestoria Kit

# mockinginclude 'Test::MockObject', 'Test::MockObject::Extends', 'Test::MockModule';

# utilitiesinclude 'Data::Dumper';

Page 24: Test::Kit 2.0 (London.pm Technical Meeting July 2014)

The Nestoria Kit

binmode Test::More->builder->output, ":encoding(utf8)";binmode Test::More->builder->failure_output, ":encoding(utf8)";binmode Test::More->builder->todo_output, ":encoding(utf8)";

Page 25: Test::Kit 2.0 (London.pm Technical Meeting July 2014)

The Nestoria Kit

{ my @req_env_vars = ( 'LOKKU_CODE', 'LOKKU_COMMON', 'LOKKU_BIGSLOW', );

foreach my $env_var (@req_env_vars) { if(!$ENV{$env_var}) { die "Environment variable '$env_var' not set"; } }}

Page 26: Test::Kit 2.0 (London.pm Technical Meeting July 2014)

Actively Maintained!

Page 27: Test::Kit 2.0 (London.pm Technical Meeting July 2014)

import::Into 1.002003

● Changed behaviour

● Flagged up as failures on CPAN Testers

● Test::Kit 2.01 released 2 days later

Page 28: Test::Kit 2.0 (London.pm Technical Meeting July 2014)

Test::Builder 1.301001_013

● Dev release of Test::Builder!

● Test::Builder::Module deprecated

● Again, flagged on CPAN Testers

● Not fixed yet, but will be very soon

Page 29: Test::Kit 2.0 (London.pm Technical Meeting July 2014)

See Also

Page 30: Test::Kit 2.0 (London.pm Technical Meeting July 2014)

See Also

● ToolSet

● Import::Base

● Import::Into

● Test::Builder::Provider

Page 31: Test::Kit 2.0 (London.pm Technical Meeting July 2014)

Questions?

Page 32: Test::Kit 2.0 (London.pm Technical Meeting July 2014)

Nestoria

is H

iring!

http://l

okku.co

m/jobs

Thanks!