Tests pass locally but fail in CI: how to find the difference
Updated:
When a test passes on a laptop and fails in CI, start by comparing the conditions of both runs. The difference may lie in the environment, data, or way the test was launched. Diagnosis should establish which difference changes the outcome so you can verify the repair.
Why local and CI results differ
Continuous integration (CI) builds software and runs checks on a separate runner, the machine or process that executes automated steps. That runner may have a different operating system, capacity, time zone, browser version, access rights, and test data from the local computer. The more accurately you capture those conditions, the easier it becomes to reproduce and fix the failure.
Preserve evidence from the original failure first
Do not begin with another run alone. Retain the test name, commit and application build, exact command, execution time, runner, browser, and first error message. Add the test’s console output, relevant application and network logs, and a request or correlation ID if the system uses one. Look for the first cause. Later errors may only be consequences.
For user interface (UI) tests, a recording of test steps (trace), screenshot, or video may help. Check the actual configuration, rather than just the settings recommended in a guide. Playwright’s on-first-retry mode records the first retry, not the original failure. The retain-on-failure mode records individual attempts and keeps recordings of those that failed. Its Trace Viewer lets you inspect steps, page state, console output, and network activity.
The artefact must belong to the failed attempt. A video from a retry that passed may not reveal the original problem. Also check that artefacts are uploaded after a pipeline job fails. Recordings may contain personal data, tokens, or completed forms, so give them restricted access and a suitable retention period.
An illustrative example: an order fails only in the nightly CI run
Consider an illustrative test that creates an order using today’s date for delivery. The test process prepares the date using its own time zone. Locally, it uses Europe/Bratislava, as does the API’s business rule that rejects a past date. At ten in the morning, the test passes. In CI, however, the test process uses UTC. Shortly after midnight in Bratislava, it is still the previous day in UTC, so the test submits a date the API considers to be in the past. A screenshot shows a validation error, but without knowing where the date came from, the cause remains unclear.
The illustrative suite is configured with four workers, or concurrent execution processes. Each uses e2e-customer@example.test and empties that account’s basket before its test. One scenario can therefore remove another scenario’s items. The local command is configured with one worker, so the collision does not occur. This is the configuration in this example, rather than a universal default.
The third hypothesis concerns versions. Compare the library and browser actually running, rather than just the cache contents. Each Playwright version needs specific browser versions. An old binary being present does not mean the tool used it. A different package, a configured Chrome channel, or a custom executablePath can account for a difference.
Time, concurrency, and environment version are separate hypotheses. In one experiment, repeat the CI run with one worker. In another, set the test process to the business time zone, and compare exact versions separately. Restore the baseline conditions before each experiment. Otherwise, a pass after several changes will not show which one made the difference.
Build a systematic environment diff
Instead of changing timeouts at random, compare the local and CI runs across the same categories:
- Code and command: the same commit, built artefact, working directory, test tags, and configuration files. Confirm that the local command really is the one used by the pipeline.
- Versions: runtime, installed packages resolved by the dependency lockfile, the browser actually launched, its channel, any custom executable path, drivers, and operating system. Record the exact container image tag and identifier (digest) too. For a remote Playwright server, the Docker integration documentation says the client version should match the server version in the container. The image contains browsers and system dependencies, while the project’s package is installed separately.
- Configuration: environment variables, feature flags, base URL, proxy, certificates, headless mode, and permissions. For a secret access credential, record only its name and whether the availability check passed. Do not print its value or hash. Compare non-sensitive settings through a predefined list of permitted fields.
- Time and locale: the time zones of the test process, browser, and API, along with locale, date format, and system clocks. Establish which part calculates today’s date. Time-zone emulation in Playwright changes the browser, rather than the test process or API time zone.
- Data and access: database migrations, seeded records, account state, role, token expiry, secret availability, and external-service behaviour. A missing secret may surface as an ordinary sign-in failure.
- Resources and network: CPU and memory capacity, latency, DNS, ports, firewall rules, and application start-up time. A slower runner can expose a wait tied to elapsed time instead of the required state.
When access is missing, also check the event that triggered the job. For example, GitHub Actions does not provide ordinary secrets for runs triggered from a fork. A local sign-in may therefore be a poor comparison. Write a short environment diff: what matches, what differs, and what remains unknown.
Reproduce the conditions, not just the test
Run the failing test from a freshly retrieved repository (clean checkout) with the same lockfile and command. If the pipeline uses a container, try the same image locally. Conversely, a diagnostic CI step can print safe tool versions, locale, and time zone. The official Playwright CI guide, for example, separates package installation, browser installation, and test execution, allowing each phase to be checked.
If the test passes alone but fails in the suite, investigate concurrency and shared state. Temporarily set one worker and compare the outcome. Then look for the same account, record name, file, port, or assumed test order. One worker can help with diagnosis, but does not itself remove a shared-data conflict. Adjust the process count to the runner’s available resources too.
If it also fails alone, change one condition at a time: time zone, version, configuration, or data source. Record the outcome of each attempt. When failures are unpredictable in both environments, you are more likely dealing with instability that should be measured across run history, not only a local-versus-CI difference.
How to confirm that you found the cause
A single successful run does not confirm the repair. A hypothesis becomes convincing when you can control the outcome: under the original condition the test repeatedly fails, and after one targeted change it passes without other interventions. For an account collision, keep the application and test code unchanged but give every worker a separate account. Then repeat the suite with the worker count used in CI.
Design a small experiment for each hypothesis:
- Write the expected outcome, such as “the collision disappears with one worker”.
- Identify the single variable to change and keep the other conditions the same.
- Repeat both the failing and corrected configurations several times.
- Check that the failure did not merely move or change type.
- Add an automated check that will reveal a recurrence.
The final point separates diagnosis from a durable repair. If a missing secret caused the problem, the pipeline should fail during validation of required configuration. For shared data, separate the accounts or generate identifiers from the run and worker.
For a time-related error, agree on the business time zone and configure it where the date is calculated. Verify boundary dates using controlled time. Playwright’s clock controls timing functions in the browser, rather than the API or database time. If the server makes the decision, it needs its own way to accept test time or prepared data. Setting page.clock alone does not change its current date.
After the repair, compare at least these pieces of evidence: the same test passes from a clean local checkout and in CI, passes alone and in the complete suite, and produces diagnostic output containing the decisive versions and configuration. For a concurrency problem, execute the target group with the intended parallelism instead of only once in serial. For a time-related problem, exercise the boundary where it originally failed.
Record the cause in the incident or work item as “condition → mechanism → symptom”. For example: “Two workers changed the same basket, so one removed an item before the other’s assertion and the test reported the wrong total.” This is more useful than “increased timeout” and can become a rule for the entire suite.
Repairs that hide rather than solve the problem
A longer timeout can test a hypothesis about a slow operation, but it should not remain the only repair without checking the actual state. A retry may collect more evidence, but a pass on the second attempt must not erase the first failure. Likewise, do not leave the pipeline permanently serial if shared data is still the real problem.
Avoid changing application code, the test, the runner, and data at the same time. The result may turn green, but it will not show which change removed the failure or whether it can return.
What you gain
A reproducible difference shortens the path from a red result to its cause. The team can separate an application defect from a test defect, missing configuration, or a data collision. It also gains a checklist of information every CI run should retain, so the next failure does not begin without context.
Next step
For the next failure, create a one-page comparison of the command, versions, configuration, time, data, and worker count for both runs. Then transfer only one difference into the local environment or CI at a time. If the suite has been unclear for a long time and lacks useful evidence, the process for taking over an inherited failing suite can first restore control over execution and ownership.