Skip to content

Test::Class doesn't detect early exit from tests #43

@szabgab

Description

@szabgab

Sometimes the code you're testing might have embedded somewhere deep within it a call to exit (or, a call to die, and a $SIG{DIE} handler that causes it to exit gracefully.)

With Test::More, this will be caught as long as you define a plan or use done_testing:

  $ cat test2.pl
  #!/usr/bin/perl

  use strict;
  use warnings;

  use Test::More;

  ok(1);
  exit;
  ok(2);

  done_testing;

  $ perl test2.pl 
  ok 1
  # Tests were run but no plan was declared and done_testing() was not seen.

However, with Test::Class, even if you define a plan in your subtests, an early exit can cause the rest of them to run and it seems like everything is okay:

  $ cat test.pl
  #!/usr/bin/perl

  use strict;
  use warnings;

  package My::Tests;

  use base qw(Test::Class);
  use Test::More;

  sub t01_the_test : Tests(3) {
    my ($self) = @_;

    ok(1);
    exit;
  }

  sub t02_more_tests : Tests {
    ok(1);
  }
 
  My::Tests->runtests;


  $ perl test.pl
  ok 1 - t01 the test
  1..1

I think early exits should be caught - at least in the case where a plan is specified, but realistically in both if possible. (I'd like to pretend there's an explicit done_testing at the end of every subtest that doesn't have a plan defined.)


This is almost "by design": https://metacpan.org/pod/Test::Class#RETURNING-EARLY

However, dies are correctly detected: https://metacpan.org/pod/Test::Class#HANDLING-EXCEPTIONS

Test::Class predates done_testing. In fact it doesn't even use subtests to encapsulate each test method as its own test, instead doing some convoluted backflips to swap out the Test::Builder object. It most definitely needs a rewrite to use more modern TB internals. :( :(


That's bad enough, but at least early returns are limited to the enclosing test method and the return statement must be in that method.

An exit causes all subsequent test methods to never run and can be hidden anywhere in the program :/

Original: https://rt.cpan.org/Ticket/Display.html?id=103224

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions