P
💻DeveloppementIntermediateAll AIs

Automatically Generate Unit Tests with AI

Automatically generate an exhaustive unit test suite covering nominal cases, edge cases, and error cases for any source code.

Paste in your AI

Paste this prompt in ChatGPT, Claude or Gemini and customize the variables in brackets.

Tu es un ingénieur QA senior spécialisé en tests automatisés. Génère une suite complète de tests unitaires pour le code suivant écrit en [LANGAGE_DE_PROGRAMMATION] :

[CODE_SOURCE]

Contexte du projet : [DESCRIPTION_DU_PROJET]
Framework de test à utiliser : [FRAMEWORK_DE_TEST]

Pour chaque fonction ou méthode présente dans le code, génère des tests couvrant :

  1. Cas nominaux : comportement attendu avec des entrées valides typiques
  2. Cas limites : valeurs aux frontières (zéro, chaîne vide, liste vide, valeurs maximales)
  3. Cas d'erreur : entrées invalides, null/undefined, types incorrects, exceptions attendues
  4. Cas de régression : scénarios susceptibles de casser lors de refactoring

Pour chaque test :

  • Utilise la convention de nommage : test_<methode>_<scenario>_<resultat_attendu>
  • Applique le pattern AAA (Arrange, Act, Assert) avec des commentaires séparant chaque section
  • Ajoute des messages d'assertion explicites décrivant l'échec
  • Utilise des fixtures ou des mocks si des dépendances externes sont impliquées

Après les tests, fournis :

  • Un tableau récapitulatif du taux de couverture estimé par fonction
  • Les cas non testables automatiquement (nécessitant des tests d'intégration)
  • Des suggestions pour améliorer la testabilité du code source si nécessaire

Personalize this prompt with Léa

Léa rewrites this prompt for your job and your exact goal — 3 quick questions.

Why this prompt works

<p>This prompt transforms AI into an expert QA engineer to produce professional-quality unit tests. By providing your source code and desired test framework, you get a complete suite covering essential scenarios: normal operation, boundary values, and error handling.</p><p>For optimal results, <strong>paste the actual code</strong> rather than a description. Specify the test framework (<strong>pytest</strong>, <strong>Jest</strong>, <strong>JUnit</strong>, <strong>PHPUnit</strong>, etc.) to get directly usable syntax. The AAA pattern (Arrange, Act, Assert) structures each test in a readable and maintainable way.</p><p>Adapt the prompt to your needs: add minimum coverage constraints, request <strong>mocks</strong> for API or database calls, or specify your naming conventions. The final coverage table helps identify uncovered areas and prioritize complementary integration tests.</p>

Use Cases

Quickly generate tests for legacy code without existing coverageCreate a test suite before a major refactoring to prevent regressionsComplete test coverage on critical functions identified by an auditTrain junior developers in testing best practices by showing concrete examples

Expected Output

A complete ready-to-use unit test suite in the specified framework, with explicit naming, AAA pattern, coverage of nominal, edge, and error cases, accompanied by an estimated coverage table and improvement recommendations.

Improve this prompt

Run this prompt through the Optimizer to strengthen its context, constraints and expected format.

Improve this prompt with the Optimizer

Comments

  • LéaAI

    Pour des tests plus pertinents, ajoutez dans le prompt 2-3 exemples concrets d'entrées avec valeurs attendues explicites. L'IA produira des assertions précises plutôt que génériques. Précisez aussi les dépendances externes à mocker pour guider la génération des mocks.

📬 Get new prompts every week

Join our newsletter and never miss a prompt.

Go further

Similar Prompts

💻DeveloppementIntermediateAll AIs

Set up semantic versioning automation

Automate semantic versioning

0254
💻DeveloppementIntermediateAll AIs

Write Comprehensive Unit Tests

Generate an exhaustive unit test suite covering nominal cases, edge cases, and errors with appropriate mocks.

22562
💻DeveloppementIntermediateAll AIs

Improve Test Coverage

Strategically increase test coverage

0264
💻DeveloppementIntermediateAll AIs

Perplexity Prompt to Create a REST API

Perplexity AI stands out from other AI assistants by its unique ability to combine real-time web search and code generation. For creating a REST API, this advantage is significant: Perplexity can check the latest framework versions, verify current security best practices, and rely on the most recent official documentation. Unlike a classic LLM whose knowledge is fixed, Perplexity guarantees recommendations aligned with the state of the art. Whether you are building an API with Node.js/Express, Python/FastAPI, or any other stack, a well-structured prompt allows you to obtain a complete architecture, consistent endpoints, robust error handling, and test examples. The challenge is to formulate your request with enough business context and technical constraints so that Perplexity generates production-grade code, not just a tutorial. This guide provides optimized prompts to get the most out of Perplexity in designing professional REST APIs, from beginner to advanced versions.

0252