Skip to content

Throttling causes cache sharing issues between test cases #1158

Open
@JungHoon99

Description

@JungHoon99

When running tests with pytest-django, I've encountered an issue where cache data is being shared between test cases, especially when testing throttling logic. This causes tests to fail inconsistently, as the cache state from one test affects another test. I suspect the issue lies in the cache not being properly cleared or isolated between tests.

Steps to Reproduce:
Set up Django views with throttling (e.g., using rest_framework.throttling.ScopedRateThrottle).
Create multiple test cases to test the throttling behavior.
Run the test suite with pytest-django.
Expected behavior:
Each test case should start with a clean cache, and throttling logic should not interfere with other test cases.

Actual behavior:
The cache seems to persist across test cases, causing throttling limits to be applied incorrectly to subsequent tests. This results in tests failing due to unexpected throttling responses.

from rest_framework.test import APIClient
import pytest

@pytest.mark.django_db
def test_throttling_case_1():
    client = APIClient()
    response = client.get('/throttled-endpoint/')
    assert response.status_code == 200

@pytest.mark.django_db
def test_throttling_case_2():
    client = APIClient()
    response = client.get('/throttled-endpoint/')
    assert response.status_code == 200

It seems that the cache is not being cleared or reset between tests, which leads to throttling rules being incorrectly applied to test cases that should otherwise pass.

Possible Solution:
A potential solution might be to ensure that the cache is explicitly cleared or isolated between test cases, possibly by adding an automatic cache reset between tests or providing guidance on how to properly configure cache isolation in pytest-django.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions