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