💻 Coding & Development

Django Unit Test Generator for Viewsets

📁 Coding & Development 👤 Contributed by @koksalkapucuoglu 🗓️ Updated
The prompt
I want you to act as a Django Unit Test Generator. I will provide you with a Django Viewset class, and your job is to generate unit tests for it. Ensure the following: 1. Create test cases for all CRUD (Create, Read, Update, Delete) operations. 2. Include edge cases and scenarios such as invalid inputs or permissions issues. 3. Use Django's TestCase class and the APIClient for making requests. 4. Make use of setup methods to initialize any required data. Please organize the generated test cases with descriptive method names and comments for clarity. Ensure tests follow Django's standard practices and naming conventions.

Example outputs

Here is what ChatGPT, Claude, and Gemini typically return for this prompt:

🚀 ChatGPT

I'll generate comprehensive unit tests for your Django Viewset. The tests will cover all CRUD operations using Django's TestCase and APIClient, including test cases for successful operations, validation errors, authentication failures, and permission denied scenarios. Each test method will have descriptive names like `test_create_object_success`, `test_retrieve_object_not_found`, and `test_update_without_permission`, with clear comments explaining what's being tested. The setUp method will initialize test users, authentication tokens, and sample model instances to keep tests DRY and maintainable.

✨ Claude

I'd be happy to generate unit tests for your Django Viewset, though I should note I'll need to see the actual Viewset code to create accurate, specific tests. The generated tests will cover standard CRUD operations while also addressing edge cases like unauthenticated requests, missing fields, and invalid data types. I'll structure the tests with a setUp method for test data initialization and use clear naming conventions, though you may need to adjust authentication mechanisms based on whether you're using token auth, sessions, or another approach.

💎 Gemini

I will create a complete test suite for your Django Viewset that includes test cases for list, create, retrieve, update, and delete operations. The tests will utilize `APIClient` for API requests and cover scenarios including successful operations, HTTP 400 for invalid data, HTTP 401/403 for authentication/permission errors, and HTTP 404 for non-existent resources. Each test class will inherit from `django.test.TestCase` with a `setUp` method initializing test users and model instances, following Django REST framework testing best practices.

How to use this prompt

Copy the prompt above or click an "Open in" button to launch it directly in your preferred AI. You can then customize the wording to match your exact use case — for example replacing placeholders like [your topic] with real context.

Which AI model works best

Claude Opus 4 and Sonnet 4.6 generally outperform ChatGPT and Gemini on coding tasks — better reasoning, better at handling long context (full files, multi-file projects), and more honest about uncertainty. ChatGPT is faster for quick snippets; Gemini is best when code involves screenshots or visual context.

How to customize this prompt

Swap the language mentioned in the prompt (Python, JavaScript, etc.) for whichever stack you're on. For debugging or code review, paste your actual code right after the prompt. For generation tasks, specify the framework (React, Vue, Django, FastAPI) and any constraints (max lines, no external libraries, must be async).

Common use cases

  • Writing production code with strict style requirements
  • Reviewing pull requests and catching bugs before merge
  • Converting between languages (Python → TypeScript, for example)
  • Generating unit tests for existing functions
  • Explaining unfamiliar codebases to new team members

Variations

Adapt the tone (more casual, more technical), change the output format (bullet points vs. paragraphs), or add constraints (word limits, target audience).

Related prompts