Test::NoWarnings - Make sure you didn't emit any warnings while testing


Test-NoWarnings documentation Contained in the Test-NoWarnings distribution.

Index


Code Index:

NAME

Top

Test::NoWarnings - Make sure you didn't emit any warnings while testing

SYNOPSIS

Top

For scripts that have no plan

  use Test::NoWarnings;

that's it, you don't need to do anything else

For scripts that look like

  use Test::More tests => x;

change to

  use Test::More tests => x + 1;
  use Test::NoWarnings;

DESCRIPTION

Top

In general, your tests shouldn't produce warnings. This modules causes any warnings to be captured and stored. It automatically adds an extra test that will run when your script ends to check that there were no warnings. If there were any warings, the test will give a "not ok" and diagnostics of where, when and what the warning was, including a stack trace of what was going on when the it occurred.

If some of your tests are supposed to produce warnings then you should be capturing and checking them with Test::Warn, that way Test::NoWarnings will not see them and so not complain.

The test is run by an END block in Test::NoWarnings. It will not be run when any forked children exit.

USAGE

Top

Simply by using the module, you automatically get an extra test at the end of your script that checks that no warnings were emitted. So just stick

  use Test::NoWarnings

at the top of your script and continue as normal.

If you want more control you can invoke the test manually at any time with had_no_warnings().

The warnings your test has generated so far are stored in an array. You can look inside and clear this whenever you want with warnings() and clear_warnings(), however, if you are doing this sort of thing then you probably want to use Test::Warn in combination with Test::NoWarnings.

USE vs REQUIRE

Top

You will almost always want to do

  use Test::NoWarnings

If you do a require rather than a use, then there will be no automatic test at the end of your script.

OUTPUT

Top

If warning is captured during your test then the details will output as part of the diagnostics. You will get:

o

the number and name of the test that was executed just before the warning (if no test had been executed these will be 0 and '')

o

the message passed to warn,

o

a full dump of the stack when warn was called, courtesy of the Carp module

EXPORTABLE FUNCTIONS

Top

had_no_warnings

This checks that there have been warnings emitted by your test scripts. Usually you will not call this explicitly as it is called automatically when your script finishes.

clear_warnings

This will clear the array of warnings that have been captured. If the array is empty then a call to had_no_warnings() will produce a pass result.

warnings

This will return the array of warnings captured so far. Each element of this array is an object containing information about the warning. The following methods are available on these object.

PITFALLS

Top

When counting your tests for the plan, don't forget to include the test that runs automatically when your script ends.

SUPPORT

Top

Bugs should be reported via the CPAN bug tracker at

http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Test-NoWarnings

For other issues, contact the author.

HISTORY

Top

This was previously known as Test::Warn::None

SEE ALSO

Top

Test::Builder, Test::Warn

AUTHORS

Top

Fergal Daly <fergal@esatclear.ie>

Adam Kennedy <adamk@cpan.org>

COPYRIGHT

Top


Test-NoWarnings documentation Contained in the Test-NoWarnings distribution.

package Test::NoWarnings;

use 5.006;
use strict;
use warnings;
use Carp                      ();
use Exporter                  ();
use Test::Builder             ();
use Test::NoWarnings::Warning ();

use vars qw( $VERSION @EXPORT_OK @ISA $do_end_test );
BEGIN {
	$VERSION   = '1.02';
	@ISA       = 'Exporter';
	@EXPORT_OK = qw(
		clear_warnings had_no_warnings warnings
	);

	# Do we add the warning test at the end?
	$do_end_test = 0;
}

my $TEST     = Test::Builder->new;
my $PID      = $$;
my @WARNINGS = ();

$SIG{__WARN__} = make_catcher(\@WARNINGS);

sub import {
	$do_end_test = 1;
	goto &Exporter::import;
}

# the END block must be after the "use Test::Builder" to make sure it runs
# before Test::Builder's end block
# only run the test if there have been other tests
END {
	had_no_warnings() if $do_end_test;
}

sub make_warning {
	local $SIG{__WARN__};

	my $msg     = shift;
	my $warning = Test::NoWarnings::Warning->new;

	$warning->setMessage($msg);
	$warning->fillTest($TEST);
	$warning->fillTrace(__PACKAGE__);

	$Carp::Internal{__PACKAGE__.""}++;
	local $Carp::CarpLevel = $Carp::CarpLevel + 1;
	$warning->fillCarp($msg);
	$Carp::Internal{__PACKAGE__.""}--;

	return $warning;
}

# this make a subroutine which can be used in $SIG{__WARN__}
# it takes one argument, a ref to an array
# it will push the details of the warning onto the end of the array.
sub make_catcher {
	my $array = shift;

	return sub {
		my $msg = shift;

		$Carp::Internal{__PACKAGE__.""}++;
		push(@$array, make_warning($msg));
		$Carp::Internal{__PACKAGE__.""}--;

		return $msg;
	};
}

sub had_no_warnings {
	return 0 if $$ != $PID;

	local $SIG{__WARN__};
	my $name = shift || "no warnings";

	my $ok;
	my $diag;
	if ( @WARNINGS == 0 ) {
		$ok = 1;
	} else {
		$ok = 0;
		$diag = "There were ".@WARNINGS." warning(s)\n";
		$diag .= join "----------\n", map { $_->toString } @WARNINGS;
	}

	$TEST->ok($ok, $name) || $TEST->diag($diag);

	return $ok;
}

sub clear_warnings {
	local $SIG{__WARN__};
	@WARNINGS = ();
}

sub warnings {
	local $SIG{__WARN__};
	return @WARNINGS;
}

sub builder {
	local $SIG{__WARN__};
	if ( @_ ) {
		$TEST = shift;
	}
	return $TEST;
}

1;

__END__