1

I'm using Test::More to test my application. I have a single script, run_tests.pl, that runs all the tests. Now I want to split this into run_tests_component_A.pl and B, and run both test suites from run_tests.pl. What is the proper way of doing this, does Test::More have any helpful methods?

I'm not using any build system.

Anna
  • 2,645
  • 5
  • 25
  • 34
  • 1
    It seems to me that you are doing it wrong. You can make as many test scripts as you want, they normally end in `.t` and put them in a directory. Then, you use Test::More's `prove` with their directory and it will run them all and report. Note that `Test::More` is now just a wrapper around Test2-Suite, so you might want to look into starting with that. – Ecuador Jul 10 '22 at 13:21
  • If it's not clear, for running tests you don't do `perl run_tests.pl`, you do something like `prove -lvr testdir`. For Test2, `prove` is replaced by `yath`. – Ecuador Jul 10 '22 at 13:22
  • @Ecuador I followed the perl documentation [Test::Tutorial](https://perldoc.perl.org/Test::Tutorial), and which never mentions `prove`. The Test::More docs have a single mention under "Other components". Are you sure I'm using it wrong and not just in another way from you? – Anna Jul 10 '22 at 16:27
  • @Anna yes, I am pretty sure, that's an ancient and very basic tutorial, from the fact that you want to have more than 1 test file you have outgrown it, so you should start using a test harness like `prove`. Perhaps take a look at https://perlmaven.com/testing - it even has an intro to Test2 which is the modern Perl test framework I mentioned. – Ecuador Jul 10 '22 at 18:08
  • @Ecuador Thanks for pointing me there and to `prove`! – Anna Jul 11 '22 at 08:15

1 Answers1

4

Instead of running the creating a run_tests.pl to run the test suite, the standard practice is to use prove.

Say you have

t/foo.t
t/bar.t

Then,

  • prove is short for prove t.
  • prove t runs the entire test suite (both t/foo.t and t/bar.t).
  • prove t/foo.t runs that specific test file.
  • perl t/foo.t runs that specific test file, and you get the raw output. Easier for debugging.
  • perl -d t/foo.t even allows you to run the test in the debugger.

Each file is a self-standing program. If you need to share code between test programs, you can create t/lib/Test/Utils.pm (or whatever) and use the following in your test files:

use FindBin qw( $RealBin );
use lib "$RealBin/lib";
use Test::Utils;

prove executes the files in alphabetical order, so it's common to name the files

00_baseline.t
01_basic_tests.t
02_more_basic_tests.t
03_advanced_tests.t

The 00 test tests if the modules can be loaded and that's it. It usually outputs the versions of loaded modules to help with dependency problems. Then you have your more basic tests. The stuff that's like "if this doesn't work, you have major problems". There's no point in testing the more complex features if the basics don't work.

ikegami
  • 367,544
  • 15
  • 269
  • 518