| 1 |
@issue |
| 2 |
Feature: Issue #77: Does not capture stdout from sub-processes |
| 3 |
|
| 4 |
. My step functions are using wrapper objects to interact with SUT. |
| 5 |
. Those wrappers use this kind of thing to invoke executables: |
| 6 |
. |
| 7 |
. subprocess.check_call('myprog', ..., stderr=subprocess.STDOUT) |
| 8 |
. |
| 9 |
. However, the output from those calls does not appear in the stdout |
| 10 |
. captured by behave when a step fails. |
| 11 |
|
| 12 |
|
| 13 |
Background: Test Setup |
| 14 |
Given a new working directory |
| 15 |
Given a file named "hello.py" with: |
| 16 |
""" |
| 17 |
import sys |
| 18 |
|
| 19 |
def hello(): |
| 20 |
result = 0 |
| 21 |
args = sys.argv[1:] |
| 22 |
if args and args[0].startswith("--fail"): |
| 23 |
result = 1 |
| 24 |
args = args[1:] |
| 25 |
message = " ".join(args) |
| 26 |
sys.stdout.write("Hello {0}\n".format(message)) |
| 27 |
sys.exit(result) |
| 28 |
|
| 29 |
if __name__ == "__main__": |
| 30 |
hello() |
| 31 |
""" |
| 32 |
And a file named "features/steps/subprocess_call_steps.py" with: |
| 33 |
""" |
| 34 |
from behave import given, when, then |
| 35 |
import subprocess |
| 36 |
import os.path |
| 37 |
import sys |
| 38 |
|
| 39 |
PYTHON = sys.executable |
| 40 |
HERE = os.path.dirname(__file__) |
| 41 |
|
| 42 |
@when(u'I make a subprocess call "hello {commandline}"') |
| 43 |
def step(context, commandline): |
| 44 |
result = subprocess_call_hello(commandline.split()) |
| 45 |
assert result == 0 |
| 46 |
|
| 47 |
def subprocess_call_hello(args): |
| 48 |
command_args = [ PYTHON, "hello.py" ] + args |
| 49 |
result = subprocess.check_call(command_args, stderr=subprocess.STDOUT) |
| 50 |
return result |
| 51 |
# result = subprocess.check_output(command_args, stderr=subprocess.STDOUT) |
| 52 |
# return result |
| 53 |
""" |
| 54 |
|
| 55 |
Scenario: Subprocess call shows generated output |
| 56 |
Given a file named "features/issue77_hello_OK.feature" with: |
| 57 |
""" |
| 58 |
Feature: |
| 59 |
Scenario: |
| 60 |
When I make a subprocess call "hello world." |
| 61 |
""" |
| 62 |
When I run "behave -f plain features/issue77_hello_OK.feature" |
| 63 |
Then it should pass with: |
| 64 |
""" |
| 65 |
1 scenario passed, 0 failed, 0 skipped |
| 66 |
1 step passed, 0 failed, 0 skipped, 0 undefined |
| 67 |
""" |
| 68 |
And the command output should contain: |
| 69 |
""" |
| 70 |
Hello world. |
| 71 |
""" |
| 72 |
|
| 73 |
Scenario: Subprocess call fails with captured output |
| 74 |
Given a file named "features/issue77_hello_FAIL.feature" with: |
| 75 |
""" |
| 76 |
Feature: |
| 77 |
Scenario: |
| 78 |
When I make a subprocess call "hello --fail FAIL." |
| 79 |
""" |
| 80 |
When I run "behave -f plain features/issue77_hello_FAIL.feature" |
| 81 |
Then it should fail with: |
| 82 |
""" |
| 83 |
0 scenarios passed, 1 failed, 0 skipped |
| 84 |
0 steps passed, 1 failed, 0 skipped, 0 undefined |
| 85 |
""" |
| 86 |
And the command output should contain: |
| 87 |
""" |
| 88 |
Hello FAIL. |
| 89 |
""" |