Skip to content

Refactor Django Views Tests to use setUpTestData for Performance Improvement #389

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

Closed
wants to merge 4 commits into from

Conversation

Itz-Agasta
Copy link
Contributor

closes #369

Refactored Django test classes to use setUpTestData() instead of setUp() for database object creation, following Django's official testing best practices for improved performance.

Changes Made

Files Modified:

  1. test_views.py (Primary target)
  2. buffalogs/impossible_travel/tests/alerting/test_alert_googlechat.py (Demonstration)

Key Refactoring:

  • Before: Database objects created in setUp() method (runs before every test)
  • After: Database objects moved to @classmethod setUpTestData() (runs once per test class)
  • Maintained: Per-test setup (like Client() instantiation) remains in setUp()

Performance Impact (test_views.py)

Metric Before (setUp) After (setUpTestData) Improvement
Test execution time ~0.30s ~0.25s 17% faster
Total runtime ~1.8s ~1.7s 6% faster
Database operations 195 objects (15×13) 15 objects (1×15) 92% reduction

Testing & Validation

  • ✅ All 13 tests in test_views.py pass
  • ✅ All 4 tests in test_alert_googlechat.py pass
  • ✅ Test isolation maintained (no shared state issues)
  • ✅ Performance improvements verified with timing measurements

This implementation follows the Django Testing Documentation guidelines:

"If you need to create objects in setUpTestData, you should use cls to reference them throughout the test class rather than creating objects in setUp()."

@Itz-Agasta
Copy link
Contributor Author

hi @Lorygold I have refactored 2 test files to use setUpTestData instead of setUp for database object creation:

  • test_views.py
  • test_alert_googlechat.py

All tests are passing and performance has improved.

Could you please review these changes? If everything looks good, I will continue refactoring the remaining test files that still use setUp. Thank you!

@Lorygold
Copy link
Collaborator

Hi @Itz-Agasta, black linter failed (black . --check --diff --config ${GITHUB_WORKSPACE}/.github/configurations/python_linters/.black)
Please check the CONTRIBUTING.md file to run flake8, black and isort linters manually for passing CI tests

@Lorygold Lorygold self-requested a review July 30, 2025 16:06
@Lorygold
Copy link
Collaborator

Lorygold commented Jul 30, 2025

The related issue of this PR has been opened to refactor just the Views test (other issues and PRs have been opened in order to refactor other methods), so the changes to test_alerter_* are not required in this task (conflicts indeed)

@@ -164,44 +173,65 @@ def setUp(self):
]
)

def setUp(self):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is not needed, Django should already provide the default self.client instance in TestCase and APITestCase → documentation here

Lorygold and others added 3 commits July 31, 2025 09:31
- Removed setUp() method that manually created self.client
- APITestCase automatically provides self.client
- Removed unused Client import as per mentor feedback
- Removed setUp() method that manually created self.client
- APITestCase automatically provides self.client
- Removed unused Client import
- Updated test to use new API response format as per mentor feedback
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

Successfully merging this pull request may close these issues.

[REFACTOR] <Views Tests>: Refactor Views Tests Setup to Use setUpTestData for Improved Performance
2 participants