def test_reset(self): captured = Captured("STDOUT", "STDERR", "LOG_OUTPUT") captured.reset() assert captured.stdout == u"" assert captured.stderr == u"" assert captured.log_output == u"" assert not captured
def test_add__without_captured_data(self): captured1 = Captured() captured2 = Captured("STDOUT", "STDERR", "LOG_OUTPUT") captured1.add(captured2) assert captured1.stdout == "STDOUT" assert captured1.stderr == "STDERR" assert captured1.log_output == "LOG_OUTPUT"
def test_add__with_captured_data(self): captured1 = Captured("stdout1", "stderr1", "log_output1") captured2 = Captured("STDOUT2", "STDERR2", "LOG_OUTPUT2") captured1.add(captured2) assert captured1.stdout == "stdout1\nSTDOUT2" assert captured1.stderr == "stderr1\nSTDERR2" assert captured1.log_output == "log_output1\nLOG_OUTPUT2"
def test_make_report__with_all_sections(self): captured = Captured(stdout="xxx", stderr="yyy", log_output="zzz") expected = """\ Captured stdout: xxx Captured stderr: yyy Captured logging: zzz""" assert captured.make_report() == expected
def test_operator_iadd(self): captured1 = Captured("stdout1", "stderr1", "log_output1") captured2 = Captured("STDOUT2", "STDERR2", "LOG_OUTPUT2") captured1 += captured2 assert captured1.stdout == "stdout1\nSTDOUT2" assert captured1.stderr == "stderr1\nSTDERR2" assert captured1.log_output == "log_output1\nLOG_OUTPUT2" # -- ENSURE: captured2 is not modified. assert captured2.stdout == "STDOUT2" assert captured2.stderr == "STDERR2" assert captured2.log_output == "LOG_OUTPUT2"
def __init__(self, filename, line, keyword, name): filename = filename or '<string>' filename = os.path.relpath(filename, os.getcwd()) # -- NEEDS: abspath? self.location = FileLocation(filename, line) assert isinstance(keyword, six.text_type) assert isinstance(name, six.text_type) self.keyword = keyword self.name = name # -- SINCE: 1.2.6 self.captured = Captured() # -- ERROR CONTEXT INFO: self.exception = None self.exc_traceback = None self.error_message = None
def test_make_report__should_only_contain_nonempty_data_sections(self): captured1 = Captured(stdout="xxx") expected = "Captured stdout:\nxxx" assert captured1.make_report() == expected captured2 = Captured(stderr="yyy") expected = "Captured stderr:\nyyy" assert captured2.make_report() == expected captured3 = Captured(log_output="zzz") expected = "Captured logging:\nzzz" assert captured3.make_report() == expected
def test_ctor_with_params(self): captured = Captured(u"STDOUT", u"STDERR", u"LOG_OUTPUT") assert captured.stdout == u"STDOUT" assert captured.stderr == u"STDERR" assert captured.log_output == u"LOG_OUTPUT" captured = Captured(stdout=u"STDOUT") assert captured.stdout == u"STDOUT" assert captured.stderr == u"" assert captured.log_output == u"" captured = Captured(stderr=u"STDERR") assert captured.stdout == u"" assert captured.stderr == u"STDERR" assert captured.log_output == u"" captured = Captured(log_output=u"LOG_OUTPUT") assert captured.stdout == u"" assert captured.stderr == u"" assert captured.log_output == u"LOG_OUTPUT"
def __init__(self, filename, line, keyword, name): filename = filename or '<string>' try: filename = os.path.relpath(filename, os.getcwd()) # -- NEEDS: abspath? except ValueError: # On Windows a relative path can't be evaluated for # paths on two different drives (i.e. c:\foo and f:\bar). # The only thing left to is to use the original absolute # path. filename = filename self.location = FileLocation(filename, line) assert isinstance(keyword, six.text_type) assert isinstance(name, six.text_type) self.keyword = keyword self.name = name # -- SINCE: 1.2.6 self.captured = Captured() # -- ERROR CONTEXT INFO: self.exception = None self.exc_traceback = None self.error_message = None
class BasicStatement(object): def __init__(self, filename, line, keyword, name): filename = filename or '<string>' filename = os.path.relpath(filename, os.getcwd()) # -- NEEDS: abspath? self.location = FileLocation(filename, line) assert isinstance(keyword, six.text_type) assert isinstance(name, six.text_type) self.keyword = keyword self.name = name # -- SINCE: 1.2.6 self.captured = Captured() # -- ERROR CONTEXT INFO: self.exception = None self.exc_traceback = None self.error_message = None @property def filename(self): # return os.path.abspath(self.location.filename) return self.location.filename @property def line(self): return self.location.line def reset(self): # -- RESET: Captured output data self.captured.reset() # -- RESET: ERROR CONTEXT INFO self.exception = None self.exc_traceback = None self.error_message = None def store_exception_context(self, exception): self.exception = exception self.exc_traceback = sys.exc_info()[2] def __hash__(self): # -- NEEDED-FOR: PYTHON3 # return id((self.keyword, self.name)) return id(self) def __eq__(self, other): # -- PYTHON3 SUPPORT, ORDERABLE: # NOTE: Ignore potential FileLocation differences. return (self.keyword, self.name) == (other.keyword, other.name) def __lt__(self, other): # -- PYTHON3 SUPPORT, ORDERABLE: # NOTE: Ignore potential FileLocation differences. return (self.keyword, self.name) < (other.keyword, other.name) def __ne__(self, other): return not self.__eq__(other) def __le__(self, other): # -- SEE ALSO: python2.7, functools.total_ordering # return not other < self return other >= self def __gt__(self, other): # -- SEE ALSO: python2.7, functools.total_ordering assert isinstance(other, BasicStatement) return other < self def __ge__(self, other): # -- SEE ALSO: python2.7, functools.total_ordering # OR: return self >= other return not self < other # pylint: disable=unneeded-not
def test_bool_conversion__returns_false_without_captured_output(self): captured = Captured() assert bool(captured) == False
def test_default_ctor(self): captured = Captured() assert captured.stdout == u"" assert captured.stderr == u"" assert captured.log_output == u"" assert not captured
class BasicStatement(object): def __init__(self, filename, line, keyword, name): filename = filename or '<string>' filename = os.path.relpath(filename, os.getcwd()) # -- NEEDS: abspath? self.location = FileLocation(filename, line) assert isinstance(keyword, six.text_type) assert isinstance(name, six.text_type) self.keyword = keyword self.name = name # -- SINCE: 1.2.6 self.captured = Captured() # -- ERROR CONTEXT INFO: self.exception = None self.exc_traceback = None self.error_message = None @property def filename(self): # return os.path.abspath(self.location.filename) return self.location.filename @property def line(self): return self.location.line def reset(self): # -- RESET: Captured output data self.captured.reset() # -- RESET: ERROR CONTEXT INFO self.exception = None self.exc_traceback = None self.error_message = None def send_status(self): """Emit the volatile attributes of this model in a primitive dict """ ret = { 'exception': self.exception, 'error_message': self.error_message, 'exc_traceback': self.exc_traceback, 'captured': self.captured.send_status() } return ret def recv_status(self, value): """Set volatile attributes from a `send_status()` primitive value """ for key in 'exception', 'error_message', 'exc_traceback': if key in value: setattr(self, key, value[key]) if 'captured' in value: self.captured.recv_status(value['captured']) def store_exception_context(self, exception): self.exception = exception self.exc_traceback = traceback.format_tb(sys.exc_info()[2]) def __hash__(self): # -- NEEDED-FOR: PYTHON3 # return id((self.keyword, self.name)) return id(self) def __eq__(self, other): # -- PYTHON3 SUPPORT, ORDERABLE: # NOTE: Ignore potential FileLocation differences. return (self.keyword, self.name) == (other.keyword, other.name) def __lt__(self, other): # -- PYTHON3 SUPPORT, ORDERABLE: # NOTE: Ignore potential FileLocation differences. return (self.keyword, self.name) < (other.keyword, other.name) def __ne__(self, other): return not self.__eq__(other) def __le__(self, other): # -- SEE ALSO: python2.7, functools.total_ordering # return not other < self return other >= self def __gt__(self, other): # -- SEE ALSO: python2.7, functools.total_ordering assert isinstance(other, BasicStatement) return other < self def __ge__(self, other): # -- SEE ALSO: python2.7, functools.total_ordering # OR: return self >= other return not self < other # pylint: disable=unneeded-not
def test_bool_conversion__returns_true_with_captured_output(self, params): captured = Captured(**params) assert bool(captured)
def test_output__contains_concatenated_parts(self, params, expected): captured = Captured(**params) assert captured.output == expected