hbutils.testing.capture¶
- Overview:
Utilities for capturing different kinds of results, which can be useful in testing.
capture_output¶
-
hbutils.testing.capture.
capture_output
(mem: bool = False) → AbstractContextManager[hbutils.testing.capture.output.OutputCaptureResult][source]¶ - Overview:
Capture all the output to
sys.stdout
andsys.stderr
in thiswith
block.
- Parameters:
mem – Use memory to put the result or not. Default is
False
which means the output will be redirected to temporary files.
- Examples::
>>> from hbutils.testing import capture_output >>> import sys >>> >>> with capture_output() as r: ... print('this is stdout') ... print('this is stderr', file=sys.stderr) ... >>> r.stdout 'this is stdout\n' >>> r.stderr 'this is stderr\n'
Note
When
mem
is set toTrue
,io.StringIO
is used, which do not havefileno
method. This may cause some problems in some cases (such assubprocess.run()
).OutputCaptureResult¶
-
class
hbutils.testing.capture.
OutputCaptureResult
[source]¶ - Overview:
Result model of output capturing.
-
__init__
()[source]¶ Constructor of
OutputCaptureResult
.
-
put_result
(stdout: Optional[str], stderr: Optional[str])[source]¶ Put result inside this model.
- Parameters:
stdout – Stdout result.
stderr – Stderr result.
-
property
stderr
¶ Stderr of the output result.
Note
Do not use this property when
capture_output()
’s with block is not quited, or this property will be jammed due to the deadlock inside.-
property
stdout
¶ Stdout of the output result.
Note
Do not use this property when
capture_output()
’s with block is not quited, or this property will be jammed due to the deadlock inside.disable_output¶
-
hbutils.testing.capture.
disable_output
() → AbstractContextManager[hbutils.testing.capture.output.OutputCaptureResult][source]¶ - Overview:
Disable all the output to
sys.stdout
andsys.stderr
in thiswith
block.- Examples::
>>> import sys >>> from hbutils.testing import disable_output >>> >>> with disable_output(): # no output will be shown ... print('this is stdout') ... print('this is stderr', file=sys.stderr)
capture_exit¶
-
hbutils.testing.capture.
capture_exit
(default=0) → AbstractContextManager[hbutils.testing.capture.exit.ExitCaptureResult][source]¶ - Overview:
Capture for exitcode,
quit()
andsys.exit()
can be captured.
- Parameters:
default – Default exitcode, default is
0
.
- Examples::
>>> from hbutils.testing import capture_exit >>> with capture_exit() as ex: ... pass >>> ex.exitcode 0 >>> >>> with capture_exit() as ex: ... quit() >>> ex.exitcode 0 >>> >>> with capture_exit() as ex: ... quit(0xf) >>> ex.exitcode 15 >>> >>> import sys >>> with capture_exit() as ex: ... sys.exit(0xf) >>> ex.exitcode 15
ExitCaptureResult¶
-
class
hbutils.testing.capture.
ExitCaptureResult
(exitcode)[source]¶ - Overview:
Model of exit capture result.
-
__init__
(exitcode)[source]¶ Constructor of
ExitCaptureResult
.- Parameters:
exitcode – Exitcode value.
-
property
exitcode
¶ Exitcode value.
Note
Do not use this property when
capture_exit()
is not over, otherwise this result is not guaranteed to be correct.
-
-
property