summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJordan Cook <jordan.cook@pioneer.com>2021-04-11 21:26:31 -0500
committerJordan Cook <jordan.cook@pioneer.com>2021-04-12 13:02:01 -0500
commit0d1bceed7a3a06dcd49dab63ba72bd2b8ace77fa (patch)
treea41b53224d7649a471282cd48dce34ea04e30832
parentf3c6eae9d19f5b98b0d39b68198dba9cd433e8ea (diff)
downloadrequests-cache-0d1bceed7a3a06dcd49dab63ba72bd2b8ace77fa.tar.gz
Add option to use pytest-httpbin instead of httpbin container
-rw-r--r--CONTRIBUTING.md22
-rw-r--r--tests/conftest.py32
2 files changed, 47 insertions, 7 deletions
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index 72c7539..0f68c92 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -58,10 +58,10 @@ $ pre-commit uninstall
* Run `./runtests.sh` to run all tests with some useful options for test coverage reports,
multiprocessing, and debugging.
-### Integration Tests
-Live databases are required to run integration tests, and docker-compose config is included to make
-this easier. First, [install docker](https://docs.docker.com/get-docker/) and
-[install docker-compose](https://docs.docker.com/compose/install/).
+### Integration Test Containers
+A live web server and backend databases are required to run integration tests, and docker-compose
+config is included to make this easier. First, [install docker](https://docs.docker.com/get-docker/)
+and [install docker-compose](https://docs.docker.com/compose/install/).
Then, run:
```bash
@@ -69,6 +69,20 @@ $ docker-compose up -d
pytest tests/integration
```
+### Integration Test Alternatives
+If you can't easily run Docker containers in your environment but still want to run some of the
+integration tests, you can use [pytest-httpbin](https://github.com/kevin1024/pytest-httpbin) instead
+of the httpbin container. This just requires installing an extra package and setting an environment
+variable:
+```bash
+pip install pytest-httpbin
+export USE_PYTEST_HTTPBIN=true
+pytest tests/integration/test_cache.py
+```
+
+For backend databases, you can install and run them on the host instead of in a container, as long
+as they are running on the default port.
+
## Debugging
When you run into issues while working on new features and/or tests, it will make your life much easier
to use a debugger instead of `print` statements. Most IDEs have a built-in debugger, but if
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')