├── .gitignore ├── MANIFEST.in ├── README.md ├── pytest_httpretty.py ├── setup.py ├── test_pytest_httpretty.py └── tox.ini /.gitignore: -------------------------------------------------------------------------------- 1 | *.egg-info 2 | *.pyc 3 | 4 | /.tox 5 | /dist 6 | -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | include README.md 2 | include test_pytest_httpretty.py 3 | include tox.ini 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # pytest-httpretty - A thin wrapper of HTTPretty for pytest 2 | 3 | pytest-httpretty provides `httpretty` marker and `stub_get()` shorthand function. 4 | 5 | ```python 6 | import httpretty 7 | import pytest 8 | import requests 9 | from pytest_httpretty import stub_get 10 | 11 | 12 | @pytest.mark.httpretty 13 | def test_mark_httpretty(): 14 | httpretty.register_uri(httpretty.GET, 'http://example.com/', body='Hello') 15 | 16 | assert requests.get('http://example.com').text == 'Hello' 17 | 18 | 19 | @pytest.mark.httpretty 20 | def test_stub_get(): 21 | stub_get('http://example.com/', body='World!') 22 | 23 | assert requests.get('http://example.com').text == 'World!' 24 | ``` 25 | -------------------------------------------------------------------------------- /pytest_httpretty.py: -------------------------------------------------------------------------------- 1 | import functools 2 | 3 | import httpretty 4 | 5 | 6 | __version__ = '0.2.0' 7 | 8 | 9 | def pytest_configure(config): 10 | config.addinivalue_line('markers', 11 | 'httpretty: mark tests to activate HTTPretty.') 12 | 13 | 14 | def pytest_runtest_setup(item): 15 | marker = item.get_marker('httpretty') 16 | if marker is not None: 17 | httpretty.reset() 18 | httpretty.enable() 19 | 20 | 21 | def pytest_runtest_teardown(item, nextitem): 22 | marker = item.get_marker('httpretty') 23 | if marker is not None: 24 | httpretty.disable() 25 | 26 | 27 | stub_get = functools.partial(httpretty.register_uri, httpretty.GET) 28 | 29 | last_request = httpretty.last_request 30 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | import os.path 2 | 3 | from setuptools import setup 4 | 5 | 6 | here = os.path.dirname(os.path.abspath(__file__)) 7 | version = next((line.split('=')[1].strip().replace("'", '') 8 | for line in open(os.path.join(here, 'pytest_httpretty.py')) 9 | if line.startswith('__version__ = ')), 10 | '0.0.0.dev0') 11 | 12 | 13 | setup(name='pytest-httpretty', 14 | version=version, 15 | description='A thin wrapper of HTTPretty for pytest', 16 | author='papaeye', 17 | author_email='papaeye@gmail.com', 18 | url='http://github.com/papaeye/pytest-httpretty', 19 | py_modules=['pytest_httpretty'], 20 | include_package_data=True, 21 | install_requires=[ 22 | 'httpretty', 23 | 'pytest', 24 | ], 25 | tests_require=[ 26 | 'requests' 27 | ], 28 | zip_safe=False, 29 | keywords='pytest httpretty http stub mock', 30 | classifiers=[ 31 | 'Development Status :: 3 - Alpha', 32 | 'License :: OSI Approved :: BSD License', 33 | 'Operating System :: OS Independent', 34 | 'Programming Language :: Python', 35 | 'Programming Language :: Python :: 2', 36 | 'Programming Language :: Python :: 2.7', 37 | 'Programming Language :: Python :: 3', 38 | 'Programming Language :: Python :: 3.3', 39 | ], 40 | platforms='any', 41 | license='BSD License', 42 | entry_points={ 43 | 'pytest11': ['httpretty = pytest_httpretty'] 44 | }) 45 | -------------------------------------------------------------------------------- /test_pytest_httpretty.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | import httpretty 4 | import pytest 5 | import requests 6 | 7 | from pytest_httpretty import stub_get 8 | 9 | 10 | @pytest.fixture 11 | def dummy(): 12 | return 13 | 14 | 15 | @httpretty.activate 16 | def test_httpretty_activate(): 17 | httpretty.register_uri(httpretty.GET, 'http://example.com/', body='Hello') 18 | 19 | assert requests.get('http://example.com').text == 'Hello' 20 | 21 | 22 | @pytest.mark.xfail(sys.version_info[0] == 2, 23 | reason=('httpretty.activate() does not work with pytest ' 24 | 'fixtures in Python 2')) 25 | @httpretty.activate 26 | def test_httpretty_activate_with_pytest_fixtures(dummy): 27 | httpretty.register_uri(httpretty.GET, 'http://example.com/', body='Hello') 28 | 29 | assert requests.get('http://example.com').text == 'Hello' 30 | 31 | 32 | @pytest.mark.httpretty 33 | def test_mark_httpretty(dummy): 34 | httpretty.register_uri(httpretty.GET, 'http://example.com/', body='Hello') 35 | 36 | assert requests.get('http://example.com').text == 'Hello' 37 | 38 | 39 | @pytest.mark.httpretty 40 | def test_stub_get(dummy): 41 | stub_get('http://example.com/', body='World!') 42 | 43 | assert requests.get('http://example.com').text == 'World!' 44 | -------------------------------------------------------------------------------- /tox.ini: -------------------------------------------------------------------------------- 1 | [tox] 2 | envlist = py27, py33 3 | 4 | [testenv] 5 | deps = 6 | httpretty 7 | pytest 8 | requests 9 | commands = py.test 10 | --------------------------------------------------------------------------------