Robert
Robert

Reputation: 8571

How can I debug a single Perl unit test sub?

I have the usual MakeMaker module with a t/ tests directory, and I can run a single test file with e.g., prove -I lib t/my-test.t.

My tests use Test::Class and Test::More and subs (with the modulino technique from Effective Perl), like this:

use strict;
use warnings;
use base 'Test::Class';  
use Test::More;

__PACKAGE__->runtests() unless caller;

sub set_up : Test(setup) {
    # ...
} 

sub test_something : Test {
    is(MyModule::some_sub(1), 1);
}

# ...more test subs...

Now I want to use the Perl debugger to investigate a test sub that shows a problem in my module. I want to only run test_something in the debugger, without running all the other test subs in the .t file.

prove does not seem to have such an option.

perl -d -I lib t/my-test.t runs all tests, unless I change my modulino to call the setup method and then the actual test method instead of __PACKAGE__->runtests():

unless (caller) { 
    set_up(); 
    test_something();  
    done_testing();  
}

How can I run only one test sub without modifying the code?

Upvotes: 3

Views: 666

Answers (1)

mob
mob

Reputation: 118605

To avoid running all of your tests, caller must be defined when your test script is loaded. Try something like this:

$ perl -Ilib -de 1
DB<1>   do 't/my-test.t'
DB<2>   set_up()
DB<3>   b test_something_else
DB<4>   test_something_else()
    ... step through test_something_else() function ...
DB<16>   done_testing()

Upvotes: 8

Related Questions