| 1 |
# -*- coding: UTF-8 -*- |
| 2 |
""" |
| 3 |
Unittests for :mod:`behave.capture` module. |
| 4 |
""" |
| 5 |
|
| 6 |
from __future__ import absolute_import, print_function |
| 7 |
import sys |
| 8 |
from behave.capture import Captured, CaptureController |
| 9 |
from mock import Mock |
| 10 |
import pytest |
| 11 |
|
| 12 |
# ----------------------------------------------------------------------------- |
| 13 |
# TEST SUPPORT: |
| 14 |
# ----------------------------------------------------------------------------- |
| 15 |
def create_capture_controller(config=None): |
| 16 |
if not config: |
| 17 |
config = Mock() |
| 18 |
config.stdout_capture = True |
| 19 |
config.stderr_capture = True |
| 20 |
config.log_capture = True |
| 21 |
config.logging_filter = None |
| 22 |
config.logging_level = "INFO" |
| 23 |
return CaptureController(config) |
| 24 |
|
| 25 |
def setup_capture_controller(capture_controller, context=None): |
| 26 |
if not context: |
| 27 |
context = Mock() |
| 28 |
context.aborted = False |
| 29 |
context.config = capture_controller.config |
| 30 |
capture_controller.setup_capture(context) |
| 31 |
|
| 32 |
|
| 33 |
# ----------------------------------------------------------------------------- |
| 34 |
# FIXTURES: |
| 35 |
# ----------------------------------------------------------------------------- |
| 36 |
@pytest.fixture |
| 37 |
def capture_controller(): |
| 38 |
"""Provides a capture_controller that is already setup and automatically |
| 39 |
performs a teardown afterwards. |
| 40 |
""" |
| 41 |
capture_controller = create_capture_controller() |
| 42 |
setup_capture_controller(capture_controller) |
| 43 |
yield capture_controller |
| 44 |
# -- CLEANUP: |
| 45 |
capture_controller.teardown_capture() |
| 46 |
|
| 47 |
|
| 48 |
# ----------------------------------------------------------------------------- |
| 49 |
# TEST SUITE: |
| 50 |
# ----------------------------------------------------------------------------- |
| 51 |
class TestCaptured(object): |
| 52 |
|
| 53 |
def test_default_ctor(self): |
| 54 |
captured = Captured() |
| 55 |
assert captured.stdout == u"" |
| 56 |
assert captured.stderr == u"" |
| 57 |
assert captured.log_output == u"" |
| 58 |
assert not captured |
| 59 |
|
| 60 |
def test_ctor_with_params(self): |
| 61 |
captured = Captured(u"STDOUT", u"STDERR", u"LOG_OUTPUT") |
| 62 |
assert captured.stdout == u"STDOUT" |
| 63 |
assert captured.stderr == u"STDERR" |
| 64 |
assert captured.log_output == u"LOG_OUTPUT" |
| 65 |
|
| 66 |
captured = Captured(stdout=u"STDOUT") |
| 67 |
assert captured.stdout == u"STDOUT" |
| 68 |
assert captured.stderr == u"" |
| 69 |
assert captured.log_output == u"" |
| 70 |
|
| 71 |
captured = Captured(stderr=u"STDERR") |
| 72 |
assert captured.stdout == u"" |
| 73 |
assert captured.stderr == u"STDERR" |
| 74 |
assert captured.log_output == u"" |
| 75 |
|
| 76 |
captured = Captured(log_output=u"LOG_OUTPUT") |
| 77 |
assert captured.stdout == u"" |
| 78 |
assert captured.stderr == u"" |
| 79 |
assert captured.log_output == u"LOG_OUTPUT" |
| 80 |
|
| 81 |
def test_reset(self): |
| 82 |
captured = Captured("STDOUT", "STDERR", "LOG_OUTPUT") |
| 83 |
captured.reset() |
| 84 |
assert captured.stdout == u"" |
| 85 |
assert captured.stderr == u"" |
| 86 |
assert captured.log_output == u"" |
| 87 |
assert not captured |
| 88 |
|
| 89 |
def test_bool_conversion__returns_false_without_captured_output(self): |
| 90 |
captured = Captured() |
| 91 |
assert bool(captured) == False |
| 92 |
|
| 93 |
@pytest.mark.parametrize("params", [ |
| 94 |
dict(stdout="xxx"), |
| 95 |
dict(stderr="yyy"), |
| 96 |
dict(log_output="zzz"), |
| 97 |
dict(stderr="yyy", log_output="zzz"), |
| 98 |
dict(stdout="xxx", stderr="yyy", log_output="zzz"), |
| 99 |
]) |
| 100 |
def test_bool_conversion__returns_true_with_captured_output(self, params): |
| 101 |
captured = Captured(**params) |
| 102 |
assert bool(captured) |
| 103 |
|
| 104 |
@pytest.mark.parametrize("params, expected", [ |
| 105 |
(dict(), ""), |
| 106 |
(dict(stdout="STDOUT"), "STDOUT"), |
| 107 |
(dict(stderr="STDERR"), "STDERR"), |
| 108 |
(dict(log_output="LOG_OUTPUT"), "LOG_OUTPUT"), |
| 109 |
(dict(stdout="STDOUT", stderr="STDERR"), "STDOUT\nSTDERR"), |
| 110 |
(dict(stdout="STDOUT", log_output="LOG_OUTPUT"), "STDOUT\nLOG_OUTPUT"), |
| 111 |
(dict(stdout="STDOUT", stderr="STDERR", log_output="LOG_OUTPUT"), |
| 112 |
"STDOUT\nSTDERR\nLOG_OUTPUT"), |
| 113 |
]) |
| 114 |
def test_output__contains_concatenated_parts(self, params, expected): |
| 115 |
captured = Captured(**params) |
| 116 |
assert captured.output == expected |
| 117 |
|
| 118 |
def test_add__without_captured_data(self): |
| 119 |
captured1 = Captured() |
| 120 |
captured2 = Captured("STDOUT", "STDERR", "LOG_OUTPUT") |
| 121 |
captured1.add(captured2) |
| 122 |
assert captured1.stdout == "STDOUT" |
| 123 |
assert captured1.stderr == "STDERR" |
| 124 |
assert captured1.log_output == "LOG_OUTPUT" |
| 125 |
|
| 126 |
def test_add__with_captured_data(self): |
| 127 |
captured1 = Captured("stdout1", "stderr1", "log_output1") |
| 128 |
captured2 = Captured("STDOUT2", "STDERR2", "LOG_OUTPUT2") |
| 129 |
|
| 130 |
captured1.add(captured2) |
| 131 |
assert captured1.stdout == "stdout1\nSTDOUT2" |
| 132 |
assert captured1.stderr == "stderr1\nSTDERR2" |
| 133 |
assert captured1.log_output == "log_output1\nLOG_OUTPUT2" |
| 134 |
|
| 135 |
def test_operator_add(self): |
| 136 |
captured1 = Captured("stdout1", "stderr1", "log_output1") |
| 137 |
captured2 = Captured("STDOUT2", "STDERR2", "LOG_OUTPUT2") |
| 138 |
|
| 139 |
captured3 = captured1 + captured2 |
| 140 |
assert captured3.stdout == "stdout1\nSTDOUT2" |
| 141 |
assert captured3.stderr == "stderr1\nSTDERR2" |
| 142 |
assert captured3.log_output == "log_output1\nLOG_OUTPUT2" |
| 143 |
# -- ENSURE: captured1 is not modified. |
| 144 |
assert captured1.stdout == "stdout1" |
| 145 |
assert captured1.stderr == "stderr1" |
| 146 |
assert captured1.log_output == "log_output1" |
| 147 |
# -- ENSURE: captured2 is not modified. |
| 148 |
assert captured2.stdout == "STDOUT2" |
| 149 |
assert captured2.stderr == "STDERR2" |
| 150 |
assert captured2.log_output == "LOG_OUTPUT2" |
| 151 |
|
| 152 |
def test_operator_iadd(self): |
| 153 |
captured1 = Captured("stdout1", "stderr1", "log_output1") |
| 154 |
captured2 = Captured("STDOUT2", "STDERR2", "LOG_OUTPUT2") |
| 155 |
|
| 156 |
captured1 += captured2 |
| 157 |
assert captured1.stdout == "stdout1\nSTDOUT2" |
| 158 |
assert captured1.stderr == "stderr1\nSTDERR2" |
| 159 |
assert captured1.log_output == "log_output1\nLOG_OUTPUT2" |
| 160 |
# -- ENSURE: captured2 is not modified. |
| 161 |
assert captured2.stdout == "STDOUT2" |
| 162 |
assert captured2.stderr == "STDERR2" |
| 163 |
assert captured2.log_output == "LOG_OUTPUT2" |
| 164 |
|
| 165 |
def test_make_report__with_all_sections(self): |
| 166 |
captured = Captured(stdout="xxx", stderr="yyy", log_output="zzz") |
| 167 |
expected = """\ |
| 168 |
Captured stdout: |
| 169 |
xxx |
| 170 |
|
| 171 |
Captured stderr: |
| 172 |
yyy |
| 173 |
|
| 174 |
Captured logging: |
| 175 |
zzz""" |
| 176 |
assert captured.make_report() == expected |
| 177 |
|
| 178 |
def test_make_report__should_only_contain_nonempty_data_sections(self): |
| 179 |
captured1 = Captured(stdout="xxx") |
| 180 |
expected = "Captured stdout:\nxxx" |
| 181 |
assert captured1.make_report() == expected |
| 182 |
|
| 183 |
captured2 = Captured(stderr="yyy") |
| 184 |
expected = "Captured stderr:\nyyy" |
| 185 |
assert captured2.make_report() == expected |
| 186 |
|
| 187 |
captured3 = Captured(log_output="zzz") |
| 188 |
expected = "Captured logging:\nzzz" |
| 189 |
assert captured3.make_report() == expected |
| 190 |
|
| 191 |
|
| 192 |
class Theory4ActiveCaptureController(object): |
| 193 |
@staticmethod |
| 194 |
def check_invariants(controller): |
| 195 |
if controller.config.capture_stdout: |
| 196 |
assert controller.stdout_capture is not None |
| 197 |
assert sys.stdout is controller.stdout_capture |
| 198 |
else: |
| 199 |
assert controller.stdout_capture is None |
| 200 |
assert sys.stdout is not controller.stdout_capture |
| 201 |
|
| 202 |
if controller.config.capture_stderr: |
| 203 |
assert controller.stderr_capture is not None |
| 204 |
assert sys.stderr is controller.stderr_capture |
| 205 |
else: |
| 206 |
assert controller.stderr_capture is None |
| 207 |
assert sys.stderr is not controller.stderr_capture |
| 208 |
|
| 209 |
if controller.config.capture_log: |
| 210 |
assert controller.log_capture is not None |
| 211 |
# assert sys.stderr is controller.stderr_capture |
| 212 |
else: |
| 213 |
assert controller.log_capture is None |
| 214 |
# assert sys.stderr is not controller.stderr_capture |
| 215 |
|
| 216 |
|
| 217 |
class TestCaptureController(object): |
| 218 |
|
| 219 |
# @pytest.no_capture |
| 220 |
def test_basics(self): |
| 221 |
capture_controller = create_capture_controller() |
| 222 |
context = Mock() |
| 223 |
context.aborted = False |
| 224 |
context.config = capture_controller.config |
| 225 |
|
| 226 |
capture_controller.setup_capture(context) |
| 227 |
# XXX AVOID: Due to pytest capture mode |
| 228 |
# Theory4ActiveCaptureController.check_invariants(capture_controller) |
| 229 |
|
| 230 |
capture_controller.start_capture() |
| 231 |
sys.stdout.write("HELLO\n") |
| 232 |
sys.stderr.write("world\n") |
| 233 |
capture_controller.stop_capture() |
| 234 |
assert capture_controller.captured.output == "HELLO\nworld\n" |
| 235 |
|
| 236 |
# -- FINALLY: |
| 237 |
capture_controller.teardown_capture() |
| 238 |
|
| 239 |
def test_capturing__retrieve_captured_several_times(self, capture_controller): |
| 240 |
capture_controller.start_capture() |
| 241 |
sys.stdout.write("HELLO\n") |
| 242 |
sys.stderr.write("Alice\n") |
| 243 |
assert capture_controller.captured.output == "HELLO\nAlice\n" |
| 244 |
|
| 245 |
print("Sam") |
| 246 |
sys.stderr.write("Bob\n") |
| 247 |
capture_controller.stop_capture() |
| 248 |
assert capture_controller.captured.output == "HELLO\nSam\nAlice\nBob\n" |
| 249 |
|
| 250 |
|
| 251 |
def test_capturing__with_several_start_stop_cycles(self, capture_controller): |
| 252 |
capture_controller.start_capture() |
| 253 |
sys.stdout.write("HELLO\n") |
| 254 |
sys.stderr.write("Alice\n") |
| 255 |
capture_controller.stop_capture() |
| 256 |
assert capture_controller.captured.output == "HELLO\nAlice\n" |
| 257 |
|
| 258 |
sys.stdout.write("__UNCAPTURED:stdout;\n") |
| 259 |
sys.stderr.write("__UNCAPTURED:stderr;\n") |
| 260 |
|
| 261 |
capture_controller.start_capture() |
| 262 |
sys.stdout.write("Sam\n") |
| 263 |
sys.stderr.write("Bob\n") |
| 264 |
capture_controller.stop_capture() |
| 265 |
assert capture_controller.captured.output == "HELLO\nSam\nAlice\nBob\n" |
| 266 |
|
| 267 |
|
| 268 |
def test_make_capture_report(self, capture_controller): |
| 269 |
capture_controller.start_capture() |
| 270 |
print("HELLO") |
| 271 |
sys.stderr.write("Alice\n") |
| 272 |
capture_controller.stop_capture() |
| 273 |
report = capture_controller.make_capture_report() |
| 274 |
expected = """\ |
| 275 |
Captured stdout: |
| 276 |
HELLO |
| 277 |
|
| 278 |
Captured stderr: |
| 279 |
Alice""" |
| 280 |
assert report == expected |