Git Product home page Git Product logo

test-output's Introduction

Coverage Status

The Test::Output module

This is the README for the Test::Output Perl module. You're probably looking at this because you don't know where else to find what you're looking for. Read this once and you might never have to read one again for any Perl module.

Documentation

To read about Test::Output, look at the embedded documentation in the module itself. Inside the distribution, you can format it with perldoc:

% perldoc lib/Test/Output.pm

If you have already installed the module, you can specify the module name instead of the file location:

% perldoc Test::Output

You can read the documentation and inspect the meta data at MetaCPAN.

The standard module documentation has example uses in the SYNOPSIS section, but you can also look in the examples/ directory (if it's there), or look at the test files in t/.

Installation

You can install this module with a CPAN client, which will resolve and install the dependencies:

% cpan Test::Output
% cpanm Test::Output

You can also install directly from the distribution directory, which will also install the dependencies:

% cpan .
% cpanm .

You could install just this module manually:

% perl Makefile.PL
% make
% make test
% make install

You probably don't want to do that unless you're fiddling with the module and only want to run the tests without installing anything.

Source location

The meta data, such as the source repository and bug tracker, is in Makefile.PL or the META.* files it creates. You can find that on those CPAN web interfaces, but you can also look at files directly in the source repository:

If you find a problem, file a ticket in the issue tracker:

Getting help

Although I'm happy to hear from module users in private email, that's the best way for me to forget to do something.

Besides the issue trackers, you can find help at Perlmonks or Stackoverflow, both of which have many competent Perlers who can answer your question, almost in real time. They might not know the particulars of this module, but they can help you diagnose your problem.

You might like to read brian's Guide to Solving Any Perl Problem.

You should have received a LICENSE file, but the license is also noted in the module files. About the only thing you can't do is pretend that you wrote code that you didn't.

Good luck!

Enjoy,

brian d foy, [email protected]

test-output's People

Contributors

briandfoy avatar chorny avatar dsteinbrunner avatar edwardbetts avatar okxchg avatar xdg avatar yibe avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

test-output's Issues

Optionally use tee instead of capture

We're using Test::Output and often have the situation that we print debugging statements when working on a test.
To see the actual output, we have to change stderr_like to stderr_from, save it into a variable, add a like statement and diag the output.
It would be helpful if Test::Output could use Capture::Tiny's tee function on request, for example via an environment variable.
If you think this could be useful as well, I would try to make a PR.

License ambiguity again

This old chestnut again.

The 'LICENSE' file says that the module is licensed "under the same terms as perl itself, under the Artistic License 2.0", which is self-contradicting because perl is licensed under either GPL or Artistic 1 licenses, not Artistic 2. The text in the module file itself says "same as perl", without mentioning Artistic 2.0. I suspect you mean to license the module as Artistic 2.0, in which case it would be better to get rid of the "same as perl" text everywhere and just say "Artistic 2.0" instead.

stderr capture not working

I'm struggling to figure out why in some test cases, this (great!) module captures stderr and in others it does not. I am probably doing something dumb but maybe there is an actual problem.

I have a perl program that I need to test main() for. An example test is below.

output_like( sub { main( '-z' ) },
             qr/./,
             qr/Unexpected option -z, ignoring/ms,
             'Unexpected options are ignored'
           );                                                                   

The code that generates the stderr output is

sub main {
...
    if ( $ARGV[0] =~ /^-/ ) {
        warn "Unexpected option $ARGV[0], ignoring.\n";
        shift @ARGV;
        next;
    }

The issue I am seeing is when I get partway through a battery of tests I get failures because output from warn() is ending up on the screen and not being captured by output_like().

I tried to write a reproducer but can't find a simple case.
The failing calling chain is: main()->message()->warn()
Here's the attempted reproducer:

$ cat z.pl
#!/usr/bin/env perl

use strict;
use warnings;
use File::Path qw(remove_tree);
use File::Spec;
use Sys::Hostname;

use FindBin qw($Bin); 
use lib ("$Bin", "$Bin/..", "$Bin/lib", "$Bin/../lib");
unless( caller() ) {
    main( @ARGV );
    exit;
}

sub main {
   message("abc123");
};

sub message {
   warn "Warning from inside message()\n";
}

1;

$ cat t/z.t
use strict;
use warnings;

use FindBin qw($Bin);
use lib ("$Bin", "$Bin/..", "$Bin/lib", "$Bin/../lib");

use Test::More tests => 1;
use Test::Output;

require 'z.pl';

chdir('./t') if ( -d './t' );

my $startdir = `pwd`;
chomp($startdir);

output_like( sub { main() },
             qr/.*/,
             qr/Warning from/,
             'Detects warning from inside subroutine'
           );

This passes fine.

$ perl t/z.t 
1..1
ok 1 - Detects warning from inside subroutine

The test I am having trouble with is in the midst of about 70-odd tests in one file.
Some of them do a bit of set up and tear down along the way, but I don't see why that would be a problem. When I run the set of tests, what happens is I get the warning printed on stderr

$ perl t/10_main.t 1>/dev/null
#   Failed test 'Empty directories are detected and no message is sent.'
#   at t/10_dirmanager.t line 375.
# STDERR:
# 
# doesn't match:
# (?^:.*Directory\s+sempty,\s+message.*)
# as expected
Directory empty, message not sent

instead of captured by output_like.

I've tested this against the latest git version (in lib/Test/Output.pm).
Any feedback would be greatly welcomed.

Importing combined_from broken in 1.032

It seems combined_from is not exported by default anymore. Importing all functions works:

use Test::More tests => 1;
use Test::Output qw(:functions);
say Test::Output->VERSION;
my $out = combined_from { say "hello" };
like $out, qr{hello};
__END__
1..1
1.032
ok 1

But not by default:

use Test::More tests => 1;
use Test::Output;         
say Test::Output->VERSION;
my $out = combined_from { say "hello" };
like $out, qr{hello};
__END__
1..1
1.032
hello
Can't locate object method "combined_from" via package "1" (perhaps you forgot to load "1"?) at -e line 5.
# Looks like your test exited with 255 before it could output anything.

It worked in 1.031:

use Test::More tests => 1;
use Test::Output;
say Test::Output->VERSION;
my $out = combined_from { say "hello" };
like $out, qr{hello};
__END__
1..1
1.031
ok 1

Other functions are still working:

use Test::More tests => 1;
use Test::Output;
say Test::Output->VERSION;
my $out = combined_like { say "hello" } qr{hello}, "label"
__END__
1..1
1.032
ok 1 - label

syntax errors in pod code examples

Hi, in the code examples for section TESTS subsection OUTPUT, subsubsection output_is, output_isnt, there are two code blocks (following

is functionally equivalent to
.

One for output_is

 stdout_is(sub {print "foo";},'foo')
    && stderr_is(sub {print STDERR "bar";'bar');

which should be

 stdout_is(sub {print "foo";},'foo')
    && stderr_is(sub {print STDERR "bar";},'bar');

(adds the missing curly closing brace and comma before the last argument of stderr_is)

and another one for output_isnt

  stdout_is(sub {print "foo";},'bar')
    && stderr_is(sub {print STDERR "bar";'foo');

which should be

  stdout_isnt(sub {print "foo";},'bar')
    && stderr_isnt(sub {print STDERR "bar";},'foo');

(changes std*_is -> std*_isnt and adds the missing curly brace and comma)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.