Tests and quality gates in the pipeline
Which tests go in which pipeline stage, how to set quality gates that ratchet instead of nag, and why a flaky test is worse than no test.
Anatomy of a pipeline: stages, jobs and gatesContinuous integration as a practice
Tests and quality gates in the pipeline
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- Arrange tests by speed and scope: many fast unit tests in the commit stage, fewer integration tests next, and a small set of end-to-end tests against a deployed copy.
- A quality gate is a pass or fail rule the pipeline enforces, such as no failing tests, no critical vulnerabilities, or coverage not lower than the last merge.
- Ratchet gates compare against a recorded baseline, so new code can't make things worse, without demanding that old code be fixed first.
- A flaky test that fails at random trains people to re-run and ignore red builds. Quarantine and fix flaky tests quickly, because a gate nobody trusts protects nothing.
- Some gates belong to people, such as approval before production, but anything a machine can check reliably should be automated.
What the pipeline is for
A pipeline is only as useful as the checks inside it. A green build should mean “this change is very likely safe to release”. If it means less, people stop trusting it and start testing by hand again. If the checks are so slow that a build takes an hour, people batch their changes, and you lose continuous integration.
So the design problem is: get the most confidence for the least time, and make the signal trustworthy.
Which tests go where
The classic guide is the test pyramid: many small, fast tests at the bottom, fewer slower ones above. In pipeline terms:
- Commit stage, minutes. Unit tests, which check one function or class in memory, plus linting and type checks. Thousands of these can run in a minute. They catch most mistakes.
- Integration stage. Tests that talk to a real database, queue or another service, usually started in containers. Testing backend services with real dependencies covers how. Contract tests check that services still agree on their API without running them together.
- Acceptance stage. A few end-to-end tests that drive the deployed system like a user, such as sign up, add to cart, pay. They are slow and more fragile, so keep them to the critical journeys.
- After deploy. Smoke tests, a handful of checks against the real environment right after each deploy: the health endpoint answers, login works.
The pyramid is a guide, not a law. Some teams argue for more integration tests in the middle, sometimes drawn as a trophy. What does not change is the ordering by speed. The testing strategy for a full-stack app works through one real example.
Gates beyond “tests pass”
A quality gate is any rule the pipeline enforces before a change moves on. Typical gates:
- All tests pass.
- Linters and formatters report nothing. Automating the easy checks explains why this saves reviewers time.
- No known critical vulnerability in dependencies.
- Coverage, bundle size or performance not worse than before.
- A person approves before production.
The trap is the absolute threshold. “Coverage must be 80%” sounds reasonable until you apply it to a five-year-old service at 41%. Every merge is blocked, so someone lowers the threshold or disables the gate. A ratchet works better: record the current value, and fail only if a change makes it worse.
A ratchet gate, run for real
I wrote a 16-line Python gate for a small pricing.py. It runs the tests under Python’s built-in trace module, counts which lines of the module executed, compares the result to a baseline stored in baseline.json, and exits nonzero if coverage dropped:
pct = 100 * len(run) / (len(run) + len(missed))
base = json.load(open(baseline_file))["coverage"] if os.path.exists(baseline_file) else 0
print(f"{mod}: {pct:.1f}% of lines run (baseline {base:.1f}%)")
for l in missed: print(" not run:", l.replace(">>>>>>", "").strip())
if pct + 0.05 < base:
print("GATE FAILED: coverage dropped"); sys.exit(1)
json.dump({"coverage": round(pct, 1)}, open(baseline_file, "w")); print("gate passed, baseline updated")
With a baseline of 80% and only one test, for a small order:
pricing: 71.4% of lines run (baseline 80.0%)
not run: raise ValueError("qty must be >= 0")
not run: total = total * 0.9
GATE FAILED: coverage dropped
exit=1
The useful part is not the percentage. It is the list of lines no test touched: the bulk discount and the negative-quantity check, which are exactly the edge cases that cause pricing bugs. I added two tests for them and ran it again:
pricing: 100.0% of lines run (baseline 80.0%)
gate passed, baseline updated
exit=0
The baseline is now 100%, so the next change can’t quietly drop below it. In a real pipeline you would use a proper coverage tool and store the baseline from the main branch, not a local file, but the logic is the same. Coverage tells you what ran, not what was checked. A test with no assertions earns full coverage, so treat it as a map of blind spots, not a score.
Flaky tests break gates
A flaky test passes and fails on the same code. I wrote one deliberately: it starts a background thread that fills a cache after a random 1 to 12 milliseconds, sleeps 10 milliseconds, and asserts the cache is full. That is a race. I ran the suite 50 times with no code change:
passed 43 of 50 runs with no code change
One build in seven goes red for nothing. After a week of that, the team’s reflex on a red build is to click re-run, and the day a real failure arrives, they re-run that too. A flaky test is worse than a missing one, because it damages trust in every other test.
The usual remedy:
- Track which tests fail and then pass on retry. Many CI tools report this.
- Quarantine a flaky test: move it out of the blocking gate so it still runs and reports, but doesn’t block merges.
- Fix the cause, usually timing, shared state between tests, order dependence, a real clock or network, and bring it back.
- Never make automatic retries the permanent answer. They hide the flake and slow the build.
About the video
The embed is a 15½-minute clip from DheerajTechInsight’s “Building Quality Gates in DevOps CI/CD Pipelines” (April 2026, 31 minutes in full). Per its description and chapters, the clip covers the testing pyramid, four automated quality gates (build success, unit tests, code quality checks, integration tests) and manual approval gates, built on AWS CodePipeline with an SNS notification for approval. The later chapters on smoke tests and rollback strategies are left out, since this roadmap covers them in the delivery group. It is a small channel and AWS-specific, but nothing else I found covered gates this directly. I had no transcript, so this is from the title, description and chapters. YouTube lists the video’s original audio as English. Nana’s CI tutorial, whose first nine minutes explain tests as the merge gatekeeper, and a 2026 discussion of the test pyramid’s limits hosted by Emily Bache are under deeper.
Where this fits
A change that passes the gates produces something deployable. The next lesson is about that output: build artifacts and artifact repositories.
Check yourself
Why are end-to-end tests kept few, even though they test the most realistic path?
They are slow, expensive to run and more likely to fail for reasons unrelated to the change, such as timing or test data. A few covering critical journeys give confidence, while most behaviour is checked by faster tests.
Your legacy service has 41% coverage. A gate requiring 80% would block every merge. What can you do instead?
Use a ratchet: record the current coverage as the baseline and fail only if a change lowers it. Coverage can only stay the same or rise, and nobody is blocked by old debt.
A test passes 43 of 50 times with no code change. What should the team do?
Treat it as a bug. Move it out of the blocking gate into a quarantine, find the cause, often a race, shared state or a real clock, and fix it. Leaving it in teaches everyone to ignore failures.
Is 100% coverage a good gate?
Usually not. It shows which lines ran, not whether the assertions were meaningful, and chasing the last few percent produces tests that execute code without checking it. Use coverage to find untested areas, not as proof of quality.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.