-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Convert logging usage doctests to testable snippets #2556
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jonparrott Can you also peek at these?
do_something_with(entry) | ||
if token is None: | ||
break | ||
entries, token = logger.list_entries(page_token=token) # API request |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
# [END logger_list_entries] | ||
|
||
to_delete.remove(logger) | ||
backoff = [1, 2, 4, 8] |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
filter_=FILTER, description=DESCRIPTIION) | ||
assert not new_metric.exists() # API request | ||
new_metric.create() # API request | ||
assert new_metric.exists() # API request |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
except AssertionError as e: | ||
print(' FAIL: %s' % (e,)) | ||
except Exception as e: # pylint: disable=broad-except | ||
print(' ERROR: %r' % (e,)) |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks okay to me as a start, but it is concerning to see so much setup/cleanup logic in the snippet function body. Pytest fixtures can help a lot with that, or just having separate test functions for each snippet. We do both in python-docs-samples
.
Fixture example from @pytest.fixture
def example_log():
client = logging.Client()
logger = client.logger(TEST_LOGGER_NAME)
text = 'Hello, world.'
logger.log_text(text)
return text This looks pretty nice! |
@tseaver Here is a proof of concept for running readable interpreter snippets without worrying about calling First, create a fake module (or any object really) with our content in the >>> import types
>>> fake_mod = types.ModuleType('haha-nope')
>>> fake_mod.__doc__ = """\
... Here is some prose, and then a dragon appears
... and it makes sure division::
...
... >>> a = 45
... >>> a / 3
... 15
...
... and then the bunny hops down the lane and we
... find out we have to print a null::
...
... >>> b = {}
... >>> v = b.get('anything')
... >>> v
... >>> print(v)
... None
... """ then hand that object off to >>> import doctest
>>> fake_globals = {}
>>> doctest.run_docstring_examples(fake_mod, fake_globals, verbose=False, name='foo')
>>> doctest.run_docstring_examples(fake_mod, fake_globals, verbose=True, name='foo')
Finding tests in foo
Trying:
a = 45
Expecting nothing
ok
Trying:
a / 3
Expecting:
15
ok
Trying:
b = {}
Expecting nothing
ok
Trying:
v = b.get('anything')
Expecting nothing
ok
Trying:
v
Expecting nothing
ok
Trying:
print(v)
Expecting:
None
ok |
Toward #212 / #2535.