10 Best Practices for Test Automation
The ten best practices for test automation are: automate the highest-value tests first, use stable selectors, wait for conditions instead of fixed delays, keep every test independent, remove duplication, separate test data from code, run the tests in CI on every change, make failures easy to diagnose, fix flaky tests immediately and maintain the suite like production code.
- Automate for value, not coverage: stable, repetitive, high-risk scenarios first; exploratory and UX checks stay manual
- Reliability beats quantity: a small suite the team trusts is worth more than a large one that fails at random
- Modern tools help: Playwright auto-waits and retries its assertions, Cypress retries commands and assertions, Selenium offers explicit waits
- Test code is production code: review it, refactor it and delete what no longer earns its place
1. What should you automate first?
Automate the tests that run often, check high-risk functionality and have stable expected results: smoke tests, regression tests on critical user journeys, and API checks on core business rules. Leave one-off checks, features whose design still changes every sprint, and anything that needs human judgement, such as usability, to manual or exploratory testing.
| Good candidates | Poor candidates (for now) |
|---|---|
| Login, checkout, payment and other critical journeys | Features still being redesigned |
| Regression checks run at every release | Tests run once or twice |
| Calculations and rules with many input combinations | Visual appeal and ease of use |
| API contracts between services | Exploratory testing sessions |
2. How do you choose stable selectors?
Select elements the way a user or a stable contract identifies them, not by their position in the page. Playwright recommends user-facing locators such as getByRole and getByLabel, or getByTestId for a dedicated test attribute. Cypress recommends data-* attributes such as data-cy. With Selenium, prefer IDs and short CSS selectors to long XPath expressions that follow the DOM structure. Selectors based on generated class names or element order break at the first redesign.
3. How should tests wait for the application?
Wait for a condition, never for a fixed amount of time. A sleep(5) is too long when the application is fast and too short when it is slow, so it makes tests both slow and flaky. Playwright waits automatically for elements to be actionable, and its web-first assertions such as expect(locator).toBeVisible() retry until a timeout. Cypress retries queries and assertions in the same way. In Selenium, use WebDriverWait with expected conditions.
4. Why must automated tests be independent?
Because a test that depends on another one fails for the wrong reason, cannot be run alone and cannot run in parallel. Each test should create the state it needs, through an API call, a fixture or a factory, and clean up after itself. Playwright Test gives each test a fresh browser context by default, which isolates cookies and storage. A simple check: run the suite in random order and in parallel; if results change, some tests share state.
5. How do you avoid duplicated test code?
Move repeated interactions into page objects, components and helpers, and keep the assertions and the scenario readable in the test itself. The Page Object Model puts each page's locators and actions in one class, so a changed selector is fixed in one place. Do not over-abstract: a test should still read like a scenario. Our article on test framework design patterns compares the Page Object Model, Screenplay and other patterns.
6. How should test data be managed?
Keep test data outside the test logic and generate it rather than sharing it. Parameterised tests run the same scenario over many data rows; factories and libraries such as Faker create realistic, unique records; API calls prepare data faster than the user interface. Never depend on data that another test or a colleague might modify. Our data-driven testing guide shows how to do it with pytest.
7. When should automated tests run?
Automatically, on every pull request, in a CI pipeline that blocks the merge when a test fails. Run the fastest tests first and keep long suites for a nightly schedule. Tests that only run when someone remembers to launch them stop protecting anything. See how to set this up in our guide to integrating automation into CI/CD.
8. How do you make test failures easy to diagnose?
Make every failure tell you what broke without re-running the test. Give tests names that describe the expected behaviour, check one behaviour per test, and write assertion messages that show expected and actual values. Collect evidence on failure: screenshots, videos, logs and, with Playwright, a trace you can replay step by step. Publish results in a report (HTML or JUnit XML) that the whole team can open.
9. What should you do about flaky tests?
Fix them or quarantine them immediately; never let them fail in the main suite. A flaky test passes and fails on the same code, so the team learns to ignore red builds, and real regressions slip through. Track which tests are flaky, find the root cause (timing, shared data, unstable environment, weak selector) and assign an owner. Automatic retries can reveal flakiness, but they must not hide it.
10. How do you keep an automation suite maintainable?
Apply the same standards as for application code: version control, code review, linting, conventions and regular refactoring. Delete tests that duplicate others or cover removed features. Document how to run the suite, add a test and read a report. Watch a few health indicators rather than the raw number of tests: suite duration, share of flaky tests and defects that escaped to production despite the suite.
Which practice prevents which problem?
| Practice | Problem it prevents |
|---|---|
| Stable selectors | Tests breaking after every UI change |
| Condition-based waits | Slow and flaky tests |
| Independent tests | Cascading failures, no parallel runs |
| Page objects and helpers | Fixing the same selector in many files |
| External, generated test data | Tests failing because shared data changed |
| CI on every pull request | Regressions discovered days later |
| Evidence on failure | Hours spent reproducing a failure |
Frequently asked questions
Should you automate all tests?
No. Automate tests that run often, cover high-risk features and have stable expected results. Exploratory testing, usability checks and features that change every sprint are better tested manually.
What is the most common cause of flaky tests?
Timing problems are among the most frequent: fixed waits and assertions made before the page or API is ready. Shared test data, dependencies between tests, unstable environments and fragile selectors are the other usual causes.
Is the Page Object Model still useful with Playwright?
Yes. Playwright's documentation describes the Page Object Model as a way to structure larger suites: locators and actions live in page classes, while tests keep the scenario and the assertions.
How often should automated tests run?
Fast tests should run on every pull request in CI. Long suites such as full cross-browser regression or performance tests are usually scheduled nightly.
Which language is best for test automation?
The language of your application team, so developers can read and maintain the tests. TypeScript with Playwright, Python with pytest and Java with Selenium or REST Assured are all common choices.
Put these practices into practice
AutomationDataCamp (ADC) teaches these practices on real projects. Our QA Automation & AI Tester programme (399 h over 12 weeks, 100% online, taught in English or French, no technical prerequisites) covers Playwright with TypeScript, API testing and a GitHub Actions CI pipeline, and includes the ISTQB CTFL v4.0 exam.
View the 3-month programme View all coursesRelated reading: Test automation ROI calculator: payback period and savings.
Share this article
Related articles

Data-Driven Testing: Complete Guide
A complete guide to Data-Driven Testing with Python...
Read more : Data-Driven Testing — Complete Guide
API Testing: Tool Comparison
Detailed comparison of the best tools for testing APIs...
Read more : API Testing — Tool Comparison