Test::Base
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Test::Base

on

  • 7,003 views

Shibuya.pm tech talk #6

Shibuya.pm tech talk #6

Statistics

Views

Total Views
7,003
Views on SlideShare
7,003
Embed Views
0

Actions

Likes
1
Downloads
70
Comments
1

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Test::Base Presentation Transcript

  • 1. use Test::Base; Tatsuhiko Miyagawa [email_address] Six Apart, Ltd. / Shibuya Perl Mongers Shibuya.pm Tech Talks #7
  • 2.
    • Test::Base
    • とは
  • 3.
    • データドリブン
    • テストベースクラス
  • 4.
    • by Ingy döt Net
  • 5.
    • use Test::More?
  • 6.
    • use Test::Base!
    • Test::More と互換のあるインターフェース
  • 7. Test::More compatible
    • use Test::More;
    • plan tests => 2;
    • sub camelize {
    • local $_ = shift;
    • s/_(w)/uc($1)/eg;
    • $_;
    • }
    • is camelize("foo_bar"), "fooBar";
    • is camelize("foo_bar_baz"), "fooBarBaz";
  • 8. Test::More compatible
    • use Test::Base;
    • plan tests => 2;
    • sub camelize {
    • local $_ = shift;
    • s/_(w)/uc($1)/eg;
    • $_;
    • }
    • is camelize("foo_bar"), "fooBar";
    • is camelize("foo_bar_baz"), "fooBarBaz";
  • 9. Test::Base-y code
    • use Test::Base;
    • sub camelize {
    • local $_ = shift;
    • s/_(w)/uc($1)/eg;
    • $_;
    • }
    • filters { input => 'camelize' }
    • __DATA__
    • ===
    • --- input: foo_bar
    • --- expected: fooBar
    • ===
    • --- input: foo_bar_baz
    • --- expected: fooBarBaz
  • 10. Test::Base code
    • # 01-json.t
    • use Test::Base;
    • use JSON::Syck;
    • sub json { JSON::Syck::Dump($_[0]) }
    • run_is 'input' => 'expected';
    • __END__
    • === Simple JSON Test
    • --- input eval json
    • { foo => "bar" }
    • --- expected chomp
    • {"foo":"bar"}
  • 11. TAP compatible
    • % prove –l 01-json.t
    • 01-json....ok
    • All tests successful.
    • Files=1, Tests=1, 0 wallclock secs ( 0.10 cusr + 0.02 csys = 0.12 CPU)
  • 12. use Test::Base
    • # 01-json.t
    • use Test::Base;
    • use JSON::Syck;
    • sub json { JSON::Syck::Dump($_[0]) }
    • run_is 'input' => 'expected';
    • __END__
    • === Simple JSON Test
    • --- input eval json
    • { foo => "bar" }
    • --- expected chomp
    • {"foo":"bar"}
  • 13. Block names
    • # 01-json.t
    • use Test::Base;
    • use JSON::Syck;
    • sub json { JSON::Syck::Dump($_[0]) }
    • run_is ' input ' => ' expected ';
    • __END__
    • === Simple JSON Test
    • --- input eval json
    • { foo => "bar" }
    • --- expected chomp
    • {"foo":"bar"}
  • 14. Filters
    • # 01-json.t
    • use Test::Base;
    • use JSON::Syck;
    • sub json { JSON::Syck::Dump($_[0]) }
    • run_is 'input' => 'expected';
    • __END__
    • === Simple JSON Test
    • --- input eval json
    • { foo => "bar" }
    • --- expected chomp
    • {"foo":"bar"}
  • 15.
    • perldoc Test::Base::Filter
  • 16. Define Filters
    • # 01-json.t
    • use Test::Base;
    • use JSON::Syck;
    • sub json { JSON::Syck::Dump($_[0]) }
    • run_is 'input' => 'expected';
    • __END__
    • === Simple JSON Test
    • --- input eval json
    • { foo => "bar" }
    • --- expected chomp
    • {"foo":"bar"}
  • 17. Filter arguments === filter args --- input lines array tail=2 Join foo Bar baz --- expected chomp regexp=xis bar.*baz
  • 18. filters __DATA__ === test 1 --- input foo bar baz xxx --- expected quox yyy === test 2 --- input foo bar baz xxx --- expected quox yyy === test 3 --- input foo bar baz xxx --- expected quox yyy
  • 19. filters filters { input => [ 'foo', 'bar', 'baz' ], expected => 'quox' }; __DATA__ === test 1 --- input xxx --- expected yyy === test 2 --- input xxx --- expected yyy === test 3 --- input xxx --- expected yyy
  • 20. run_is
    • # 01-json.t
    • use Test::Base;
    • use JSON::Syck;
    • sub json { JSON::Syck::Dump($_[0]) }
    • run_is 'input' => 'expected';
    • __END__
    • ===
    • --- input eval json
    • { foo => "bar" }
    • --- expected chomp
    • {"foo":"bar"}
  • 21. Auto-diff 01-json....NOK 1 # Failed test ' # @@ -1,2 +1,2 @@ # -{"foo":"bar"} # +{ foo => "bar" } # xxx # ' Requires Algorithm::Diff Turn off with no_diff()
  • 22.
    • run_is
    • run_like
    • run_unlike
    • run_is_deeply
    • run_compare
  • 23. run_is default blocks
    • # 01-json.t
    • use Test::Base;
    • use JSON::Syck;
    • sub json { JSON::Syck::Dump($_[0]) }
    • run_is;
    • __END__
    • ===
    • --- input eval json
    • { foo => "bar" }
    • --- expected chomp
    • {"foo":"bar"}
  • 24. run_is default blocks
    • # 01-json.t
    • use Test::Base;
    • use JSON::Syck;
    • sub json { JSON::Syck::Dump($_[0]) }
    • run_is;
    • __END__
    • ===
    • --- xxx eval json
    • { foo => "bar" }
    • --- yyy chomp
    • {"foo":"bar"}
  • 25. run_compare
    • # 01-json.t
    • use Test::Base;
    • use JSON::Syck;
    • sub json { JSON::Syck::Dump($_[0]) }
    • run_compare;
    • __END__
    • ===
    • --- input eval json
    • { foo => "bar" }
    • --- expected chomp
    • {"foo":"bar"}
    • ===
    • --- input eval json
    • { foo => [ "bar", "baz" ] }
    • --- expected regexp
    • bar.*baz
  • 26. run_compare is default! # 01-json.t use Test::Base; use JSON::Syck; sub json { JSON::Syck::Dump($_[0]) } __END__ === --- input eval json { foo => "bar" } --- expected chomp {"foo":"bar"} === --- input eval json { foo => [ "bar", "baz" ] } --- expected regexp bar.*baz
  • 27. run # 01-json.t use Test::Base; use JSON::Syck; plan tests => 1 * blocks; run { my $block = shift; is JSON::Syck::Dump($block->input), $block->expected, $block->name; }; __END__ === --- input eval { foo => "bar" } --- expected chomp {"foo":"bar"}
  • 28. blocks() # 01-json.t use Test::Base; use JSON::Syck; plan tests => 1 * blocks; for my $block (blocks) { is JSON::Syck::Dump($block->input), $block->expected, $block->name; } __END__ === --- input eval { foo => "bar" } --- expected chomp {"foo":"bar"}
  • 29. Block specific use Test::Base; run_compare; __END__ === --- ONLY --- input eval json { foo => "bar" } --- expected chomp {"foo":"bar"} === --- input eval json { foo => [ "bar", "baz" ] } --- expected regexp bar.*baz
  • 30. Block specific use Test::Base; run_compare; __END__ === --- SKIP --- input eval json { foo => "bar" } --- expected chomp {"foo":"bar"} === --- input eval json { foo => [ "bar", "baz" ] } --- expected regexp bar.*baz
  • 31. Subclassing Test::Base # t/TestJSON.pm package t::TestJSON; use Test::Base -Base; use JSON::Syck; package t::TestJSON::Filter; use Test::Base::Filter –base; sub json { JSON::Syck::Dump($_[0]) } # t/01-json.t use t::TestJSON; __END__ === --- input eval json { foo => "bar" } --- expected …
  • 32. In your CPAN modules use inc::Module::Install; name 'Foo-Bar'; all_from 'lib/Foo/Bar.pm'; use_test_base; WriteAll;
  • 33.
    • Tips
  • 34. Spiffy XXX XXX $foo = $bar; WWW $bar; YYY $baz; ZZZ $quox;
  • 35. no chomp filters use Test::Base; filters { input => 'chomp', expected => 'chomp' } __END__ === --- input foo --- expected bar === --- input fooaba --- expected bzaahiepa
  • 36. no chomp filters use Test::Base; __END__ === --- input: foo --- expected: bar === --- input: fooaba --- expected: bzaahiepa
  • 37. success tests vs. error tests # t/01-success.t run_is 'input' => 'expected'; # t/02-failure.t run { my $block = shift; eval { … }; run_like $@, $block->expected; } ;
  • 38. Test::Base に向かないテスト
    • 複雑な API
    • オブジェクト状態が変化
  • 39.
    • Test::Base で
    • うまくテストできない
    • = 複雑な API
  • 40.
    • Design your API
    • Sufficiently
    • Advanced.
    • © Damian Conway
  • 41.
    • References
    • perldoc Test::Base
    • http://search.cpan.org/~miyagawa/?R=D
  • 42.
    • JSAN
    • Test.Base.js
    • (PMConnect?)
  • 43.
    • Refactoring Example
    • http://yapc.kwiki.org/data-driven-testing/slide10.html