test-class icon indicating copy to clipboard operation
test-class copied to clipboard

num_method_tests() doesn’t grok inheritance correctly

Open FGasper opened this issue 6 years ago • 2 comments

#!perl

use strict;
use warnings;

package t::Base;

use Test::More;

use base 'Test::Class';

sub mytest : Tests(2) {
    ok 1;
    ok 1;
}

package t::Subclass;

use Test::More;

use base 't::Base';

sub new {
    my $class = shift;

    my $self = $class->SUPER::new();

    my $test_install_for_user_base_count = t::Base->num_method_tests('mytest');
    print "base: $test_install_for_user_base_count\n";

    $self->num_method_tests( mytest => 1 + $test_install_for_user_base_count );

    return $self;
}

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

    $self->SUPER::mytest();

    ok 1;
}

__PACKAGE__->new()->runtests() if !caller;

… fails thus:

base: no_plan
Argument "no_plan" isn't numeric in addition (+) at t/inheritance_count.t line 31.
#
# t::Subclass->mytest
1..1
ok 1 - mytest
ok 2 - mytest
ok 3 - mytest
# expected 1 test(s) in t::Subclass::mytest, 3 completed
# Looks like you planned 1 test but ran 3.
Dubious, test returned 255 (wstat 65280, 0xff00)
All 1 subtests passed

Test Summary Report
-------------------
t/inheritance_count.t (Wstat: 65280 Tests: 3 Failed: 2)
  Failed tests:  2-3
  Non-zero exit status: 255
  Parse errors: Bad plan.  You planned 1 tests but ran 3.
Files=1, Tests=3,  0 wallclock secs ( 0.02 usr  0.01 sys +  0.05 cusr  0.00 csys =  0.08 CPU)
Result: FAIL

From looking at the code, it appears that num_method_tests() improperly looks in the call stack for where it’s coming from. In fact, this:

    my $class = _find_calling_test_class( $self )
        or croak "not called in a Test::Class";

… makes no sense because _find_calling_test_class() doesn’t do anything with its given argument.

FGasper avatar Feb 10 '20 21:02 FGasper

This may not be fixable without breaking a bunch of stuff, but it’s at least worth reporting.

FGasper avatar Feb 10 '20 21:02 FGasper

Workaround:

    my $test_install_for_user_base_count = do {
        package t::Base;
        t::Base->num_method_tests('mytest');
    };

FGasper avatar Feb 10 '20 21:02 FGasper