Use AI to Write Tests and Docs Faster
Use AI to Write Tests and Documentation Without Losing Quality
AI can dramatically reduce the time developers spend writing unit tests, integration tests, README updates, API examples, and changelog notes. The key is not to ask the model to “write tests” in one vague prompt. The best results come from giving it context, constraints, and a repeatable workflow. This tutorial shows a practical process you can use with Claude, GPT, Claude Code, Codex, or any OpenAI-compatible SDK.
If you want to keep costs low while running frequent prompts against real code, 59API is a strong option. It provides pay-as-you-go access to Claude models such as Opus, Sonnet, Haiku, and Fable, plus GPT models, through the API base URL https://api.59api.com. It is compatible with Claude Code, Codex, and OpenAI SDK workflows, uses native official-quality models without downgrade, and is designed as a low-cost relay for developer automation.
Step 1: Choose the Right AI Model for the Task
Start by matching the model to the job. For complex test design, edge cases, mocking strategy, and architecture-aware documentation, use a stronger reasoning model such as Claude Sonnet, Claude Opus, or a capable GPT model. For repetitive tasks such as generating docstrings, simple test cases, or usage examples, a faster and cheaper model such as Claude Haiku can be enough.
With 59API, you can experiment across models without changing your whole workflow because it supports OpenAI-compatible usage and tools like Claude Code and Codex. This makes it easier to use premium models only when needed and cheaper models for bulk documentation passes.
Step 2: Give the AI a Small, Complete Code Context
Do not paste your entire repository unless your tool supports repository-aware context. Instead, provide the smallest complete unit: the function, class, route handler, schema, and any related test utilities. Include the language, framework, test runner, and expected behavior.
A useful prompt structure is: explain the purpose of this file, list important edge cases, then write tests using our existing conventions. If the project uses Jest, Pytest, Vitest, Go test, RSpec, or PHPUnit, say so explicitly. Mention whether network calls, file access, database writes, and time-dependent behavior should be mocked.
Step 3: Ask for a Test Plan Before Code
Before generating test files, ask the AI to produce a test plan. This prevents shallow tests that only check happy paths. The plan should include normal inputs, boundary values, invalid inputs, error handling, permissions, concurrency concerns, and regression cases for known bugs.
For example, if you have a payment calculation function, the model should identify rounding behavior, zero-value transactions, discounts, tax rules, currency precision, and failed validation. Review the plan first, remove irrelevant cases, and add business-specific cases the model cannot know. Then ask it to generate the actual tests from the approved plan.
Step 4: Generate Tests That Fit Your Repository
Now ask the AI to write tests in the same style as your existing test suite. Include one or two current test files as examples. This is more effective than only naming the framework because it teaches the model your naming conventions, setup helpers, fixture patterns, and assertion style.
Be specific about output. Ask for one complete test file, no unrelated refactors, deterministic tests, and clear test names. If the code touches external systems, require mocks or fakes. If the function depends on time, ask for frozen time. If it uses random values, require seeded randomness or injected dependencies.
Step 5: Run the Tests and Feed Back the Errors
AI-generated tests should be treated like a first draft. Run them locally or in CI. When failures happen, paste the exact error output back into the model and ask for a minimal fix. Include whether the failure is caused by a bad test assumption or a real bug.
This loop is where AI becomes especially useful. It can quickly adapt imports, update mocks, fix async handling, correct fixture setup, or rewrite assertions. However, do not blindly accept changes that weaken the test. If the model removes meaningful assertions just to make the test pass, ask it to preserve the original behavior being verified.
Step 6: Use AI to Improve Documentation From the Tests
Once tests define behavior clearly, use them as source material for documentation. Ask the AI to create or update README sections, API reference notes, examples, error tables, and migration instructions based on the tested behavior. Tests are often more accurate than old documentation because they describe what the code actually does.
A strong documentation prompt includes the target audience, format, and scope. For example: write a developer-facing README section for this authentication middleware, include installation assumptions, configuration options, one minimal example, common errors, and security notes. Tell the model not to invent features that are not visible in the code or tests.
Step 7: Add Documentation Checks to Your Workflow
AI can also help maintain documentation over time. In a pull request workflow, ask the model to compare changed files against existing docs and list sections that may need updates. For SDKs and APIs, ask it to verify whether examples still match function signatures. For public libraries, ask it to draft changelog entries from commit messages or pull request summaries.
This is a good place to use a cost-efficient relay like 59API, because documentation review can run frequently. You can call compatible Claude or GPT models through https://api.59api.com, keep your existing OpenAI SDK-style integration, and control spend with pay-as-you-go usage. If your team runs many small AI checks, the lower relay cost and referral rebate can make the workflow more sustainable.
Step 8: Review for Accuracy, Security, and Maintainability
AI output should always go through human review. For tests, check that assertions verify real behavior rather than implementation details. Avoid tests that only duplicate the source code logic. For documentation, check that examples run, configuration names are correct, and security-sensitive advice is accurate.
- Verify commands: Run every generated setup command and example before publishing.
- Check edge cases: Make sure the AI did not ignore nulls, empty arrays, permission failures, or network errors.
- Protect secrets: Never paste production API keys, private customer data, or credentials into prompts.
- Keep ownership: Treat AI as a coding assistant, not the final authority on product behavior.
A Practical AI Testing and Docs Workflow
A reliable repeatable workflow looks like this: provide focused code context, ask for a test plan, approve the plan, generate tests, run them, feed back errors, finalize the tests, then generate documentation from the tested behavior. This approach gives you faster coverage and better docs without sacrificing engineering judgment.
If you want to try this with Claude, GPT, Claude Code, Codex, or your existing OpenAI SDK setup while keeping costs low, consider signing up for 59API and pointing your client to https://api.59api.com. It is a practical way to add AI-assisted testing and documentation to your daily development workflow without overpaying for model access.
Pronto para começar?
Conecte Claude e GPT em minutos pelos menores preços, sem cortes. Cadastre-se e obtenha sua chave API.
Cadastro grátis