Perl Testing

Peter J. Holzer
hjp@hjp.at

Zitate

Program maintenance is an entropy-increasing process, and even its most skillful execution only delays the subsidence of the system into unfixable obsolescence. Frederick Brooks: The Mythical Man Month (1975)
Theoretically, after each fix one must run the entire bank of test cases previously run against the system, to ensure that it has not been damaged in an obscure way. In practice such regression testing must indeed approximate this theoretical ideal, and it is costly. Frederick Brooks: The Mythical Man Month (1975)
All programmers are optimists. Frederick Brooks: The Mythical Man Month (1975)

Regression Testing

Test-Driven Development

Perl Test-Framework

Test::More

#!/usr/bin/perl
use warnings;
use strict;
use Test::More;

ok(1,         "1 is true");
ok(0,         "0 is true");
ok(5 < 10, "5 numerically less than 10");
ok(5 lt 10,   "5 is collated before 10");
done_testing;
      

Test::More

% ./t/test1.t
ok 1 - 1 is true
not ok 2 - 0 is true
#   Failed test '0 is true'
#   at test1 line 6.
ok 3 - 5 numerically less than 10
not ok 4 - 5 is collated before 10
#   Failed test '5 is collated before 10'
#   at test1 line 8.
1..4
# Looks like you failed 2 tests of 4.
      

Test::More

% prove t/test1.t
test1 .. 1/? 
#   Failed test '0 is true'
#   at test1 line 6.

#   Failed test '5 is collated before 10'
#   at test1 line 8.
# Looks like you failed 2 tests of 4.
test1 .. Dubious, test returned 2 (wstat 512, 0x200)
Failed 2/4 subtests 

Test Summary Report
-------------------
test1 (Wstat: 512 Tests: 4 Failed: 2)
  Failed tests:  2, 4
  Non-zero exit status: 2
Files=1, Tests=4,  0 wallclock secs ( 0.04 usr  0.00 sys +  0.02 cusr  0.00 csys =  0.06 CPU)
Result: FAIL
#   at test1 line 8.
1..4
# Looks like you failed 2 tests of 4.
      

Test::More

#!/usr/bin/perl
use warnings;
use strict;
use Test::More;

ok(1,         "1 is true");
ok(!0,        "0 is false");
ok(5 < 10, "5 numerically less than 10");
ok(5 gt 10,   "5 is collated after 10");
done_testing;
      

Test::More

% prove t/test1.t
test2 .. ok   
All tests successful.
Files=1, Tests=4,  0 wallclock secs ( 0.02 usr  0.01 sys +  0.02 cusr  0.00 csys =  0.05 CPU)
Result: PASS
      

Test::More

  use Test::More tests => 23;

  BEGIN { use_ok( 'Some::Module' ); }
  require_ok( 'Some::Module' );

  # Various ways to say "ok"
  ok($got eq $expected, $test_name);

  is  ($got, $expected, $test_name);
  isnt($got, $expected, $test_name);

  # Rather than print STDERR "# here's what went wrong\n"
  diag("here's what went wrong");

  like  ($got, qr/expected/, $test_name);
  unlike($got, qr/expected/, $test_name);

  cmp_ok($got, '==', $expected, $test_name);

  is_deeply($got_complex_structure, $expected_complex_structure, $test_name);

  can_ok($module, @methods);
  isa_ok($object, $class);

Test::More

  SKIP: {
      skip $why, $how_many unless $have_some_feature;

      ok( foo(),       $test_name );
      is( foo(42), 23, $test_name );
  };

  TODO: {
      local $TODO = $why;

      ok( foo(),       $test_name );
      is( foo(42), 23, $test_name );
  };

prove-Optionen

Andere Test-Module

Test-Organisation

Devel::Cover

Sammelt Test Coverage Daten:

Devel::Cover Verwendung