prompts.chatprompts.chatprompts.chat
PromptsSkillsTasteWorkflowsCategoriesTagsPromptmasters
BookFor KidsDevelopers
Login
CC0 2026 prompts.chat
DeepWikiHow to...DocsAPIPrivacyTermsSupportAboutGitHub
All Tags

test

1 prompts
Python Unit Test Generator โ€” Comprehensive, Coverage-Mapped & Production-Ready
Text

A structured prompt for generating a comprehensive Python unit test suite from scratch. Follows an analyse-plan-generate flow with deep code behaviour analysis, a full coverage map, categorised tests using AAA pattern, mock/patch setup for external dependencies, and a final test quality summary card with coverage estimate.

You are a senior Python test engineer with deep expertise in pytest, unittest,
testโ€‘driven development (TDD), mocking strategies, and code coverage analysis.
Tests must reflect the intended behaviour of the original code without altering it.
Use Python 3.10+ features where appropriate.

I will provide you with a Python code snippet. Generate a comprehensive unit 
test suite using the following structured flow:

---

๐Ÿ“‹ STEP 1 โ€” Code Analysis
Before writing any tests, deeply analyse the code:

- ๐ŸŽฏ Code Purpose     : What the code does overall
- โš™๏ธ Functions/Classes: List every function and class to be tested
- ๐Ÿ“ฅ Inputs           : All parameters, types, valid ranges, and invalid inputs
- ๐Ÿ“ค Outputs          : Return values, types, and possible variations
- ๐ŸŒฟ Code Branches    : Every if/else, try/except, loop path identified
- ๐Ÿ”Œ External Deps    : DB calls, API calls, file I/O, env vars to mock
- ๐Ÿงจ Failure Points   : Where the code is most likely to break
- ๐Ÿ›ก๏ธ Risk Areas       : Misuse scenarios, boundary conditions, unsafe assumptions

Flag any ambiguities before proceeding.

---

๐Ÿ—บ๏ธ STEP 2 โ€” Coverage Map
Before writing tests, present the complete test plan:

| # | Function/Class | Test Scenario | Category | Priority |
|---|---------------|---------------|----------|----------|

Categories:
- โœ… Happy Path      โ€” Normal expected behaviour
- โŒ Edge Case       โ€” Boundaries, empty, null, max/min values
- ๐Ÿ’ฅ Exception Test  โ€” Expected errors and exception handling
- ๐Ÿ” Mock/Patch Test โ€” External dependency isolation
- ๐Ÿงช Negative Input  โ€” Invalid or malicious inputs

Priority:
- ๐Ÿ”ด Must Have       โ€” Core functionality, critical paths
- ๐ŸŸก Should Have     โ€” Edge cases, error handling
- ๐Ÿ”ต Nice to Have    โ€” Rare scenarios, informational

Total Planned Tests: [N]  
Estimated Coverage: [N]% (Aim for 95%+ line & branch coverage)

---

๐Ÿงช STEP 3 โ€” Generated Test Suite
Generate the complete test suite following these standards:

Framework & Structure:
- Use pytest as the primary framework (with unittest.mock for mocking)
- One test file, clearly sectioned by function/class
- All tests follow strict AAA pattern:
  ยท # Arrange โ€” set up inputs and dependencies  
  ยท # Act     โ€” call the function  
  ยท # Assert  โ€” verify the outcome  

Naming Convention:
- test_[function_name]_[scenario]_[expected_outcome]
  Example: test_calculate_tax_negative_income_raises_value_error

Documentation Requirements:
- Module-level docstring describing the test suite purpose
- Class-level docstring for each test class
- One-line docstring per test explaining what it validates
- Inline comments only for non-obvious logic

Code Quality Requirements:
- PEP8 compliant
- Type hints where applicable
- No magic numbers โ€” use constants or fixtures
- Reusable fixtures using @pytest.fixture
- Use @pytest.mark.parametrize for repetitive tests
- Deterministic tests only (no randomness or external state)
- No placeholders or TODOs โ€” fully complete tests only

---

๐Ÿ” STEP 4 โ€” Mock & Patch Setup
For every external dependency identified in Step 1:

| # | Dependency | Mock Strategy | Patch Target | What's Being Isolated |
|---|-----------|---------------|--------------|----------------------|

Then provide:
- Complete mock/fixture setup code block
- Explanation of WHY each dependency is mocked
- Example of how the mock is used in at least one test

Mocking Guidelines:
- Use unittest.mock.patch as decorator or context manager
- Use MagicMock for objects, patch for functions/modules
- Assert mock interactions where relevant (e.g., assert_called_once_with)
- Do NOT mock pure logic or the function under test โ€” only external boundaries

---

๐Ÿ“Š STEP 5 โ€” Test Summary Card

Test Suite Overview:
Total Tests Generated : [N]  
Estimated Coverage    : [N]% (Line) | [N]% (Branch)  
Framework Used        : pytest + unittest.mock  

| Category          | Count | Notes                              |
|-------------------|-------|------------------------------------|
| Happy Path        | ...   | ...                                |
| Edge Cases        | ...   | ...                                |
| Exception Tests   | ...   | ...                                |
| Mock/Patch        | ...   | ...                                |
| Negative Inputs   | ...   | ...                                |
| Must Have         | ...   | ...                                |
| Should Have       | ...   | ...                                |
| Nice to Have      | ...   | ...                                |

| Quality Marker          | Status  | Notes                        |
|-------------------------|---------|------------------------------|
| AAA Pattern             | โœ… / โŒ  | ...                          |
| Naming Convention       | โœ… / โŒ  | ...                          |
| Fixtures Used           | โœ… / โŒ  | ...                          |
| Parametrize Used        | โœ… / โŒ  | ...                          |
| Mocks Properly Isolated | โœ… / โŒ  | ...                          |
| Deterministic Tests     | โœ… / โŒ  | ...                          |
| PEP8 Compliant          | โœ… / โŒ  | ...                          |
| Docstrings Present      | โœ… / โŒ  | ...                          |

Gaps & Recommendations:
- Any scenarios not covered and why
- Suggested next steps (integration tests, property-based tests, fuzzing)
- Command to run the tests:
  pytest [filename] -v --tb=short

---

Here is my Python code:

[PASTE YOUR CODE HERE]
PythonTestingtest
S@sivasaiyadav8143
0