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

In my unit testing I frequently find that I'd like to be able to fail-fast on my unit tests. I usually organize them so that later tests depend on the success of prior ones to insure the tools/modules/logic is working correctly.

Unfortunately, if one of the earlier tests fail, there might be a massive wave of other failures shortly there after and it can be difficult to find that first one. I noticed that Test::Harness itself doesn't seem to have a fail-fast option or flag.

Is there any way to make it behave in this fashion?

Any suggestions or thoughts would be welcome on this!

  • Comment on How would I make Test::Harness fail-fast?

Replies are listed 'Best First'.
Re: How would I make Test::Harness fail-fast?
by gwadej (Chaplain) on Jun 01, 2009 at 03:36 UTC

    I recently stumbled across this in something I was reading. In Test::Most, there are a pair of functions die_on_fail and bail_on_fail which might do what you need.

    G. Wade

      This looks to be the kind of behavior I'm looking for. From what I can tell ENV{BAIL_ON_FAIL} is a flag that'll do it.

      I'll give it a run and see how it goes, thanks a bunch!

      Danny.
Re: How would I make Test::Harness fail-fast?
by Khen1950fx (Canon) on Jun 01, 2009 at 00:03 UTC
    Test::Harness does have fail and fast via prove. See perldoc prove to get a better idea of what you can do. Here's an example where I ran the tests in the t directory of Math::Pari source:

    prove -v --state=failed,fast,save /my/Desktop/Math-Pari/Math-Pari-2.010800/t

    --state - asks prove to remember the state of previous runs
    failed - runs only the tests that failed on the last run
    fast - runs the tests in fastest to slowest order
    save - saves the state on exit
    -v - means verbose

    If you would like to add a timer, then

    prove -v --state=failed,fast,save -p --timer

    Update: added "fast"

      As I understand it "fast-fail" would be that the entire process aborts prematurely at the first sign of failure. In the case of the testing it'd mean that the entire test suite stops at the first failed test.

      This is the behavior I am looking for.

      Danny.
        I understand. I suggested prove because it was the closest thing to fail-fast that I could think of.
Re: How would I make Test::Harness fail-fast?
by syphilis (Archbishop) on Jun 01, 2009 at 00:16 UTC
    if one of the earlier tests fail ... it can be difficult to find that first one

    Doesn't the report at the end make it apparent which test failed first ?

    Cheers,
    Rob
      It generates a summary at the end, but it can be quite noisy. It isn't the behavior I'm really looking for.
      Danny.
Re: How would I make Test::Harness fail-fast?
by Bloodnok (Vicar) on Jun 01, 2009 at 09:52 UTC
    I, now, use (pun intended) autodie to achieve the goal you seek e.g.
    use Test::More qw/no_plan/; use autodie qw/use_ok require_ok ok .../; require_ok('Some::Class'); use_ok('Some::Class'); . . .
    Depending on the criteria, the opt list to the module should be used to delineate the test names on which you wish to quit early.

    A user level that continues to overstate my experience :-))