Skip to content

Instantly share code, notes, and snippets.

@hpk42
Created October 13, 2013 20:12
Show Gist options
  • Save hpk42/6966904 to your computer and use it in GitHub Desktop.
Save hpk42/6966904 to your computer and use it in GitHub Desktop.
etamax_cassette = pytest.mark.betamax_cassette
class TestGitHubAPI:
def test_user(self, session, vcr):
vcr.use_cassette('user')
resp = session.get('https://api.github.com/user',
auth=('user', 'pass'))
assert resp.json()['login'] is not None
@betamax_cassette("repo")
def test_repo(self, session):
resp = session.get(
'https://api.github.com/repos/sigmavirus24/github3.py'
)
assert resp.json()['owner'] != {}
# or if we want to use recording for each method with automatically
# named cassettes
@pytest.mark.usefixtures("betamax_per_method") # use method-named cassettes
class TestGitHubAPIAuto:
def test_login(self, session): # use cassete TestGitHubAPIAuto.login
resp = session.get('https://api.github.com/user',
auth=('user', 'pass'))
assert resp.json()['login'] is not None
def test_repo(self, session): # use cassette TestGitHubAPIAuto.repo
resp = session.get(
'https://api.github.com/repos/sigmavirus24/github3.py'
)
assert resp.json()['owner'] != {}
@sigmavirus24
Copy link

So in the case of github3.py (my main use-case of betamax), I store a requests session on the object, i.e.,

import github3

g = github3.GitHub()
g._session

What I started doing was:

class TestGitHub(unittest.TestCase):
    def setUp(self):
        self.gh = github3.GitHub()
        self.session = self.gh._session

    def test_get_user(self):
        with Betamax(self.session).use_cassette('TestGitHub.get_user'):
            u = self.gh.user('hpk42')
            assert u.login == 'hpk42'
            # etc.

How could fixtures help in this case?

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