| 1 |
@issue |
| 2 |
Feature: Issue #194: Nested steps prevent that original stdout/stderr is restored |
| 3 |
|
| 4 |
. When nested steps are used, |
| 5 |
. the original stdout/stderr streams are not restored after the scenario. |
| 6 |
. This is caused by starting/stopping capture again while executing nested steps. |
| 7 |
. |
| 8 |
. ENSURE THAT: |
| 9 |
. * Original streams are restored in after_scenario() hook. |
| 10 |
. * Nested steps should not replace existing capture objects. |
| 11 |
|
| 12 |
@setup |
| 13 |
Scenario: Feature Setup |
| 14 |
Given a new working directory |
| 15 |
And a file named "behave.ini" with: |
| 16 |
""" |
| 17 |
[behave] |
| 18 |
log_capture = false |
| 19 |
logging_level = INFO |
| 20 |
logging_format = LOG.%(levelname)-8s %(name)s: %(message)s |
| 21 |
""" |
| 22 |
And a file named "features/steps/use_behave4cmd_steps.py" with: |
| 23 |
""" |
| 24 |
import behave4cmd0.passing_steps |
| 25 |
import behave4cmd0.failing_steps |
| 26 |
import behave4cmd0.note_steps |
| 27 |
""" |
| 28 |
And a file named "features/steps/stdout_steps.py" with: |
| 29 |
""" |
| 30 |
from behave import given, when, then, step, matchers |
| 31 |
import parse |
| 32 |
import sys |
| 33 |
|
| 34 |
# -- USER-DEFINED DATA TYPES: |
| 35 |
@parse.with_pattern(r"stdout|stderr") |
| 36 |
def parse_stream_name(text): |
| 37 |
assert text in ("stdout", "stderr") |
| 38 |
return text |
| 39 |
|
| 40 |
matchers.register_type(StreamName=parse_stream_name) |
| 41 |
|
| 42 |
# -- UTILITY FUNCTIONS: |
| 43 |
def write_text_to(stream, text, enforce_newline=True): |
| 44 |
if enforce_newline and not text.endswith("\n"): |
| 45 |
text += "\n" |
| 46 |
stream.write(text) |
| 47 |
|
| 48 |
# -- STEP DEFINITIONS: |
| 49 |
@step('I write "{text}" to {stream_name:StreamName}') |
| 50 |
def step_write_text_to_stdxxx(context, text, stream_name): |
| 51 |
stream = getattr(sys, stream_name) |
| 52 |
write_text_to(stream, text) |
| 53 |
|
| 54 |
@step('I execute the following steps') |
| 55 |
def step_execute_steps(context): |
| 56 |
assert context.text, "REQUIRE: context.text" |
| 57 |
context.execute_steps(context.text) |
| 58 |
sys.stdout.write("STDOUT:AFTER-EXECUTE-STEPS\n") |
| 59 |
sys.stderr.write("STDERR:AFTER-EXECUTE-STEPS\n") |
| 60 |
""" |
| 61 |
And a file named "features/environment.py" with: |
| 62 |
""" |
| 63 |
import sys |
| 64 |
|
| 65 |
stdout_id = 0 |
| 66 |
stderr_id = 0 |
| 67 |
|
| 68 |
def stdout_print(text): |
| 69 |
sys.__stdout__.write(text + "\n") |
| 70 |
|
| 71 |
def inspect_stdout(context, scope, statement): |
| 72 |
global stdout_id |
| 73 |
stream_id = id(sys.stdout) |
| 74 |
stream_class = sys.stdout.__class__.__name__ |
| 75 |
expected_id = stdout_id |
| 76 |
if stream_id != expected_id: |
| 77 |
name = statement.name |
| 78 |
stdout_print("CHANGED-STDOUT %s:%s: stream.id=%s:%d (was: %d)" % \ |
| 79 |
(scope, name, stream_class, stream_id, expected_id)) |
| 80 |
stdout_id = stream_id |
| 81 |
|
| 82 |
def inspect_stderr(context, scope, statement): |
| 83 |
global stderr_id |
| 84 |
stream_id = id(sys.stderr) |
| 85 |
stream_class = sys.stderr.__class__.__name__ |
| 86 |
expected_id = stderr_id |
| 87 |
if stream_id != expected_id: |
| 88 |
name = statement.name |
| 89 |
stdout_print("CHANGED-STDERR %s:%s: stream.id=%s:%d (was: %d)" % \ |
| 90 |
(scope, name, stream_class, stream_id, expected_id)) |
| 91 |
stderr_id = stream_id |
| 92 |
|
| 93 |
def inspect_stdxxx(context, scope, statement): |
| 94 |
inspect_stdout(context, scope, statement) |
| 95 |
inspect_stderr(context, scope, statement) |
| 96 |
|
| 97 |
def before_all(context): |
| 98 |
context.config.setup_logging(filename="behave.log") |
| 99 |
|
| 100 |
def before_scenario(context, scenario): |
| 101 |
inspect_stdxxx(context, "before_scenario", scenario) |
| 102 |
|
| 103 |
def after_scenario(context, scenario): |
| 104 |
inspect_stdxxx(context, "after_scenario", scenario) |
| 105 |
# -- ENSURE: Original streams are restored. |
| 106 |
assert sys.stdout is sys.__stdout__ |
| 107 |
assert sys.stderr is sys.__stderr__ |
| 108 |
stdout_print("AFTER-SCENARIO %s: Streams are restored." % scenario.name) |
| 109 |
|
| 110 |
def before_step(context, step): |
| 111 |
inspect_stdxxx(context, "before_step", step) |
| 112 |
|
| 113 |
def after_step(context, step): |
| 114 |
inspect_stdxxx(context, "after_step", step) |
| 115 |
""" |
| 116 |
|
| 117 |
Scenario: Stdout capture works with nested steps |
| 118 |
Given a file named "features/stdout.failing_with_nested.feature" with |
| 119 |
""" |
| 120 |
Feature: |
| 121 |
Scenario: Failing with nested steps |
| 122 |
Given I write "STDOUT:Hello Alice" to stdout |
| 123 |
When I write "STDOUT:Hello Bob" to stdout |
| 124 |
Then I execute the following steps: |
| 125 |
''' |
| 126 |
* I write "STDOUT:Hello nested.Alice" to stdout |
| 127 |
* I write "STDOUT:Hello nested.Bob" to stdout |
| 128 |
''' |
| 129 |
And I write "STDOUT:Hello Charly" to stdout |
| 130 |
And another step fails |
| 131 |
|
| 132 |
Scenario: Another failing |
| 133 |
Given I write "STDOUT:Hello Dora" to stdout |
| 134 |
And another step fails |
| 135 |
""" |
| 136 |
When I run "behave -f plain features/stdout.failing_with_nested.feature" |
| 137 |
Then it should fail with: |
| 138 |
""" |
| 139 |
0 scenarios passed, 2 failed, 0 skipped |
| 140 |
5 steps passed, 2 failed, 0 skipped, 0 undefined |
| 141 |
""" |
| 142 |
And note that "the summary is only shown if hooks have no errors" |
| 143 |
And the command output should contain: |
| 144 |
""" |
| 145 |
Captured stdout: |
| 146 |
STDOUT:Hello Alice |
| 147 |
STDOUT:Hello Bob |
| 148 |
STDOUT:Hello nested.Alice |
| 149 |
STDOUT:Hello nested.Bob |
| 150 |
STDOUT:AFTER-EXECUTE-STEPS |
| 151 |
STDOUT:Hello Charly |
| 152 |
""" |
| 153 |
And the command output should contain: |
| 154 |
""" |
| 155 |
Captured stdout: |
| 156 |
STDOUT:Hello Dora |
| 157 |
""" |
| 158 |
And the command output should contain: |
| 159 |
""" |
| 160 |
AFTER-SCENARIO Failing with nested steps: Streams are restored. |
| 161 |
""" |
| 162 |
And the command output should contain: |
| 163 |
""" |
| 164 |
AFTER-SCENARIO Another failing: Streams are restored. |
| 165 |
""" |
| 166 |
|
| 167 |
Scenario: Stderr capture works with nested steps |
| 168 |
Given a file named "features/stderr.failing_with_nested.feature" with |
| 169 |
""" |
| 170 |
Feature: |
| 171 |
Scenario: Failing with nested steps |
| 172 |
Given I write "STDERR:Hello Alice" to stderr |
| 173 |
When I write "STDERR:Hello Bob" to stderr |
| 174 |
Then I execute the following steps: |
| 175 |
''' |
| 176 |
* I write "STDERR:Hello nested.Alice" to stderr |
| 177 |
* I write "STDERR:Hello nested.Bob" to stderr |
| 178 |
''' |
| 179 |
And I write "STDERR:Hello Charly" to stderr |
| 180 |
And another step fails |
| 181 |
|
| 182 |
Scenario: Another failing |
| 183 |
Given I write "STDERR:Hello Dora" to stderr |
| 184 |
And another step fails |
| 185 |
""" |
| 186 |
When I run "behave -f plain features/stderr.failing_with_nested.feature" |
| 187 |
Then it should fail with: |
| 188 |
""" |
| 189 |
0 scenarios passed, 2 failed, 0 skipped |
| 190 |
5 steps passed, 2 failed, 0 skipped, 0 undefined |
| 191 |
""" |
| 192 |
And note that "the summary is only shown if hooks have no errors" |
| 193 |
And the command output should contain: |
| 194 |
""" |
| 195 |
Captured stderr: |
| 196 |
STDERR:Hello Alice |
| 197 |
STDERR:Hello Bob |
| 198 |
STDERR:Hello nested.Alice |
| 199 |
STDERR:Hello nested.Bob |
| 200 |
STDERR:AFTER-EXECUTE-STEPS |
| 201 |
STDERR:Hello Charly |
| 202 |
""" |
| 203 |
And the command output should contain: |
| 204 |
""" |
| 205 |
Captured stderr: |
| 206 |
STDERR:Hello Dora |
| 207 |
""" |
| 208 |
And the command output should contain: |
| 209 |
""" |
| 210 |
AFTER-SCENARIO Failing with nested steps: Streams are restored. |
| 211 |
""" |
| 212 |
And the command output should contain: |
| 213 |
""" |
| 214 |
AFTER-SCENARIO Another failing: Streams are restored. |
| 215 |
""" |