0

I am writing test cases for an API that I have written using Django. I am calling that API in my test and then analysing the response, something like this:

from django.test import Client, TestCase
class MyTest(TestCase):
    fixtures = ['model1.json', 'model2.json']

    def test_some_case(self):
        client = Client()
        request_data = {
            'path': '/internal/api/v1/some-url',
            'data': {"key": "value"},
            'content_type': "application/json"
        }
        response = client.post(**request_data)
        assert response.status_code is not None

The API call (some-url) gets a redis client and creates an entry something like this:

client = MyRedis().get_redis_client()
client.setex(name=some_id, value=some_value,
    time=some_expiry_time)

But as soon as I get the client, the test cases would keep running indefinitely, even after it has passed. If I comment out all code related to Redis in the API implementation, then the test runs as expected. This is a screenshot from IDE: You can see test passed but process still running This is causing my test time-out during during deployments.

Kundan Kumar
  • 471
  • 2
  • 6
  • 10
  • 1
    It's a unit test. You have to mock all external dependencies like Redis anyways. – Klaus D. Mar 02 '22 at 06:16
  • 1
    Maybe need to "shutdown" the redis client? ref: https://stackoverflow.com/questions/58505015/redis-py-not-closing-threads-on-exit – rogerdpack Mar 02 '22 at 06:18
  • 1
    Thanks Klaus D. I finally ended up mocking the __init__ method and the method that returned the Redis Client. – Kundan Kumar Mar 03 '22 at 08:30

0 Answers0