Unit Test Generator
Prerequisites & Dependencies
- Language runtime matching the project: Node.js 18+ (Jest/Vitest), Python 3.10+ (pytest, pytest-mock), or Go 1.21+
- Coverage tooling configured in the project (
c8/Istanbul, pytest-cov, or go test -cover)
- Existing test conventions (directory layout, config files) so new tests match the repo setup
Execution Steps
- Inventory the units under test and classify their dependencies: pure logic, I/O (DB, HTTP, filesystem), time, and randomness.
- Derive test cases per unit: happy path, boundary values, invalid inputs, error propagation, and state transitions.
- Replace external dependencies with mocks/stubs; assert interactions (call count, arguments) only where they are part of the unit's contract.
- Write tests using the project's framework with arrange-act-assert structure and behavior-describing test names.
- Run the suite with coverage; add cases until all public branches are exercised, without testing implementation internals.
- Ensure determinism: seed randomness, inject clocks instead of real time, and keep tests order-independent.
import pytest
from unittest.mock import MagicMock
from orders.service import OrderService
def test_place_order_charges_and_persists():
payments, repo = MagicMock(), MagicMock()
svc = OrderService(payments=payments, repo=repo)
order = svc.place_order(user_id=7, items=[("sku-1", 2)], price=10)
payments.charge.assert_called_once_with(user_id=7, amount=20)
repo.save.assert_called_once_with(order)
assert order.total == 20
def test_place_order_propagates_payment_failure():
payments = MagicMock()
payments.charge.side_effect = RuntimeError("declined")
svc = OrderService(payments=payments, repo=MagicMock())
with pytest.raises(RuntimeError, match="declined"):
svc.place_order(user_id=7, items=[("sku-1", 1)], price=5)
pytest --cov=orders --cov-report=term-missing -q
1---2name: unit-test-generator3description: Generate automated unit test suites (Jest, Pytest, Go test) with complete mocking.4---56# Unit Test Generator78## Prerequisites & Dependencies9- Language runtime matching the project: Node.js 18+ (Jest/Vitest), Python 3.10+ (pytest, pytest-mock), or Go 1.21+10- Coverage tooling configured in the project (`c8`/Istanbul, `pytest-cov`, or `go test -cover`)11- Existing test conventions (directory layout, config files) so new tests match the repo setup1213## Execution Steps141. Inventory the units under test and classify their dependencies: pure logic, I/O (DB, HTTP, filesystem), time, and randomness.152. Derive test cases per unit: happy path, boundary values, invalid inputs, error propagation, and state transitions.163. Replace external dependencies with mocks/stubs; assert interactions (call count, arguments) only where they are part of the unit's contract.174. Write tests using the project's framework with arrange-act-assert structure and behavior-describing test names.185. Run the suite with coverage; add cases until all public branches are exercised, without testing implementation internals.196. Ensure determinism: seed randomness, inject clocks instead of real time, and keep tests order-independent.2021```python22import pytest23from unittest.mock import MagicMock24from orders.service import OrderService2526def test_place_order_charges_and_persists():27 payments, repo = MagicMock(), MagicMock()28 svc = OrderService(payments=payments, repo=repo)2930 order = svc.place_order(user_id=7, items=[("sku-1", 2)], price=10)3132 payments.charge.assert_called_once_with(user_id=7, amount=20)33 repo.save.assert_called_once_with(order)34 assert order.total == 203536def test_place_order_propagates_payment_failure():37 payments = MagicMock()38 payments.charge.side_effect = RuntimeError("declined")39 svc = OrderService(payments=payments, repo=MagicMock())4041 with pytest.raises(RuntimeError, match="declined"):42 svc.place_order(user_id=7, items=[("sku-1", 1)], price=5)43```4445```bash46pytest --cov=orders --cov-report=term-missing -q47```48