The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

MooX::Cmd::Tester - MooX cli app commands tester

VERSION

version 0.009

SYNOPSIS

  use MooX::Cmd::Tester;
  use Test::More;

  use MyFoo;

  # basic tests as instance check, initialization check etc. is done there
  my $rv = test_cmd( MyFoo => [ command(s) option(s) ] );

  like( $rv->stdout, qr/operation successful/, "Command performed" );
  like( $rv->stderr, qr/patient dead/, "Deal with expected command error" );

  is_deeply( $rv->execute_rv, \@expected_return_values, "got what I deserve?" );

  cmp_ok( $rv->exit_code, "==", 0, "Command successful" );

DESCRIPTION

The test coverage of most CLI apps is somewhere between poor and wretched. With the same approach as App::Cmd::Tester comes MooX::Cmd::Tester to ease writing tests for CLI apps.

FUNCTIONS

test_cmd

  my $rv = test_cmd( MyApp => \@argv );

test_cmd invokes the app with given argv as if would be invoked from command line and captures the output, the return values and exit code.

Some minor tests are done to prove whether class matches, execute succeeds, command_name and command_chain are not totally scrambled.

It returns an object with following attributes/accessors:

app

Name of package of App

cmd

Name of executed (1st level) command

stdout

Content of stdout

stderr

Content of stderr

output

Content of merged stdout and stderr

error

the exception thrown by running the application (if any)

execute_rv

return values from execute

exit_code

0 on sucess, $! when error occured and $! available, -1 otherwise

test_cmd_ok

  my $rv = test_cmd_ok( MyApp => \@argv );

Runs test_cmd and expects it being successful - command_name must be in command_commands, etc.

Returns the same object test_cmd returns.

If an error occured, no additional test is done (behavior as test_cmd).

ACKNOWLEDGEMENTS

MooX::Cmd::Tester is inspired by App::Cmd::Tester from Ricardo Signes. In fact, I resused the entire design and adopt it to the requirements of MooX::Cmd.

AUTHOR

Torsten Raudssus <torsten@raudss.us>

COPYRIGHT AND LICENSE

This software is copyright (c) 2013 by Torsten Raudssus.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.