diff options
| author | Jordan Cook <jordan.cook@pioneer.com> | 2021-04-11 21:26:31 -0500 |
|---|---|---|
| committer | Jordan Cook <jordan.cook@pioneer.com> | 2021-04-12 13:02:01 -0500 |
| commit | 0d1bceed7a3a06dcd49dab63ba72bd2b8ace77fa (patch) | |
| tree | a41b53224d7649a471282cd48dce34ea04e30832 /tests/conftest.py | |
| parent | f3c6eae9d19f5b98b0d39b68198dba9cd433e8ea (diff) | |
| download | requests-cache-0d1bceed7a3a06dcd49dab63ba72bd2b8ace77fa.tar.gz | |
Add option to use pytest-httpbin instead of httpbin container
Diffstat (limited to 'tests/conftest.py')
| -rw-r--r-- | tests/conftest.py | 32 |
1 files changed, 29 insertions, 3 deletions
diff --git a/tests/conftest.py b/tests/conftest.py index 999f456..ad89f01 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -4,8 +4,9 @@ Note: The protocol ``http(s)+mock://`` helps :py:class:`requests_mock.Adapter` p :py:class:`requests.PreparedRequest`. More info here: https://requests-mock.readthedocs.io/en/latest/adapter.html """ +import os import pytest -from os import getenv +from logging import basicConfig, getLogger from tempfile import NamedTemporaryFile import requests @@ -22,11 +23,36 @@ MOCKED_URL_REDIRECT = 'http+mock://requests-cache.com/redirect' MOCKED_URL_REDIRECT_TARGET = 'http+mock://requests-cache.com/redirect_target' MOCK_PROTOCOLS = ['mock://', 'http+mock://', 'https+mock://'] +# Configure logging to show debug output when tests fail (or with pytest -s) +basicConfig(level='INFO') +getLogger('requests_cache').setLevel('DEBUG') +logger = getLogger(__name__) + def httpbin(path): """Get the url for either a local or remote httpbin instance""" - base_url = getenv('HTTPBIN_URL', 'http://localhost:80/') - return base_url + path + base_url = os.getenv('HTTPBIN_URL', 'http://localhost:80').rstrip('/') + return f'{base_url}/{path}' + + +"""The following allows pytest-httpbin to be used instead of the httpbin container. +A server will be started via an autoused fixture if both: +* pytest-httpbin is installed +* The environment variable USE_PYTEST_HTTPBIN is set to 'true' +""" +try: + import pytest_httpbin # noqa: F401 + + USE_PYTEST_HTTPBIN = os.getenv('USE_PYTEST_HTTPBIN', '').lower() == 'true' + logger.info('Using pytest-httpin for integration tests') +except ImportError: + USE_PYTEST_HTTPBIN = False + + +@pytest.fixture(scope='session', autouse=USE_PYTEST_HTTPBIN) +def httpbin_wrapper(httpbin): + os.environ['HTTPBIN_URL'] = httpbin.url + return httpbin @pytest.fixture(scope='function') |
