Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throttling causes cache sharing issues between test cases #1158

Open
JungHoon99 opened this issue Oct 22, 2024 · 0 comments
Open

Throttling causes cache sharing issues between test cases #1158

JungHoon99 opened this issue Oct 22, 2024 · 0 comments

Comments

@JungHoon99
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant