I have being taking a look at some modules of perl, and I have seen it is kind of a standard to have a lot of tests under a folder t
whith the extension .t
.
Those scripts can be executed directly with perl <test>.t
.
I want to do a little framework to test one application. I want to have many simple tests. And then a main script which calls this tests to validate everything at once, but still being able to run the little tests individually without having to disable manually the other tests in the main script.
My question is how should I do this calling? As suggested in How do I run a Perl script from within a Perl script? this interprocess communication should be avoided and instead use modules but that would make not possible to run the tests directly.
My guess was to do something as follows. In an example where I need to give some parameters in a selenium script This is the folder structure:
├── main_script.pl
└── t
├── 00-Test_1.t
├── 01-Test_2.t
├── 02-Test_3.t
└── 03-Test_4.t
And I would call them like this:
system($^X, "./t/00-Test_1.t", "-b $browser", "-s $server", "-d $debug");
my $results = capture($^X, "./t/00-Test_1.t", "-b $browser", "-s $server", "-d $debug";
$log->info("Result is $results");
And each test would have something like this:
my ($browser, $server, $debug);
GetOptions(
'debug|d' => \$debug,
'debug|d' => \$trace,
'trace|t' => \$browser,
'server|s=s' => \$server,
) or die "[ERROR] Invalid options passed to $0\n";
Is this a correct approach or is there something more appropiate for doing this using modules?
You already have "a main script which calls this tests to validate everything at once, but still being able to run the little tests individually" as part of the standard Perl distribution. It's called prove
.
Running prove
with no parameters will run all test scripts matching t/*.t
(so you want the tests in a t/
subdirectory when you run prove
, not in the same directory), or you can use prove t/00-Test_1.t
to run a single test script.
Note that prove
expects the test scripts to emit output in TAP ("Test Anything Protocol") format, which is usually accomplished by using Test::More or other Test::* modules in the test scripts.
If you follow the convention of putting your application-specific modules in a lib/
subdirectory, then running prove -l
will add them to @INC
to make them available when running the test scripts. i.e.,
Root directory (run `prove` from here)
├── lib
| └── MyProject
| └── MyModule.pm
└── t
├── 00-Test_1.t
├── 01-Test_2.t
├── 02-Test_3.t
└── 03-Test_4.t