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

This is a curiosity question. A couple of times here at the Monestary, I've come into a thread and worked up a test framework for the various solutions posted. Here are examples:

In a single script, I send multiple tests to each of multiple solutions. Pseudocode:

foreach my $solution ( ... ) { foreach my $test ( ... ) { ok( ... ); } }

The result is that I get multiple lines of output for each solution when the test script is run. The problem is that I'm really only interested in the solution as a whole. Did it pass all tests or not? The solutions are not otherwise related to each other. I'd like to get a report like Test::Harness would give for separate test scripts. I'd like to have something notionally like this:

foreach my $solution ( ... ) { harness "tests for $solution" { foreach my $test ( ... ) { ok( ... ); } } }

Then instead of:

ok 1 - kyle test 1 ok 2 - kyle test 2 ok 3 - elyk test 1 not ok 4 - elyk test 2 # four # lines # of # stuff

...I could get something like this:

kyle...ok elyk...dubious

It would be nice if there were a flag I could set to tell it whether to summarize results or show individual tests. I can think of two ways to get something like what I want, but they're both more pain than I'm willing to endure.

One is to write separate files for each solution. I could put each solution in a heredoc, write them out to a bunch of test files and have Test::Harness actually execute them all individually.

The other is to not execute individual tests as tests. Instead of ok() everywhere, just check the test results manually and call a single pass() or fail() for each solution. If I did this, I'd probably end up writing my_own_ok(), etc. I'd do this so that it would be easy to run it as a full test (without the harness) if I'm interested in the details of one solution.

Is there a better way to do what I'm looking for?

Replies are listed 'Best First'.
Re: Can Test::Harness treat sets of tests as files?
by Old_Gray_Bear (Bishop) on Jan 31, 2008 at 18:04 UTC
    Take a look at prove(), as I recall it came with Test::Harness. Prove() runs test-files (files with a '.t' post-fix) and reports a neat little summary.
    t$ prove t00_* t00_api....ok t00_dep....ok t00_pod....ok t00_upd....ok All tests successful. Files=4, Tests=86, 1 wallclock secs ( 0.46 cusr + 0.03 csys = 0.49 +CPU)
    Is this what you are looking for?

    Update:
    Note: I have 30+ files in the directory, if I run prove() with no arguments, all 33 files will be driven. I provided a subset, for demonstration purposes.

    Update II:
    Shows you how long it's been. Test::Harness (and prove) are part of Core, at least with Perl 5.8.5...

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

    OGB

      Is this what you are looking for?

      Not exactly, no. That's the kind of output I'm looking for, but I'm looking to get it from a single script. I have sets of tests. For each set, I'd like a one-line summary for every test in the set.

        More like this, then?
        t$ prove -v t00_[pu]* t00_pod....1..3 ok 1 - Base is covered ok 2 - Deploy is covered ok 3 - Update is covered ok t00_upd....1..2 ok 1 - Module loaded successfully ok 2 - new() -- returns proper object ok All tests successful. Files=2, Tests=5, 0 wallclock secs ( 0.26 cusr + 0.02 csys = 0.27 C +PU)

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

        OGB

Re: Can Test::Harness treat sets of tests as files?
by TGI (Parson) on Feb 01, 2008 at 08:49 UTC

    I think you could subclass some of the TAP modules. Subclass:

    • TAP::Parser::Source to produce a stream from a code ref, and
    • TAP::Parser to build the right sort of Source object when passed a code ref.
    • TAP::Harness to build the right Parser object to build the right Source.

    Ugh. This sort of thing that can too easily happen with OO inheritance. When it does, I always ask myself, "where did I go wrong?".

    I hope I am missing some key parameter or callback that could be passed to TAP::Harness or TAP::Parser to avoid all the extra subclassing.


    TGI says moo