Skip to content

Commit

Permalink
Swtich fixtures to marker and shorthand function
Browse files Browse the repository at this point in the history
  • Loading branch information
papaeye committed Jan 16, 2014
1 parent f5eea4e commit bded8f1
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 16 deletions.
34 changes: 20 additions & 14 deletions pytest_httpretty.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
import httpretty as _httpretty
import pytest
import functools

import httpretty


__version__ = '0.1.dev'


@pytest.fixture
def httpretty(request):
_httpretty.httpretty.reset()
_httpretty.httpretty.enable()
request.addfinalizer(_httpretty.httpretty.disable)
return _httpretty
def pytest_configure(config):
config.addinivalue_line('markers',
'httpretty: mark tests to activate HTTPretty.')


def pytest_runtest_setup(item):
marker = item.get_marker('httpretty')
if marker is not None:
httpretty.reset()
httpretty.enable()


def pytest_runtest_teardown(item, nextitem):
marker = item.get_marker('httpretty')
if marker is not None:
httpretty.disable()


@pytest.fixture
def stub_get(request, httpretty):
def wrapper(*args, **kwargs):
httpretty.register_uri(httpretty.GET, *args, **kwargs)
return httpretty
return wrapper
stub_get = functools.partial(httpretty.register_uri, httpretty.GET)
34 changes: 32 additions & 2 deletions test_pytest_httpretty.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,43 @@
import sys

import httpretty
import pytest
import requests

from pytest_httpretty import stub_get


@pytest.fixture
def dummy():
return


@httpretty.activate
def test_httpretty_activate():
httpretty.register_uri(httpretty.GET, 'http://example.com/', body='Hello')

assert requests.get('http://example.com').text == 'Hello'


@pytest.mark.xfail(sys.version_info[0] == 2,
reason=('httpretty.activate() does not work with pytest '
'fixtures in Python 2'))
@httpretty.activate
def test_httpretty_activate_with_pytest_fixtures(dummy):
httpretty.register_uri(httpretty.GET, 'http://example.com/', body='Hello')

assert requests.get('http://example.com').text == 'Hello'


def test_httpretty(httpretty):
@pytest.mark.httpretty
def test_mark_httpretty(dummy):
httpretty.register_uri(httpretty.GET, 'http://example.com/', body='Hello')

assert requests.get('http://example.com').text == 'Hello'


def test_stub_get(stub_get):
@pytest.mark.httpretty
def test_stub_get(dummy):
stub_get('http://example.com/', body='World!')

assert requests.get('http://example.com').text == 'World!'

0 comments on commit bded8f1

Please sign in to comment.