Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
szabgab opened this issue Feb 23, 2021 · 0 comments
Open

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

szabgab opened this issue Feb 23, 2021 · 0 comments

Comments

@szabgab
Copy link
Collaborator

szabgab commented Feb 23, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant