2

I have a Perl unit test that outputs "ok" for every passed test. I find myself scrolling up and up to find the first failed test, since that's the only thing I'm interested in. I am using Test::More.

use strict; use warnings;
use JSONRegex;
use Test::More;

I would like only failed tests to appear. I tried the solution of using

perl -MTest::Harness -e 'runtests @ARGV' path/to/test_JSONRegex.pl

and it worked. But I wanted to put that in the shebang line so I could just forget about it. The shebang

#!/usr/bin/env perl -MTest::Harness

failed with message Too late for "-MTest::Harness" option at ./test_JSONRegex.pl line 1.

What is the best way to suppress passed test output while still using strict in my script?

mareoraft
  • 3,474
  • 4
  • 26
  • 62
  • 1
    See [How can I redirect the test output from Perl's Test::Simple?](http://stackoverflow.com/questions/1345296/how-can-i-redirect-the-test-output-from-perls-testsimple) – Håkon Hægland Aug 09 '15 at 15:50
  • The equivalent to the command line flag `-MFoo::Bar` that you can use in a script is `use Foo::Bar;`, as documented in [`perldoc perlrun`](http://perldoc.perl.org/perlrun.html). – ThisSuitIsBlackNot Aug 09 '15 at 16:16
  • @ThisSuitIsBlackNot, yes but that won't work for this specific case, where you only want the module if the script is being run directly from the command line (and not already inside a test harness). – cjm Aug 09 '15 at 16:20
  • Thanks for this question - and for the idea running tests **not** with _prove_ but plainly with _perl_ in order to actally get the ok output. :D – Sarge1060 Jul 14 '22 at 12:37

3 Answers3

6

Why not just use the prove command to run your test script?

prove path/to/test_JSONRegex.pl

is basically equivalent to

perl -MTest::Harness -e 'runtests @ARGV' path/to/test_JSONRegex.pl

but with less typing.

Trying to get the test script to automatically determine whether it's already running inside a test harness is going to be tricky and error prone.

cjm
  • 61,471
  • 9
  • 126
  • 175
3
-MTest::Harness

simply puts

use Test::Harness;

at the top of your script. But then what? How do you plan on calling runtests? This is oh-so-very wrong. If you want to save typing, use prove as @cjm mentioned, or create a second small script to run your tests.

#!/bin/sh
# This is path/to/test_JSONRegex
BASE="$( dirname "$( readlink -e "$0" )" )"
perl -MTest::Harness -e'runtests @ARGV' "$BASE/test_JSONRegex.pl"
ikegami
  • 367,544
  • 15
  • 269
  • 518
3

Show only errors during testing:

Test::More->builder->output("/dev/null")                                        # 
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135