-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #67 from sigmavirus24/test-fixtures
Add a pytest fixture for Sessions
- Loading branch information
Showing
9 changed files
with
185 additions
and
3 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# -*- coding: utf-8 -*- | ||
"""A set of fixtures to integrate Betamax with py.test. | ||
.. autofunction:: betamax_session | ||
""" | ||
|
||
from __future__ import absolute_import | ||
|
||
import pytest | ||
import requests | ||
|
||
from .. import recorder as betamax | ||
|
||
|
||
@pytest.fixture | ||
def betamax_session(request): | ||
"""Generate a session that has Betamax already installed. | ||
This will create a new :class:`requests.Sesssion` instance that is already | ||
using Betamax with a generated cassette name. The cassette name is | ||
generated by first using the module name from where the test is collected, | ||
then the class name (if it exists), and then the test function name. For | ||
example, if your test is in ``test_stuff.py`` and is the method | ||
``TestStuffClass.test_stuff`` then your cassette name will be | ||
``test_stuff_TestStuffClass_test_stuff``. | ||
:param request: | ||
A request object from pytest giving us context information for the | ||
fixture. | ||
:returns: | ||
An instantiated requests Session wrapped by Betamax. | ||
""" | ||
cassette_name = '' | ||
|
||
if request.module is not None: | ||
cassette_name += request.module.__name__ + '.' | ||
|
||
if request.cls is not None: | ||
cassette_name += request.cls.__name__ + '.' | ||
|
||
cassette_name += request.function.__name__ | ||
|
||
session = requests.Session() | ||
recorder = betamax.Betamax(session) | ||
recorder.use_cassette(cassette_name) | ||
recorder.start() | ||
request.addfinalizer(recorder.stop) | ||
return session |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
tests/cassettes/tests.integration.test_fixtures.TestPyTestFixtures.test_pytest_fixture.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"recorded_with": "betamax/0.4.2", "http_interactions": [{"recorded_at": "2015-05-25T00:46:42", "response": {"body": {"encoding": null, "string": "{\n \"args\": {}, \n \"headers\": {\n \"Accept\": \"*/*\", \n \"Accept-Encoding\": \"gzip, deflate\", \n \"Host\": \"httpbin.org\", \n \"User-Agent\": \"python-requests/2.6.0 CPython/3.4.2 Darwin/14.1.0\"\n }, \n \"origin\": \"72.160.201.47\", \n \"url\": \"https://httpbin.org/get\"\n}\n"}, "status": {"message": "OK", "code": 200}, "url": "https://httpbin.org/get", "headers": {"connection": ["keep-alive"], "content-type": ["application/json"], "content-length": ["266"], "date": ["Mon, 25 May 2015 00:46:42 GMT"], "access-control-allow-origin": ["*"], "access-control-allow-credentials": ["true"], "server": ["nginx"]}}, "request": {"method": "GET", "body": {"encoding": "utf-8", "string": ""}, "uri": "https://httpbin.org/get", "headers": {"Connection": ["keep-alive"], "User-Agent": ["python-requests/2.6.0 CPython/3.4.2 Darwin/14.1.0"], "Accept": ["*/*"], "Accept-Encoding": ["gzip, deflate"]}}}]} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import os.path | ||
|
||
import pytest | ||
|
||
|
||
@pytest.mark.usefixtures('betamax_session') | ||
class TestPyTestFixtures: | ||
@pytest.fixture(autouse=True) | ||
def setup(self, request): | ||
"""After test hook to assert everything.""" | ||
def finalizer(): | ||
test_dir = os.path.abspath('.') | ||
cassette_name = ('tests.integration.test_fixtures.' # Module name | ||
'TestPyTestFixtures.' # Class name | ||
'test_pytest_fixture' # Test function name | ||
'.json') | ||
file_name = os.path.join(test_dir, 'tests', 'cassettes', | ||
cassette_name) | ||
assert os.path.exists(file_name) is True | ||
|
||
request.addfinalizer(finalizer) | ||
|
||
def test_pytest_fixture(self, betamax_session): | ||
"""Exercise the fixture itself.""" | ||
resp = betamax_session.get('https://httpbin.org/get') | ||
assert resp.ok |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
try: | ||
import unittest.mock as mock | ||
except ImportError: | ||
import mock | ||
|
||
import unittest | ||
|
||
import betamax | ||
from betamax.fixtures import pytest as pytest_fixture | ||
|
||
|
||
class TestPyTestFixture(unittest.TestCase): | ||
def setUp(self): | ||
self.mocked_betamax = mock.MagicMock() | ||
self.patched_betamax = mock.patch.object( | ||
betamax.recorder, 'Betamax', return_value=self.mocked_betamax) | ||
self.patched_betamax.start() | ||
|
||
def tearDown(self): | ||
self.patched_betamax.stop() | ||
|
||
def test_adds_stop_as_a_finalizer(self): | ||
# Mock a pytest request object | ||
request = mock.MagicMock() | ||
request.cls = request.module = None | ||
request.function.__name__ = 'test' | ||
|
||
pytest_fixture.betamax_session(request) | ||
assert request.addfinalizer.called is True | ||
request.addfinalizer.assert_called_once_with(self.mocked_betamax.stop) | ||
|
||
def test_auto_starts_the_recorder(self): | ||
# Mock a pytest request object | ||
request = mock.MagicMock() | ||
request.cls = request.module = None | ||
request.function.__name__ = 'test' | ||
|
||
pytest_fixture.betamax_session(request) | ||
self.mocked_betamax.start.assert_called_once_with() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters