-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support doctest #123
Comments
I think import doctest
import logging
logger = logging.getLogger(__name__)
def foo() -> None:
"""
This is foo.
>>> foo()
>>> logot.assert_logged(logged.info("foo"))
"""
logger.info("foo")
if __name__ == "__main__":
import doctest
from logot import Logot, logged
with Logot().capturing() as logot:
doctest.testmod(verbose=True) I don't use import doctest
import logging
logger = logging.getLogger(__name__)
def foo() -> None:
"""
This is foo.
>>> foo()
>>> logot.assert_logged(logged.info("foo"))
"""
logger.info("foo")
if __name__ == "__main__":
import doctest
from logot.doctest import testmod
testmod(verbose=True) Is that the sort of thing you were thinking of? |
Looking at the So while it would be possible to make a simple I think you can do a lot using the existing support in my first example. And that at least demonstrates that log capture workes fine in doctests! If there's anything else you think could be added to make things better, I'm all ears! 🙇 |
It works great so far! I had this code before: def myfun():
>>> import logging, sys; handler = logging.StreamHandler(sys.stdout)
>>> from logsetup.logsetup import get_log_object # a convenience function
>>> logger = get_log_object(__name__)
>>> logger.addHandler(handler)
>>> logger.setLevel(logging.DEBUG)
>>> # do the stuff
WHAT WE EXPECT
AS OUTPUT
>>> logger.removeHandler(handler) # mandatory cleanup
... The difficulty comes when running modules not via # Top of file
if __sut__ or '--test' in sys.argv: # code put here to reuse in tests.py, otherwise keep it at bottom test block
from logot import Logot, logged
logot_capture = Logot().capturing
logot = []
def myfunc():
>>> logot = logot[0]
...
# End of file
if __sut__:
import doctest
with logot_capture() as _logot: logot.append(_logot); doctest.testmod(optionflags=doctest.IGNORE_EXCEPTION_DETAIL) and somewhere else dt_runner = doctest.DocTestRunner(optionflags=doctest.IGNORE_EXCEPTION_DETAIL|doctest.ELLIPSIS)
with logot_capture() as _logot:
logot.append(_logot)
for t in tests: failed, attempted = dt_runner.run(t); errors += failed; success += 1 if failed == 0 else 0 This ensures there's a cross-module writable global variable which can be used inside doctest, even when the logot variable is defined in another module. The reason is that I wasn't able to use logot without the context manager ( If there is another way to use it, that would simplify it very much. |
Hmm. So maybe it would be possible to have an api like: logot = Logot.get_current() This would return a reference to the |
Alternatively, a
|
I like the |
I try to use doctest only in my code (with occasional
unittest.DoctestRunner
) and it's always a hassle to capture log output, because doctest clones the logger instances transparently (or something).The text was updated successfully, but these errors were encountered: