Created
January 15, 2024 19:40
-
-
Save victory-sokolov/34a452a07f136c5770540c395107d73b to your computer and use it in GitHub Desktop.
Python nplusone inside tests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from functools import wraps | |
from django.test import TestCase | |
def raise_nplusone_decorator(cls): | |
original_setup_class = getattr(cls, 'setUpClass') | |
original_teardown_class = getattr(cls, 'tearDownClass') | |
@classmethod | |
def new_setup_class(cls): | |
# Perform setup steps before the test class | |
with Profiler(): | |
original_setup_class() | |
@classmethod | |
def new_teardown_class(cls): | |
# Perform teardown steps after the test class | |
original_teardown_class() | |
cls.setUpClass = new_setup_class | |
cls.tearDownClass = new_teardown_class | |
return cls | |
@raise_nplusone_decorator | |
class MyTestCase(TestCase): | |
def test_my_functionality(self): | |
# Your test logic goes here | |
self.assertEqual(2 + 2, 4) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment