-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Swtich fixtures to marker and shorthand function
- Loading branch information
Showing
2 changed files
with
52 additions
and
16 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -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) |
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 |
---|---|---|
@@ -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!' |