perlunit-testingtesting

Running only one named test case in a Perl test file


I am trying to find if it is possible to run just one test case in a Perl test file. For example, consider the following Perl test file (copied from http://www.thegeekstuff.com/2013/02/perl-test-simple/):

#!/usr/bin/perl

use strict;
use warnings;

use Test::Simple tests => 2;

sub hello_world
{
return "Hello world!";
}

sub get_number
{
return int(rand(1000));
}
ok( hello_world( ) eq "Hello world!", "My Testcase 1" );
ok( get_number( ) > 0, "My Testcase 2" );

When I run this file using command prove -v test.t I get following output:

test.t .. 
1..2
ok 1 - My Testcase 1
ok 2 - My Testcase 2
ok

Now, I just want to run the 2nd test case i.e. My Testcase2. Is there a way to execute just one named test case in a test file?


Solution

  • The short answer is: No, with the simple test framework provided by Test::Simple (or Test::More) and the prove command there is no way to single out one specific assertion from a test script and run just that assertion.

    However the Perl testing ecosystem has a rich and diverse set of options available. For example Test::Class allows you to add an object oriented layer on top of the standard test protocol. This allows you to group tests into methods, add setup and teardown methods, and also to run individual test methods. You still won't be able to do it all from the command-line without modifying your test script but it's a step closer to what you're asking for.

    Of course a much simpler solution would be to copy out just the the bit you want into its own .t file and run that.