Capture output (.output())

You can run a command and capture the stdout and stderr to a string using .output().

This is unsuitable for:

  • Capturing output from programs that draw all over the screen (like top).
  • Capturing output from programs that output special terminal characters (e.g. color characters).
  • Interacting with programs that use user input.

For all of the above use cases and more, command.interact() is more suitable.

outputtext:

#!/bin/bash
echo hello from stdout
>&2 echo "hello from stderr"
raiseerror:
#!/bin/bash
echo bad output from stdout
>&2 echo "bad output from stderr"
exit 1

from commandlib import Command

Success:

assert Command("./outputtext").output().strip() \
  == "hello from stdout\nhello from stderr"

Error:

Command("./raiseerror").output().strip()
commandlib.exceptions.CommandExitError:
"./raiseerror" failed (err code 1), output:

bad output from stdout
bad output from stderr

Executable specification

Documentation automatically generated from capture-output.story storytests.