Short answer: decide which behaviors matter most, check each one at the lowest layer where it actually lives, give your tests predictable data and fake versions of outside services, then make your CI pipeline run them on every change.
That's the whole loop of automated functional testing. The rest of this post walks through it with a small, concrete example, because "automate your tests" is easy advice to give and surprisingly hard to act on when you're staring at an empty test folder.
The example is a basic online store with a cart and discount coupons. Swap in your own product as you read. The steps don't change much.
Start with a list, not a tool
Most people open a browser tab and start comparing frameworks. That's backwards. The tool matters a lot less than knowing what you're going to test.
Grab a doc and write down three things:
- The flows that make or lose money, like signup, login, adding to cart, checkout and refunds
- The last ten or so bugs that reached production
- Anything you or your QA folks re-check by hand before every release
For our store, the list might start like this: a customer can log in, add items, apply a valid coupon, get a clear error for an expired coupon, and pay with a saved card. Five items. That's plenty for a first pass.
Write the expected behavior in plain words
Before any code, write each item as a short scenario. A simple format works well:
Given a cart with two mugs at 50.00 each
When the customer applies coupon SAVE10
Then the total is 90.00Given a coupon that expired yesterday
When the customer applies it
Then they get an "expired coupon" error and the total doesn't change
This feels like busywork. It isn't. Half the arguments about whether a test is "right" turn out to be arguments about what the feature should do, and it's much cheaper to settle those in a sentence than in a pull request.
Decide where each check belongs
Now ask where each behavior actually lives in your system.
The discount math happens on the server. So test it by calling the API directly. You don't need a browser to confirm that 100 minus 10% is 90, and a browser test for it will break whenever someone renames a button.
The fact that a customer can get from the product page to a paid order is a journey that crosses the UI, the API and the payment step. That one deserves a browser test.
For our store, the split ends up like this:
| Scenario | Layer | Why |
|---|---|---|
| Valid coupon applies the right discount | API | Pure business logic, fast to check |
| Expired coupon is rejected | API | Error handling lives in the backend |
| Login with wrong password fails | API | Same reason |
| Customer checks out with a saved card | UI | The full journey is what matters |
Three API tests and one UI test. That ratio holds up well as suites grow.
A worked example at the API layer
Here's what the coupon tests might look like in Python with pytest and requests. Any language and HTTP client will do the same job.
import uuid
import requests
BASE = "http://localhost:8000"
def new_customer():
email = f"qa-{uuid.uuid4().hex[:8]}@example.test"
r = requests.post(f"{BASE}/users", json={"email": email, "password": "Passw0rd!"})
assert r.status_code == 201
return {"Authorization": f"Bearer {r.json()['token']}"}
def test_save10_takes_ten_percent_off():
headers = new_customer()
requests.post(f"{BASE}/cart/items", json={"sku": "MUG-01", "qty": 2}, headers=headers)
r = requests.post(f"{BASE}/cart/coupon", json={"code": "SAVE10"}, headers=headers)
assert r.status_code == 200
assert r.json()["total"] == "90.00"
def test_expired_coupon_is_rejected():
headers = new_customer()
requests.post(f"{BASE}/cart/items", json={"sku": "MUG-01", "qty": 2}, headers=headers)
r = requests.post(f"{BASE}/cart/coupon", json={"code": "OLD5"}, headers=headers)
assert r.status_code == 422
assert r.json()["error"] == "coupon_expired"
assert r.json()["total"] == "100.00"
A few things worth pointing out, because they're where beginners usually slip:
- Every test creates its own customer with a random email. No test depends on another one having run first.
- Each test checks one behavior and says exactly what "correct" means. "Status is 200" alone would pass even if the discount were wrong.
- The expired coupon test also checks that the total didn't change. Bugs love to hide in the "nothing should happen" cases.
Handling the database and outside services
Those tests assume two things exist: a product called MUG-01 priced at 50.00, and a coupon called OLD5 that has already expired. Don't create those by hand in a shared staging database and hope nobody touches them. Someone will.
Put them in a seed script that runs before the suite, against a database that exists only for testing. A throwaway container started in CI is the usual approach. Fresh data every run means a failure points at your code, not at whatever a colleague did on staging last Thursday.
Outside services need the same treatment. If checkout calls a payment provider, your test suite shouldn't depend on that provider's sandbox being up. Replace it with a mock that returns realistic responses, including the unpleasant ones like a declined card or a timeout. Then keep a handful of separate checks that hit the real sandbox on a schedule, so you still find out if their API changes.
If writing mocks by hand sounds tedious, it is. Some teams record real traffic between their service and its dependencies and replay it as mocks, which saves a lot of typing and keeps the mocks closer to reality.
Adding a few UI tests
Once the API layer is covered, add browser tests for the journeys a customer can't live without. Here's the checkout one in Playwright:
import { test, expect } from '@playwright/test';
test('customer can check out with a saved card', async ({ page }) => {
await page.goto('/login');
await page.getByLabel('Email').fill('saved-card@example.test');
await page.getByLabel('Password').fill('Passw0rd!');
await page.getByRole('button', { name: 'Log in' }).click();
await page.goto('/products/mug-01');
await page.getByRole('button', { name: 'Add to cart' }).click();
await page.getByRole('link', { name: 'Checkout' }).click();
await page.getByRole('button', { name: 'Pay now' }).click();
await expect(page.getByText('Order confirmed')).toBeVisible();
});
Notice it finds elements by their label and role, not by CSS classes. Designers change class names all the time. They rarely change a button that says "Pay now" without also changing what it does.
Keep this layer small. Ten to twenty UI tests covering real journeys will tell you more than a hundred that click every button on every page.
Getting it into CI
Tests that only run on someone's laptop stop running. Put them in your pipeline so they run without anyone having to remember. A minimal GitHub Actions setup looks like this:
name: functional-tests
on: [pull_request]
jobs:
api-tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: docker compose up -d db app
- run: python scripts/seed_test_data.py
- run: pip install -r requirements-test.txt
- run: pytest tests/api -q
Start there. Once that's stable, add the UI tests on merge to your main branch, the full regression set before releases, and a nightly run against staging. Keep the pull request stage under about ten minutes, or people will merge before it finishes and the whole thing loses its point.
Keeping the suite alive
Writing the first tests is the fun part. Keeping them useful is the actual job.
When a test turns flaky, fix it or delete it that week. A retry button teaches the team that red builds don't mean anything, and that lesson is very hard to unlearn.
When a production bug slips through, write the test that would have caught it before you write the fix. Your suite slowly becomes a record of everything that has ever gone wrong, which is exactly what you want it to be.
And review test code in pull requests the same way you review everything else. A test nobody understands is a test nobody will fix when it breaks.
FAQs
What's the first functional test I should automate?
Pick the flow that would hurt most if it broke tomorrow. For most products that's login or checkout. Automate the happy path and one or two common failures, get it running in CI, then move on to the next one.
Do I need to know how to code to automate functional testing?
It helps a lot. Low-code and record-and-playback tools exist and can get you started, but suites written in code are easier to review, version and maintain once they grow past a few dozen tests.
Which framework should I use?
Use whatever fits your stack. For APIs, your language's standard test runner plus an HTTP client is enough. For browsers, Playwright and Cypress are both solid choices. The habits in this post matter far more than the framework.
How long does it take to automate functional testing?
A first useful set, say ten API tests and two UI tests running in CI, is usually a few days of focused work. Getting to a suite the whole team trusts takes months, mostly because of data, flakiness and ownership rather than writing tests.
Wrapping up
Automating functional testing comes down to a short loop: choose what matters, test it where it lives, control the data, and let CI run it every time. Start with five tests, not five hundred. Get them green, keep them green, and add more each time something breaks. That's slower than it sounds on paper, and it works.


Top comments (0)