carcassonne has asked for the wisdom of the Perl Monks concerning the following question:

Monks,

I'm starting using a common approach to make tests, based on module-starter. And so I have t subdirectories such as:

t/10_Module1Test t/20_Module2Test t/30_Module3Test

Each subdirectory will contain numerical-prefixed series of .t files.

My questions are, these tests will be run against many configuration files. For each individual test to load a config file is rather silly. Is there a well-known approach of sharing resources between multiple tests ? And, how can several configuration files be used, with the same tests run for each config file ? The numbering scheme seems to point to sequential execution instead of loops.

Bonus question: I've read José's Guide (here on PerlMonks) although it does not elaborate on testing strategies. Are there references out there that explores various ways of constructing tests ?

Thanks and best wishes for 2010!

Replies are listed 'Best First'.
Re: Tests strategies: sharing of resources
by Old_Gray_Bear (Bishop) on Dec 29, 2009 at 16:36 UTC
    The Classic: Perl Testing, A Developer's Notebook by Langworth and chromatic. This book covers testing strategies from the very basic to the very complex. It is well worth the (re)reading.

    ----
    I Go Back to Sleep, Now.

    OGB

Re: Tests strategies: sharing of resources
by ikegami (Patriarch) on Dec 29, 2009 at 18:33 UTC

    Each .t file is an independent program. How do you share something with a file on disk?

    You could convert each test program you want to run for every config file into a module that will be used by a test program that looks something like:

    #!/usr/bin/perl use Test::More; use lib ...; my @configs = ...; my @test_modules = ...; my $num_tests = 0; for my $test_module (@test_modules) { require $test_module; $num_tests += $test_module->get_num_tests(); } plan tests => @configs * $num_tests; for my $config (@configs) { for my $test_module (@test_modules) { $test_module->run_tests($config); } }
      Thanks for the suggestions and the book reference.

      Ikegami: in essence, this would be having a .t file that is actually a control file for running all tests. What I wonder right now about this approach is, how would that fit in the 'make test' generated by module-starter. 'make test' would look at all files in the t/ subdirectory. So that means putting the control file there but all other test files elsewhere outside of t/. Same goes with Narveson's suggestion.

      Currently I have all tests in a single file using Test:More. That is fine, but will become kind of ugly as the project grows. This is why I'm trying out module-starter. The modules will probably never be on CPAN but, I'd nevertheless like to adopt an approach which is generalized on CPAN if only to get familiar with it. So I'd like to devise a solution that is compatible and flows nicely.

      Maybe that's the solution. As Narveson remarked, might not be that clever, but will keep things going: put some 'control files' in the t/ subdir and have the actual tests in another directory.

        'make test' would look at all files in the t/ subdirectory.

        I'm pretty sure it only looks for .t files, so placing the modules in a subdirectory of t/ is not a requirement, but it's not a bad idea either.

Re: Tests strategies: sharing of resources
by Narveson (Chaplain) on Dec 29, 2009 at 16:56 UTC
    how can several configuration files be used, with the same tests run for each config file ?

    One way is simply to write your own loop.

    for my $config_file (@LIST) { ok is_valid($config_file); # ... is $this, $as_expected; }

    If you have three configurations in @LIST and two tests in your loop block, then just add six to your planned test count.

    This is not very clever, but it gets you going, and with test suites, it's better to install something simple right away. You can always upgrade the cleverness later.