Skip to content
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

Closed
wants to merge 0 commits into from
Closed

Convert logging usage doctests to testable snippets #2556

wants to merge 0 commits into from

Conversation

tseaver
Copy link
Contributor

@tseaver tseaver commented Oct 17, 2016

Toward #212 / #2535.

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

@dhermes dhermes left a 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.

# [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.

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.

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.

Copy link
Contributor

@theacodes theacodes left a 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.

@daspecster
Copy link
Contributor

daspecster commented Oct 17, 2016

Fixture example from python-docs-samples.

@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!
http://doc.pytest.org/en/latest/fixture.html

@dhermes
Copy link
Contributor

dhermes commented Oct 25, 2016

@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__

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

>>> 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: logging Issues related to the Cloud Logging API. cla: yes This human has signed the Contributor License Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants