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

Add session property to Pill class #31

Closed
vlcinsky opened this issue Feb 17, 2016 · 2 comments
Closed

Add session property to Pill class #31

vlcinsky opened this issue Feb 17, 2016 · 2 comments

Comments

@vlcinsky
Copy link

Trying to use placebo in my pytest based suite, I have found, that in some scenarions (recording responses) I have forgotten to pill.stop() recording. For this reason my recording test was not failing when it was supposed to (when I disconnected from Internet).

For this reason I have decided to use @pytest.fixture called recording_pill, which at teardown calls pill.stop().

Anyway, I have found, I need to get the session bound to the pill and the only way to do it now is to ask pill._session breaking the rule "we are all adults" and accessed the private property.

It would be handy to have (possibly read-only) session property on the Pill class.

Here is my current test suite tests/test_logs.py:

import pytest


@pytest.fixture
def session():
    import boto3
    return boto3.Session()


@pytest.fixture(scope="session")
def records_path(tmpdir_factory):
    """(py.path.local) directory for recorded responses"""
    path = tmpdir_factory.mktemp("logs")
    path.ensure_dir()
    return path


@pytest.yield_fixture
def recording_pill(session, records_path):
    import placebo
    pill = placebo.attach(session, data_path=records_path.strpath)
    pill.record()
    yield pill
    # tear down
    pill.stop()


@pytest.yield_fixture
def replaying_pill(session, records_path):
    import placebo
    pill = placebo.attach(session, data_path=records_path.strpath)
    pill.playback()
    yield pill
    # tear down
    pill.stop()


def get_groups(client):
        paginator = client.get_paginator('describe_log_groups')
        for page in paginator.paginate():
            for group in page.get('logGroups', []):
                yield group['logGroupName']


def test_recording(recording_pill):
    session = recording_pill._session  # better access public property here
    client = session.client("logs")
    groups = list(get_groups(client))
    assert groups


def test_playback(replaying_pill):
    session = replaying_pill._session  # better access public property here
    client = session.client("logs")
    groups = list(get_groups(client))
    assert groups

I could possibly pass the session value into test_recording and
test_playback, but this is tricky as I really want to be sure, it is exactly
the session the pill was created for.

garnaat added a commit that referenced this issue Feb 17, 2016
Add a property to pill to retrieve the associated session.  Fixes #31.
@garnaat
Copy link
Owner

garnaat commented Feb 17, 2016

Makes sense. Added a property to fetch the session.

@vlcinsky
Copy link
Author

Thanks @garnaat , it was really quick (incl. publishing new version to pypi). I tested it with the latest version and it works as expected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants