I implemented a Redis cache in my Flask app using Flask-Caching==1.3.3
, but obviously some of my endpoint unit tests are now failing because the responses are cached, making some POST/PUT tests fail.
Is there some good way to disable the cache during unit tests? I'm using pytest==3.5.0
Eg. this fails because the old entry is returned from cache:
def test_updating_biography(self):
"""Should update the current newest entry with the data in the JSON."""
response = self.app.put(
"/api/1.0/biography/",
data=json.dumps(
dict(
short="UnitTest Updated newest short",
full="UnitTest Updated newest full",
)
),
content_type="application/json"
)
get_bio = self.app.get("/api/1.0/biography/")
biodata = json.loads(get_bio.get_data().decode())
self.assertEquals(200, response.status_code)
self.assertEquals(200, get_bio.status_code)
> self.assertEquals("UnitTest Updated newest short", biodata["biography"][0]["short"])
E AssertionError: 'UnitTest Updated newest short' != 'UnitTest fourth short'
E - UnitTest Updated newest short
E + UnitTest fourth short
tests/biography/test_biography_views.py:100: AssertionError
I tried for example:
def setUp(self):
app.config["CACHE_TYPE"] = None
app.config["CACHE_REDIS_URL"] = ""
self.app = app.test_client()
And also app.config["CACHE_TYPE"] = "null"
and app.config["CACHE_TYPE"] = ""
, but it is still using the cache in the unit test.
I tried this one, but it's of course outside of app context:
@cache.cached(timeout=0)
def test_updating_biography(self):
As mentioned in the comment, sytech's idea works for me since I only test a single app with this redis. Obviously if you use a shared redis for multiple apps, this might not work for you. But for my case, it worked perfectly and can be repeated without issues:
import unittest
from flask_caching import Cache
from app import app, db
class TestBiographyViews(unittest.TestCase):
def setUp(self):
"""Add some test entries to the database, so we can test getting the latest one."""
# Clear redis cache completely
cache = Cache()
cache.init_app(app, config={"CACHE_TYPE": "redis"})
with app.app_context():
cache.clear()
self.app = app.test_client()
The above is all you need. The rest of the test cases can be as normal. Works for me.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With