Test::Base

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    1 Group

    Test::Base - Presentation Transcript

    1. use Test::Base; Tatsuhiko Miyagawa [email_address] Six Apart, Ltd. / Shibuya Perl Mongers Shibuya.pm Tech Talks #7
      • Test::Base
      • とは
      • データドリブン
      • テストベースクラス
      • by Ingy döt Net
      • use Test::More?
      • use Test::Base!
      • Test::More と互換のあるインターフェース
    2. 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";
    3. 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";
    4. 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
    5. 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"}
    6. 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)
    7. 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"}
    8. 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"}
    9. 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"}
      • perldoc Test::Base::Filter
    10. 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"}
    11. Filter arguments === filter args --- input lines array tail=2 Join foo Bar baz --- expected chomp regexp=xis bar.*baz
    12. 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
    13. 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
    14. 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"}
    15. 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()
      • run_is
      • run_like
      • run_unlike
      • run_is_deeply
      • run_compare
    16. 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"}
    17. 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"}
    18. 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
    19. 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
    20. 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"}
    21. 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"}
    22. 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
    23. 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
    24. 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 …
    25. In your CPAN modules use inc::Module::Install; name 'Foo-Bar'; all_from 'lib/Foo/Bar.pm'; use_test_base; WriteAll;
      • Tips
    26. Spiffy XXX XXX $foo = $bar; WWW $bar; YYY $baz; ZZZ $quox;
    27. no chomp filters use Test::Base; filters { input => 'chomp', expected => 'chomp' } __END__ === --- input foo --- expected bar === --- input fooaba --- expected bzaahiepa
    28. no chomp filters use Test::Base; __END__ === --- input: foo --- expected: bar === --- input: fooaba --- expected: bzaahiepa
    29. 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; } ;
    30. Test::Base に向かないテスト
      • 複雑な API
      • オブジェクト状態が変化
      • Test::Base で
      • うまくテストできない
      • = 複雑な API
      • Design your API
      • Sufficiently
      • Advanced.
      • © Damian Conway
      • References
      • perldoc Test::Base
      • http://search.cpan.org/~miyagawa/?R=D
      • JSAN
      • Test.Base.js
      • (PMConnect?)
      • Refactoring Example
      • http://yapc.kwiki.org/data-driven-testing/slide10.html

    + miyagawamiyagawa, 3 years ago

    custom

    3589 views, 0 favs, 0 embeds more stats

    Shibuya.pm tech talk #6

    More Info

    © All Rights Reserved

    Go to text version
    • Total Views 3589
      • 3589 on SlideShare
      • 0 from embeds
    • Comments 0
    • Favorites 0
    • Downloads 58
    Most viewed embeds

    more

    All embeds

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as innappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel

    Categories

    Tags

    Groups / Events