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

7a8922191c1fa60728589a3def4c179f?s=47 akiym
July 01, 2017

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

YAPC::Fukuoka 2017 HAKATA

7a8922191c1fa60728589a3def4c179f?s=128

akiym

July 01, 2017
Tweet

Transcript

  1. 2.
  2. 7.

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

    = @_; my $tb = Test::More->builder; return $tb->ok( $test, $name ); } 5FTU.PSFQN
  3. 9.

    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ͷνΣοΫɺ΄͔͸ແࢹ
  4. 13.

    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 ͜ͷείʔϓ಺ͷΈͰ༗ޮ
  5. 18.

    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
  6. 25.
  7. 36.
  8. 39.
  9. 41.

    use utf8; use Test::More; pass 'ਖ਼͍͠'; done_testing; Wide character in

    print at /.../Test2/Formatter/ TAP.pm line 113. ok 1 - ਖ਼͍͠ 1..1
  10. 43.

    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; }
  11. 46.
  12. 49.

    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͕ݺ͹ΕΔ ςετ
  13. 50.

    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. τϨʔε͕յΕΔ ςετग़ྗ
  14. 51.
  15. 55.

    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; }; }
  16. 56.

    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ઌʹ ૠೖ͍ͯ͠Δ
  17. 66.

    my $ref = {a => 1, b => 2, c

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

    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(); };
  19. 69.

    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 | # +--------+-----------------------+---------+--------------+------+ ςετग़ྗ
  20. 71.

    my $mock = mock 'Foo' => ( add => [add_method

    => sub { ... }], override => [replace_method => sub { ... }], ); Foo->add_method; Foo->replaced_method;
  21. 78.

    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ͱҟͳΔ
  22. 81.

    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͔ͨ͠Θ͔Βͳ͘ͳΔ
  23. 83.

    use Test::More; sub my_ok { local $Test::Builder::Level = $Test::Builder::Level +

    1; ok($_[0]); } 5FTU#VJMEFS MPDBM5FTU#VJMEFS-FWFM  ͱॻ͍ͯ͸͍͚ͳ͍
  24. 84.

    use Test::More; use Test2::API qw/context/; sub my_ok { my $ctx

    = context(); ok($_[0]); $ctx->release; } 5FTU
  25. 85.

    not ok 1 # Failed test at test.pl line 10.

    not ok 2 # Failed test at test.pl line 11.
  26. 88.

    use Test2::V0; use Test2::Plugin::SourceDiag; my $something = 'test'; my $pat

    = qr/test2/; like $something, $pat; done_testing;
  27. 89.

    not ok 1 - is $something, $pat; # Failed test

    'is $something, $pat;' # at test.t line 6. # +------+----+------------+ # | GOT | OP | CHECK | # +------+----+------------+ # | test | =~ | (?^:test2) | # +------+----+------------+ ςετग़ྗ
  28. 92.

    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'; };
  29. 94.

    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 | # +-------+-----+----+-------+
  30. 95.

    use Test2::V0; use Test2::DeepLike; my $foo = bless {}, 'Foo';

    my $bar = bless {}, 'Bar'; is [$foo, $bar], bag(isa('Foo'), isa('Baz'));
  31. 96.

    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> | | # +------+--------------------------+---------+------------------+-----+
  32. 97.