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

696 views
597 views

Published on

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

Published in: Software
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
696
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
1
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

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

  1. 1. Test::Kit 2.0 custom test modules with the features you want Alex Balhatchet @ London.pm Technical Meeting, July 2014
  2. 2. Hi there!
  3. 3. Hi there! ● Alex Balhatchet ● CTO at Nestoria ● Big fan of London.pm technical meetings
  4. 4. Test::Kit
  5. 5. Creating your Kit
  6. 6. Creating your Kit package MyProject::Test; use Test::Kit;
  7. 7. Creating your Kit package MyProject::Test; use Test::Kit; # Combine multiple modules' behaviour into one include 'Test::More'; include 'Test::LongString';
  8. 8. Creating your Kit package 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' }, };
  9. 9. Creating your Kit package 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' ], };
  10. 10. Creating your Kit package 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' ], };
  11. 11. Using your Kit
  12. 12. Using your Kit use strict; use warnings; use MyProject::Test tests => 4;
  13. 13. Using your Kit use 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" );
  14. 14. Using your Kit use 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";
  15. 15. Using your Kit use 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';
  16. 16. Using your Kit use 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';
  17. 17. 2.0
  18. 18. 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
  19. 19. 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
  20. 20. Why use Test::Kit? ● Reduce boilerplate ● Be more consistent ● Easily add behaviour to all your tests ● 1322 files changed, 2325 insertions(+), 7549 deletions(-)
  21. 21. Nestoria Kit
  22. 22. The Nestoria Kit # basics include 'Test::More'; # outputs, warnings and exceptions include 'Test::FailWarnings', 'Test::Warn', 'Test::Exception', 'Test::Output'; # files, data and data structures include 'Test::File', 'Test::LongString', 'Test::JSON'; include 'Test::Deep' => { 'exclude' => [ qw(all any) ] };
  23. 23. The Nestoria Kit # mocking include 'Test::MockObject', 'Test::MockObject::Extends', 'Test::MockModule'; # utilities include 'Data::Dumper';
  24. 24. 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)";
  25. 25. 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"; } } }
  26. 26. Actively Maintained!
  27. 27. import::Into 1.002003 ● Changed behaviour ● Flagged up as failures on CPAN Testers ● Test::Kit 2.01 released 2 days later
  28. 28. 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
  29. 29. See Also
  30. 30. See Also ● ToolSet ● Import::Base ● Import::Into ● Test::Builder::Provider
  31. 31. Questions?
  32. 32. N estoria is H iring! http://lokku.com /jobs Thanks!

×