Python package to intercept all external api calls during django test.
Project description
Django Blip
Django Blip is a python package that mocks any external API(s) call when running Django test(s). It allows to set global
mocked response and status code for these external API(s) calls. Notably, if you've previously used @httpretty.activate
to mock a test, Django Blip will not overwrite that behavior, ensuring backward compatibility.
How to configure:
- pip install
django-blip
to install in your virtual env. - Add
TEST_RUNNER = "blip.custom_test_runner.BlipTestRunner"
in your Django'ssettings.py
.
Optionally
3) Add,
BLIP_CONFIG = {
"blip_status_code": 500, # Default 200
"blip_response": '{"key": "value"}', # Default {}
"blip_verbose": False, # Default True
"blip_silently_bypass": False # Default True, If set to False, it will raise an exception if any
# external API call is encountered within the test
}
in your settings.py
.
Basic Usage
import httpretty
from django.test import TestCase
import requests
import json
class TestBlipWorks(TestCase):
@httpretty.activate(verbose=True, allow_net_connect=False)
def test_blip_do_not_alter_the_behaviour_of_existing_mocked_apis_with_httpretty(self):
url = "https://google.com"
httpretty.register_uri(httpretty.GET, url, body=json.dumps({"key": "value"}), status=500)
# blip's default behaviour is it mocks any api call and return status code = 200 and response={}
# this test will ensure blip don't override existing test which is already decorated with @httpretty.activate
res = requests.get(url)
self.assertEqual(res.json()["key"], "value") # blip returns response = {}
self.assertEqual(res.status_code, 500) # blip returns default 200 status code
def test_blip_works_and_mock_any_api_call(self):
url = "https://google.com"
# blip's default behaviour is it mocks any api call and return status = 200 and response={}
res = requests.get(url)
self.assertEqual(res.json(), {}) # blip returns response = {}
self.assertEqual(res.status_code, 200) # blip returns default 200 status code
In the provided code examples, test_blip_works_and_mock_any_api_call shows that Blip returns a mocked status_code of 200 and an empty response {}. Additionally, in test_blip_do_not_alter_the_behaviour_of_existing_mocked_apis_with_httpretty, you can see how Blip is designed to work seamlessly with @httpretty.activate decorators, ensuring that it doesn't override existing behaviors.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for django_blip-0.0.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ed81ad9bdfdcf22f0cd1c9f28e06ff445052a305a35d5b0c9e6563e2800bfa04 |
|
MD5 | cfb50caa534fad9673c0092c15854715 |
|
BLAKE2b-256 | e8828a73bf5c7b87409036e339c34113a8d77dd6f241537529bd6f30849d37db |