💻 Programmierung & Entwicklung

Django Unit Test Generator for Viewsets

📁 Programmierung & Entwicklung 👤 Beigetragen von @koksalkapucuoglu 🗓️ Aktualisiert
Der 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.

Beispiel-Ausgaben

So antworten ChatGPT, Claude und Gemini typischerweise auf diesen 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.

So nutzt du diesen Prompt

Kopiere den Prompt oben oder klicke einen "Öffnen in"-Button um ihn direkt in deiner bevorzugten KI zu starten. Du kannst den Text dann an deinen Anwendungsfall anpassen — z.B. Platzhalter wie [dein Thema] durch echten Kontext ersetzen.

Welches KI-Modell funktioniert am besten

Claude Opus 4 und Sonnet 4.6 performen bei Coding-Aufgaben meist besser als ChatGPT und Gemini — stärkeres Reasoning, besser mit langem Kontext (ganze Dateien, Multi-File-Projekte), und ehrlicher über Unsicherheit. ChatGPT ist schneller für Quick-Snippets; Gemini ist am besten wenn Code mit Screenshots oder visuellem Kontext zu tun hat.

Diesen Prompt anpassen

Tausche die im Prompt erwähnte Sprache (Python, JavaScript, etc.) gegen deinen Stack. Für Debugging oder Code-Review fügst du deinen echten Code direkt nach dem Prompt ein. Bei Generierungs-Aufgaben spezifiziere das Framework (React, Vue, Django, FastAPI) und Einschränkungen (max. Zeilen, keine externen Libraries, muss async sein).

Typische Anwendungsfälle

  • Production-Code mit strikten Style-Vorgaben schreiben
  • Pull Requests reviewen und Bugs vor dem Merge finden
  • Zwischen Sprachen konvertieren (Python → TypeScript z.B.)
  • Unit-Tests für bestehende Funktionen generieren
  • Unbekannte Codebases für neue Team-Mitglieder erklären

Variationen

Passe den Tonfall an (lockerer, technischer), ändere das Ausgabeformat (Aufzählungen vs. Absätze) oder füge Einschränkungen hinzu (Wortlimits, Zielgruppe).

Verwandte Prompts