Test::Stream::API - Single point of access to Test::Stream extendability features.
There are times where you want to extend or alter the bahvior of a test file or test suite. This module collects all the features and tools that Test::Stream offers for such actions. Everything in this file is accessible in other places, but with less sugar coating.
Nothing is exported by default, you must request it.
use Test::Stream::API qw/ ... /;
use Test::Stream::API qw/ munge /; munge { my ($stream, $event, @subevents) = @_; if($event->isa('Test::Stream::Diag')) { $event->set_message( "KILROY WAS HERE: " . $event->message ); } };
use Test::Stream::API qw/ disable_tap listen /; disable_tap(); listen { my $stream = shift; my ($event, @subevents) = @_; # Tracking results in a db? my $id = log_event_to_db($e); log_subevent_to_db($id, $_) for @subevents; }
use Test::Stream::API qw/ follow_up is_passing /; follow_up { my ($context) = @_; if (is_passing()) { print "KILROY Says the test file passed!\n"; } else { print "KILROY is not happy with you!\n"; } };
use Test::More; use Test::Stream::API qw/ enable_forking /; enable_forking(); # This all just works now! my $pid = fork(); if ($pid) { # Parent ok(1, "From Parent"); } else { # child ok(1, "From Child"); exit 0; } done_testing;
Note: Result order between processes is not guarenteed, but the test number is handled for you meaning you don't need to care.
Results:
ok 1 - From Child ok 2 - From Parent
Or:
ok 1 - From Parent ok 2 - From Child
You may omit any arguments to leave a specific handle unchanged. It is not possible to set a handle to undef or 0 or any other false value.
use Test::Stream::API qw/ set_tap_outputs /; set_tap_outputs( encoding => 'legacy', # Default, std => $STD_IO_HANDLE, # equivilent to $TB->output() err => $ERR_IO_HANDLE, # equivilent to $TB->failure_output() todo => $TODO_IO_HANDLE, # equivilent to $TB->todo_output() );
Note: Each encoding has independant filehandles.
The best way to generate an event is through a Test::Stream::Context object. All events have a method associated with them on the context object. The method will be the last part of the evene package name lowercased, for example Test::Stream::Event::Ok can be issued via $context->ok(...).
$context->ok(...)
use Test::Stream::API qw/ context /; my $context = context(); $context->EVENT_TYPE(...);
The arguments to the event method are the values for event accessors in order, excluding the context, created, and in_subtest arguments. For instance here is how the Ok event is defined:
context
created
in_subtest
package Test::Stream::Event::Ok; use Test::Stream::Event( accessors => [qw/real_bool name diag .../], ... );
This means that the $context->ok method takes up to 5 arguments. The first argument is a boolean true/false, the second is the name of the test, and the third is an arrayref of diagnostics messages or Test::Stream::Event::Diag objects.
$context->ok
$context->ok($bool, $name, [$diag]);
Here are the main event methods, as well as their standard arguments:
Issue an Test::Stream::Event::Ok event.
Issue an Test::Stream::Event::Diag event.
Issue an Test::Stream::Event::Note event.
Issue an Test::Stream::Event::Plan event. $max is the number of expected tests. $directive is a plan directive such as 'no_plan' or 'skip_all'. $reason is the reason for the directive (only applicable to skip_all).
$max
$directive
$reason
Issue an Test::Stream::Event::Bail event.
This is not recommended, but it demonstrates just how much the context shortcut methods do for you.
# First make a context my $context = Test::Stream::Context->new_from_pairs( frame => ..., # Where to report errors stream => ..., # Test::Stream object to use encoding => ..., # encoding from test package meta-data in_todo => ..., # Are we in a todo? todo => ..., # Which todo message should be used? modern => ..., # Is the test package modern? pid => ..., # Current PID skip => ..., # Are we inside a 'skip' state? provider => ..., # What tool created the context? ); # Make the event my $ok = Test::Stream::Event::Ok->new_from_pairs( # Should reflect where the event was produced, NOT WHERE ERRORS ARE REPORTED created => [__PACKAGE__, __FILE__, __LINE__], context => $context, # A context is required in_subtest => 0, bool => $bool, name => $name, diag => \@diag, ); # Send the event to the stream. Test::Stream->shared->send($ok);
All of these are functions. These functions all effect the current-shared Test::Stream object only.
These let you install a callback that is triggered for all primary events. The first argument is the Test::Stream object, the second is the primary Test::Stream::Event, any additional arguments are subevents. All subevents are Test::Stream::Event objects which are directly tied to the primary one. The main example of a subevent is the failure Test::Stream::Event::Diag object associated with a failed Test::Stream::Event::Ok, events within a subtest are another example.
Listen callbacks happen just after TAP is rendered (or just after it would be rendered if TAP is disabled).
Muinspect_todonge callbacks happen just before TAP is rendered (or just before it would be rendered if TAP is disabled).
A followup callback allows you to install behavior that happens either when done_testing() is called, or when the test file completes.
done_testing()
CAVEAT: If done_testing is not used, the callback will happen in the END {...} block used by Test::Stream to enact magic at the end of the test.
END {...}
Turns forking support on. This turns on a synchronization method that *just works* when you fork inside a test. This must be turned on prior to any forking.
This can only be called in the main process or thread. This is a way to manually pull in results from other processes or threads. Typically this happens automatically, but this allows you to ensure results have been gathered by a specific point.
Turn TAP on (on by default).
Turn TAP off.
Show test numbers when rendering TAP.
Do not show test numbers when rendering TAP.
This is the default way to render subtests:
# Subtest: a_subtest ok 1 - pass 1..1 ok 1 - a_subtest
Using this will automatically turn off subtest_tap_delayed
subtest_tap_delayed
This is an alternative way to render subtests, this method waits until the subtest is complete then renders it in a structured way:
ok 1 - a_subtest { ok 1 - pass 1..1 }
Using this will automatically turn off subtest_tap_instant
subtest_tap_instant
This lets you change the encoding for TAP output. This only effects the current test package.
This lets you replace the filehandles used to output TAP for any specific encoding. All fields are optional, any handles not specified will not be changed. The encoding parameter defaults to 'legacy'.
encoding
Note: The todo handle is used for failure output inside subtests where the subtest was started already in todo.
'legacy' is used when encoding is not specified.
Returns a hashref with the output handles:
{ encoding => $encoding, std => $STD_HANDLE, err => $ERR_HANDLE, todo => $TODO_HANDLE, }
Check if a test package has the 'modern' flag.
Note: Throws an exception if $package is not already a test package.
$package
Turn on the modern flag for the specified test package.
These can be used to manipulate a global todo state. When a true value is at the top of the todo stack it will effect any events generated via an Test::Stream::Context object. Typically all events are generated this way.
todo
This lets you set the todo state for the specified test package. This will throw an exception if the package is not a test package.
This lets you inspect the TODO state. Optionally you can specify a package to inspect. The return is a hashref with several keys:
{ TODO => $TODO_STACK_ARRAYREF, TB => $TEST_BUILDER_TODO_STATE, META => $PACKAGE_METADATA_TODO_STATE, PKG => $package::TODO, }
This lets you see what todo states are set where. This is primarily useful when debugging to see why something is unexpectedly TODO, or when something is not TODO despite expectations.
Check if a package is a tester, if it is the meta-object for the tester is returned.
Set the package as a tester and return the meta-object. If the package is already a tester it will return the existing meta-object.
This will get the correct Test::Stream::Context object. This may be one that was previously initialized, or it may generate a new one. Read the Test::Stream::Context documentation for more info.
This will return the current Test::Stream Object. Test::Stream objects typically live on a global stack, the topmost item on the stack is the one that is normally used.
Any events generated inside the codeblock will be intercepted and returned. No events within the block will go to the real Test::Stream instance.
Note: This comes from the Test::Stream::Tester package which provides addiitonal tools that are useful for testing/validating events.
Check how many tests have been run.
Check how many tests have failed.
Check if a plan has been issued, if so the Test::Stream::Event::Plan instance will be returned.
True if the test is complete (after done_testing).
Check if the test state is passing.
The source code repository for Test::More can be found at http://github.com/Test-More/test-more/.
The following people have all contributed to the Test-More dist (sorted using VIM's sort function).
There has been a lot of code migration between modules, here are all the original copyrights together:
Copyright 2014 Chad Granum <exodist7@gmail.com>.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
See http://www.perl.com/perl/misc/Artistic.html
Originally authored by Michael G Schwern <schwern@pobox.com> with much inspiration from Joshua Pritikin's Test module and lots of help from Barrie Slaymaker, Tony Bowden, blackstar.co.uk, chromatic, Fergal Daly and the perl-qa gang.
Idea by Tony Bowden and Paul Johnson, code by Michael G Schwern <schwern@pobox.com>, wardrobe by Calvin Klein.
Copyright 2001-2008 by Michael G Schwern <schwern@pobox.com>.
To the extent possible under law, 唐鳳 has waived all copyright and related or neighboring rights to Test-use-ok.
This work is published from Taiwan.
http://creativecommons.org/publicdomain/zero/1.0
This module is copyright 2005 Fergal Daly <fergal@esatclear.ie>, some parts are based on other people's work.
Under the same license as Perl itself
Copyright Mark Fowler <mark@twoshortplanks.com> 2002, 2004.
To install Test::Simple, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Test::Simple
CPAN shell
perl -MCPAN -e shell install Test::Simple
For more information on module installation, please visit the detailed CPAN module installation guide.