Skip to content

Convert logging usage doctests to testable snippets - #2556

Closed
tseaver wants to merge 0 commit into
googleapis:masterfrom
tseaver:logging-snippets
Closed

Convert logging usage doctests to testable snippets#2556
tseaver wants to merge 0 commit into
googleapis:masterfrom
tseaver:logging-snippets

Conversation

@tseaver

Copy link
Copy Markdown
Contributor

Toward #212 / #2535.

@tseavertseaver added docs api: logging Issues related to the Cloud Logging API. labels Oct 17, 2016
@googlebotgooglebot added the cla: yes This human has signed the Contributor License Agreement. label Oct 17, 2016

@dhermesdhermes left a comment

Copy link
Copy Markdown
Contributor

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?

Comment threaddocs/logging_snippets.py Outdated
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.

Comment threaddocs/logging_snippets.py Outdated
# [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.

This comment was marked as spam.

Comment threaddocs/logging_snippets.py Outdated
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.

This comment was marked as spam.

This comment was marked as spam.

Comment threaddocs/logging_snippets.py Outdated
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.

@theacodestheacodes left a comment

Copy link
Copy Markdown
Contributor

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.

@daspecster

daspecster commented Oct 17, 2016

Copy link
Copy Markdown
Contributor

Fixture example from python-docs-samples.

@pytest.fixturedefexample_log():
client=logging.Client()
logger=client.logger(TEST_LOGGER_NAME)
text='Hello, world.'logger.log_text(text)
returntext

This looks pretty nice!
http://doc.pytest.org/en/latest/fixture.html

@dhermes

dhermes commented Oct 25, 2016

Copy link
Copy Markdown
Contributor

@tseaver Here is a proof of concept for running readable interpreter snippets without worrying about calling eval / exec etc.

First, create a fake module (or any object really) with our content in the __doc__

>>>importtypes>>>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 doctest.run_docstring_examples

>>>importdoctest>>>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')
FindingtestsinfooTrying:
a=45ExpectingnothingokTrying:
a/3Expecting:
15okTrying:
b= {}
ExpectingnothingokTrying:
v=b.get('anything')
ExpectingnothingokTrying:
vExpectingnothingokTrying:
print(v)
Expecting:
Noneok

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: loggingIssues related to the Cloud Logging API.cla: yesThis human has signed the Contributor License Agreement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants

@tseaver@daspecster@dhermes@theacodes@googlebot