Can ChatGPT be used to write automated tests?
Updated:
ChatGPT or another language model can help draft an automated test. A useful draft needs product rules, current application context and a check of the expected outcome. Readable code may still fail to protect the right behaviour.
Start with one scenario the team understands well. The model can draft the test or suggest missing variants. A tester decides what to verify and checks whether the resulting test actually does so. Measure the benefit after including corrections and review.
What to provide before asking for a test
A general request such as “write an order test” leaves too much room for assumptions. Prepare a short brief with:
- the specific user or business rule and its source,
- the initial state, user role and available test data,
- the steps and the outcome that must change when they finish,
- current page elements or supported APIs,
- the tool version, runner and an existing test example,
- project conventions for data setup, authentication, waiting and cleanup.
OpenAI’s official guidance recommends stating the goal, context, output and boundaries. For testing, those boundaries include not inventing selectors or changing expectations without evidence. The model should identify missing information rather than replace it with a plausible guess.
The result must fit the project. Playwright Test for TypeScript and pytest-playwright for Python use different runners and test setup. If you need both versions, require the same scenario, data, prerequisites and assertions. Translating syntax while retaining a helper that does not exist is insufficient.
An illustrative brief for submitting a document
Consider a test application with an English interface. Document DOC-217 is in the Draft state. A user with the editor role can submit it using the Submit button. A viewer cannot perform that operation. Successful submission must save the Submitted state, which remains when the document is reopened.
The brief could read:
Draft a submission test for document DOC-217 as an editor and a separate negative scenario as a viewer. Use the supplied data setup and sign-in helpers. Derive selectors only from the supplied current page state. Verify the saved state after reopening the document. For the viewer, check the agreed rejection and unchanged state. Identify any missing rule or helper. Do not change permissions or expectations to make the test pass.
This is an illustrative brief to adapt to your application. Prepare a separate document in the Draft state for each scenario, or reliably reset isolated data. The negative test must not inherit Submitted from the preceding positive test.
When browser access helps
Without current evidence, a model may suggest a convincing but incorrect selector. A selector determines how the test finds a field or button. If it points to another element, the test may fail or perform the wrong operation.
In a compatible client with configured tools, the model can use Playwright MCP. It provides structured snapshots of accessible elements and browser controls. It is not automatically available in every chat. Check the specific client, connection, account and enabled tools. The Playwright MCP article explains this setup.
A snapshot captures one page state. An element in an open dialog may be unavailable before the dialog opens. The model needs to know how to reach the required state, and the test should use a stable selector for the actual element. Poor selectors can contribute to flaky tests.
Browser access does not validate a business rule. That rule must come from a requirement or another approved source. A successful click does not confirm that the document was saved.
Start review with the outcome
Check expectations before code style. For DOC-217, an open page, a non-empty heading or a successful HTTP response is insufficient. Verify the specific document, the required state and its persistence after reloading. When a write is rejected, confirm that the data remains unchanged.
Check the opposite case too. In an isolated test, temporarily set an incorrect expected state or deliberately simulate a failed save if the application supports it. The test must fail at the relevant assertion. Undo the experiment and confirm the correct outcome. This helps detect a test that passes without a useful assertion. One such check does not prove that the test catches every defect.
Review code against the evidence. Look for ignored exceptions, missing waits for the outcome, dependence on a previous test and checks of the wrong record. OpenAI’s code-review guidance recommends verifying findings against the current change, expected behaviour and tests. Another model response alone does not replace evidence.
How to fix a failing draft
Provide the exact error message, relevant code, application state and expected behaviour. Before changing anything, determine whether the problem lies in the test, product or environment setup. Avoid asking only to “fix it so it passes”.
Require a limited correction and an explanation of the cause. Increasing a wait, disabling an assertion or catching an exception may hide the problem. Compare the corrected expectations with the original brief and check that the test still protects the same rule. New helpers and dependencies must exist in the project or undergo a separate assessment.
Distinguish proposed code from completed checks. If the model had neither a tool nor access to run the test, “the test works” is not a run result. Require the actual command, environment and report wherever execution took place.
Set data and permissions before granting access
Use an approved test environment, restricted accounts and the minimum necessary context. Do not automatically send passwords, production records or entire sensitive trace files to a chat. Check organisational rules and the selected product’s settings before sharing evidence.
For agents in ChatGPT Work, OpenAI distinguishes connected-account permissions, enabled tools and data-retention policies. API settings cannot automatically be applied to ChatGPT. Page text is evidence, not permission to change the task or send data. Playwright MCP is not itself a security boundary.
When to add the result to the suite
Accept the draft after checking the rules, running it from the prepared state, verifying the negative case and integrating it into CI. Repeated runs can reveal some dependencies, but a count of successful runs does not replace checking the test’s purpose.
The approved test should remain ordinary Playwright code or code for the chosen tool in the repository. If AI is used only during authoring, later execution does not need a language model. Assign an owner and keep the code readable so another person can update it when the product changes.
Next step
Choose one well-understood scenario and compare the entire process with ordinary test authoring: context preparation, drafting, corrections, review and diagnosis. Track assertion quality and maintenance time, rather than generated line count. For a joint pilot, arrange a no-obligation consultation.