5

When I run a perl test using the prove utility, it fails if the method under test contains print statements that are not terminated by newlines.

use Test::More tests=>1;

ok(foo(), "calling foo");

sub foo{
    print "A";
    1;
}

This results in

Bad plan.  You planned 1 tests but ran 0.

If I append a newline: print "A\n"; the test passes.

(Note that if I simply execute the test perl mytest.t rather than using prove it passes either way).

Any ideas why this might be, and how to work around it?

perlyking
  • 557
  • 7
  • 14
  • 5
    See http://stackoverflow.com/q/1538260/1030675 The output of `print` interferes with what `prove` expects. – choroba Jan 16 '12 at 12:56

1 Answers1

2

I found a quick workaround:

$|=0;     # no auto-flush

...but I have no idea (yet) why this works.

kubanczyk
  • 5,184
  • 1
  • 41
  • 52
  • Thanks this works and is simpler than localising stdout etc. I can see that when I do this, with the verbose switch enabled, that the test print statements appear on a line _after_ the `ok - test passed` line, whereas before they appeared on the same line. It's almost as if `prove` were parsing its own output when deciding if the test run had passed 100%. (Note that the original test was not failing per se; rather `prove` was discounting it when tallying up the number of tests run.) – perlyking Jan 16 '12 at 15:19