Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re: Suggestions for testing interactive CLI apps?

by Corion (Patriarch)
on Apr 21, 2010 at 07:10 UTC ( [id://835972]=note: print w/replies, xml ) Need Help??

in reply to Suggestions for testing interactive CLI apps?

I would just pipe the interaction sequence from STDIN into the program, and capture the whole STDOUT in one go, and at the end compare whether the interaction was as expected. Most likely IPC::Run is the nicest thing to supply a child process with input and capture its output. I would try to stay away from true interactivity. Just push in a preset sequence of strings and only when the program has stopped, look at the generated output. alarm should be used as a safeguard so no runaway child can stop your whole test suite.

  • Comment on Re: Suggestions for testing interactive CLI apps?

Replies are listed 'Best First'.
Re^2: Suggestions for testing interactive CLI apps?
by webfiend (Vicar) on Apr 21, 2010 at 16:05 UTC

    IPC::Run looks quite nice and easy. It looks like the timeout from the module handles alarm-style behavior.

    Actually, IPC::Run looked so nice and easy that I tried it out. My attempts to use it pleased me, so I wrote a little wrapper to hide some of the clutter.

    package Test::CLI; use Modern::Perl; use IPC::Run qw(run timeout); use Test::More; use base qw(Exporter); our @EXPORT_OK = qw( run_output_is ); our $TIMEOUT = 10; sub run_output_is { my ($expected, $command, $input, $diagnostic) = @_; my ($out, $err); run $command, \$input, \$out, \$err, timeout($TIMEOUT); is $expected, $out, $diagnostic; } 1;

    That allowed for a nice simple script in my test code.

    use Modern::Perl; use Test::CLI qw(run_output_is); use Test::More tests => 1; my @command = qw(parrot code/example-01-06.pir); my $in =<<INPUT Brian INPUT ; my $expected =<<EXPECTED Please enter your name: Brian Hello, Brian! EXPECTED ; run_output_is $expected, \@command, $in;

    So thanks for the suggestion! That worked quite nicely for my needs.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://835972]
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2024-04-20 10:54 GMT
Find Nodes?
    Voting Booth?

    No recent polls found