Upgrade to Pro — share decks privately, control downloads, hide ads and more …

新時代のテストフレームワークTest2

akiym
July 01, 2017

 新時代のテストフレームワークTest2

YAPC::Fukuoka 2017 HAKATA

akiym

July 01, 2017
Tweet

More Decks by akiym

Other Decks in Technology

Transcript

  1. w ಺෦Ͱ͸5FTU#VJMEFSΛར༻ sub ok ($;$) { my( $test, $name )

    = @_; my $tb = Test::More->builder; return $tb->ok( $test, $name ); } 5FTU.PSFQN
  2. w σʔλߏ଄TFU CBH TVQFSIBTIPG w ΦϒδΣΫτͷϝιουͷνΣοΫ w ֦ுੑ͕ߴ͍5FTU%FFQ use Test::Deep;

    cmp_deeply [[1,3,2], {a => 1, b => 2, c => 3}], [set(1,2,3), superhashof({a => 1})]; ॱෆಉ LFZBͷνΣοΫɺ΄͔͸ແࢹ
  3. use Test::Mock::Guard; my $g = mock_guard 'ExternalAPI', { fetch =>

    sub { 1 }, }; { my $g = mock_guard 'ExternalAPI', { fetch => sub { 0 }, }; ExternalAPI->fetch; #=> 0 } ExternalAPI->fetch; #=> 1 ͜ͷείʔϓ಺ͷΈͰ༗ޮ
  4. package Foo; use parent qw/Test::Class/; sub setup : Test(setup) {

    $_[0]->{something} = 'init'; } sub foo : Tests { ... } sub bar : Tests { ... } __PACKAGE__->runtests; 5&45@.&5)0%GPPQSPWFUFTUU
  5. use utf8; use Test::More; pass 'ਖ਼͍͠'; done_testing; Wide character in

    print at /.../Test2/Formatter/ TAP.pm line 113. ok 1 - ਖ਼͍͠ 1..1
  6. binmode Test::More->builder->$_, ":utf8" for qw/output failure_output todo_output/; no warnings 'redefine';

    my $code = \&Test::Builder::child; *Test::Builder::child = sub { my $builder = $code->(@_); binmode $builder->$_, ":utf8" for qw/output failure_output todo_output/; return $builder; }; *__ORIGINAL::Test::Builder::_print_to_fh = \&Test::Builder::_print_to_fh; *Test::Builder::_print_to_fh = sub { my ($self, $fh, @args) = @_; binmode $fh, ':utf8'; goto \&__ORIGINAL::Test::Builder::_print_to_fh; }
  7. package t::test; sub import { my $pkg = caller; eval

    qq{ package $pkg; use parent qw/Test::Class/; use Test::More; END { $pkg->runtests; } }; } package Foo; use t::test; sub foo : Tests { fail; } # __PACKAGE__->runtests; # ෆཁ ऴྃ࣌ʹSVOUFTUT͕ݺ͹ΕΔ ςετ
  8. not ok 1 - foo # Failed test 'foo' #

    at /.../Test/Builder.pm line 135. # (in main->foo) 1..1 # Looks like you failed 1 test of 1. τϨʔε͕յΕΔ ςετग़ྗ
  9. package t::test; use Hook::AfterRuntime (); sub import { my $pkg

    = caller; eval qq{ package $pkg; use parent qw/Test::Class/; use Test::More; }; Hook::AfterRuntime::after_runtime { $pkg->runtests; }; }
  10. package Foo; use strict; use warnings; use t::test; sub foo

    : Tests { } package Foo; use t::test; use warnings; use strict; my $__ENDRUNAAAAAAAA = 'Hook::AfterRuntime'->new(0); sub foo { &pass(); } package Foo; use attributes ('Foo', sub { package Foo; use warnings; use strict; &pass(); } , 'Tests'); %FQBSTF σετϥΫλ͕ݺ͹ΕΔ ϑΝΠϧ ͷऴΘΓ ίʔυΛJNQPSUઌʹ ૠೖ͍ͯ͠Δ
  11. my $ref = {a => 1, b => 2, c

    => [1, 2, 3]} # σʔλߏ଄͕ਖ਼͍͔͠νΣοΫ is $ref, {a => 1, b => 2, c = [1, 2, 3]}; # ࢦఆͨ͠΋ͷҎ֎͸νΣοΫ͠ͳ͍ like $ref, {b => 2};
  12. use Test2::V0; my $ref = {a => ['A'], b =>

    'baz'}; like $ref, hash { field a => array { item 'a' }; field b => match qr/bar/; # ଘࡏ͢Δ͔ field c => E(); end(); };
  13. not ok 1 # Failed test at test.t line 9.

    # +--------+-----------------------+---------+--------------+------+ # | PATH | GOT | OP | CHECK | LNs | # +--------+-----------------------+---------+--------------+------+ # | | HASH(0x7fbddc028400) | | <HASH> | 3, 9 | # | {a} | ARRAY(0x7fbddc028418) | | <ARRAY> | 4 | # | {a}[0] | A | eq | a | 4 | # | {b} | baz | =~ | (?^:bar) | 5 | # | {c} | <DOES NOT EXIST> | !exists | <DOES EXIST> | 7 | # +--------+-----------------------+---------+--------------+------+ ςετग़ྗ
  14. my $mock = mock 'Foo' => ( add => [add_method

    => sub { ... }], override => [replace_method => sub { ... }], ); Foo->add_method; Foo->replaced_method;
  15. my $ref = {}; ref_ok($ref, 'HASH', 'ϋογϡϦϑΝϨϯε͔Ͳ͏͔'); isa_ok([], 'ARRAY'); #

    Failed test 'ARRAY->isa('ARRAY')' # at test.t line 3. # ARRAY is neither a blessed reference or a package name. 5FTU4VJUFͷJTB@PLͷڍಈ͸5FTU.PSFͱҟͳΔ
  16. use Test::More; sub my_ok { ok($_[0]); } my_ok(0); my_ok(0); not

    ok 1 # Failed test at test.pl line 4. not ok 2 # Failed test at test.pl line 4. Ͳͷݺͼग़͠ݩͰGBJM͔ͨ͠Θ͔Βͳ͘ͳΔ
  17. use Test::More; sub my_ok { local $Test::Builder::Level = $Test::Builder::Level +

    1; ok($_[0]); } 5FTU#VJMEFS MPDBM5FTU#VJMEFS-FWFM  ͱॻ͍ͯ͸͍͚ͳ͍
  18. use Test::More; use Test2::API qw/context/; sub my_ok { my $ctx

    = context(); ok($_[0]); $ctx->release; } 5FTU
  19. not ok 1 # Failed test at test.pl line 10.

    not ok 2 # Failed test at test.pl line 11.
  20. use Test2::V0; use Test2::Plugin::SourceDiag; my $something = 'test'; my $pat

    = qr/test2/; like $something, $pat; done_testing;
  21. not ok 1 - is $something, $pat; # Failed test

    'is $something, $pat;' # at test.t line 6. # +------+----+------------+ # | GOT | OP | CHECK | # +------+----+------------+ # | test | =~ | (?^:test2) | # +------+----+------------+ ςετग़ྗ
  22. use Test::Deep; cmp_deeply $foo, isa('Foo') & methods(foo => 'FOO'); 5FTU%FFQ

    5FTU4VJUF use Test2::V0; is $foo, object { prop blessed => 'Foo'; call foo => 'FOO'; };
  23. not ok 1 # Failed test at t/methods.t line 16.

    # Compared $data->foo # got : 'BAR' # expect : 'FOO' 5FTU%FFQ 5FTU%FFQ-JLF not ok 1 # Failed test at t/methods.t line 16. # +-------+-----+----+-------+ # | PATH | GOT | OP | CHECK | # +-------+-----+----+-------+ # | foo() | BAR | eq | FOO | # +-------+-----+----+-------+
  24. use Test2::V0; use Test2::DeepLike; my $foo = bless {}, 'Foo';

    my $bar = bless {}, 'Bar'; is [$foo, $bar], bag(isa('Foo'), isa('Baz'));
  25. not ok 1 # Failed test at test.t line 7.

    # Comparing $data as a Bag # Missing: 1 reference # Extra: 1 reference 5FTU%FFQ 5FTU%FFQ-JLF not ok 1 # Failed test at test.t line 7. # +------+--------------------------+---------+------------------+-----+ # | PATH | GOT | OP | CHECK | LNs | # +------+--------------------------+---------+------------------+-----+ # | [*] | <DOES NOT EXIST> | ISA | Baz | 7 | # | [1] | Bar=HASH(0x7f91c0803268) | !exists | <DOES NOT EXIST> | | # +------+--------------------------+---------+------------------+-----+