php - How do I test a command-line program with PHPUnit? -
how test command-line program phpunit? see plenty of using phpunit command line, none testing command-line program phpunit.
this comes because writing command-line programs in php , joomla, don't see way test output, when errors occur (because cannot test error output using phpunit's expectoutputstring()).
(edit: note bulk of code in classes tested phpunit -- i'm looking way test command-line (wrapper) program's logic.)
one way use backtick operator (`) capture output of program, examine output. works under unix/linux-style oses, can capture error outputs stderr. (it more painful under windows, can done (especially using cygwin).)
for example:
public function testhelp() { $output = `./add-event --help 2>&1`; $this->assertregexp( '/^usage:/m', $output, 'no message?' ); $this->assertregexp( '/where:/m', $output, 'no message?' ); $this->assertnotregexp( '/where event types are:/m', $output, 'no message?' ); }
you can see both stdout , stderr captured $output, regex assertions used test whether output resembled correct output.
Comments
Post a Comment