Sub topic 17
Validating and testing requirements
This chapter assumes you can already write functional requirements and measurable non-functional requirements (NFRs). It also assumes you know what SLIs and SLOs are and can read short Python snippets. Here you learn how to check a set of requirements before design starts, and how to turn each requirement into a check that can pass or fail.
Why validate requirements
A defect in a requirement costs more than a defect in code. If a requirement is wrong, vague or contradictory, the team builds the wrong thing correctly. Nobody finds out until users complain or an incident review asks why the system was never expected to meet the target.
Validation is a review of the requirements themselves. It asks two questions:
- Are these the right requirements? This is validation in the strict sense. Stakeholders confirm that the set matches what they need.
- Are these requirements well formed? This is verification of the document. The set is clear, testable and consistent.
You need both. A perfectly worded requirement that nobody wants is waste. A wanted requirement that nobody can test is a wish.
Quality checks for a single requirement
Check each requirement against the following properties.
| Property | Question to ask | Failing example |
|---|---|---|
| Unambiguous | Can two readers interpret it differently? | "The service should be fast." |
| Testable | Can you define a pass/fail check? | "The system should be user friendly." |
| Measurable | Does it have a number, a unit and a window? | "High availability." |
| Atomic | Does it state one thing only? | "Users can create and delete short URLs and see analytics." |
| Necessary | Does a stakeholder need it? | A feature added "just in case". |
| Feasible | Can it be met within the stated constraints? | "Zero downtime" with a single server. |
| Traceable | Does it have an ID and a source? | An unnumbered bullet in a slide deck. |
A measurable NFR names five things: the quality, the metric, the threshold, the percentile or ratio, and the measurement window and location. Compare these two:
- Weak: "Redirects must be quick."
- Strong: "99% of redirects complete in under 100 ms, measured at the service edge over 30 days."
The strong version says where to measure (the service edge). It also says what to measure (completed redirects) and over what period (30 days). Without the location, a team could measure inside the application and ignore network and load balancer time.
Checks across the whole set
A set of requirements can fail even when each item is fine. Look for these problems.
- Conflicts. One requirement makes another impossible. For example, "99.99% availability" combined with "a single database in one data center" conflicts, because the downtime budget is about 4.3 minutes per 30 days.
- Gaps. An important quality is missing. Many documents list functions and latency but say nothing about durability, security or operability.
- Duplicates. Two requirements say the same thing with different numbers. Later, nobody knows which number is correct.
- Hidden assumptions. The document relies on facts that are not written down, such as peak traffic or data retention.
- Scope leaks. A requirement quietly contradicts the out-of-scope list.
A simple lint for requirements documents
You can automate part of this review. A script cannot judge whether a requirement is right, but it can catch missing IDs, duplicate IDs, missing numbers in NFRs and vague words. The example below runs on the requirements document format used earlier in this tutorial.
import re
VAGUE_WORDS = {"fast", "quick", "scalable", "robust", "user-friendly",
"efficient", "highly", "reliable", "easy"}
def lint(doc):
"""Return a list of problems found in a requirements document (a dict)."""
problems = []
seen = set()
for section in ("functional", "non_functional"):
for req in doc.get(section, []):
rid = req.get("id")
if not rid:
problems.append(f"{section}: requirement without an id")
continue
if rid in seen:
problems.append(f"{rid}: duplicate id")
seen.add(rid)
text = req.get("text", "")
words = set(re.findall(r"[a-z-]+", text.lower()))
vague = sorted(words & VAGUE_WORDS)
if vague:
problems.append(f"{rid}: vague words {vague}")
if section == "non_functional" and not re.search(r"\d", text):
problems.append(f"{rid}: no number in a non-functional requirement")
return problems
doc = {
"functional": [
{"id": "FR-1", "text": "A user can submit a long URL and receive a short URL."},
],
"non_functional": [
{"id": "NFR-1", "quality": "latency",
"text": "99% of redirects complete in under 100 ms, measured at the service edge over 30 days."},
{"id": "NFR-2", "quality": "availability",
"text": "The service should be highly reliable."},
{"id": "NFR-2", "quality": "durability",
"text": "A created short URL is never lost once creation has been acknowledged."},
],
}
for problem in lint(doc):
print(problem)Running it prints:
NFR-2: vague words ['highly', 'reliable']
NFR-2: no number in a non-functional requirement
NFR-2: duplicate id
NFR-2: no number in a non-functional requirementThe last line is a false positive. NFR-2 (the durability one) has no number because "never lost" is an absolute statement, not a threshold. This shows the limit of automation: the lint flags candidates, and a person decides. Also note that "never lost" is itself worth challenging. It is hard to prove, so a reviewer might ask for a concrete target such as the number of replicas or a recovery point objective.
Turning requirements into tests
A requirement is testable when you can describe the test before the system exists. Different kinds of requirement need different kinds of check.
| Requirement type | Example | How to test |
|---|---|---|
| Functional | Requesting a short URL redirects to the long URL | Automated functional test that asserts the status code and the `Location` header |
| Latency | 99% of redirects under 100 ms | Load test that records latencies and computes the 99th percentile |
| Availability | 99.9% success over 30 days | Production monitoring of the success-ratio SLI against the SLO |
| Durability | No acknowledged URL is lost | Failure injection: kill a node after acknowledgment and check the data |
| Security | Only the creator can delete a short URL | Authorization tests using a second user's credentials |
| Scalability | Handle 7,000 requests per second at peak | Load test at the peak rate, then at a multiple of it |
Some requirements cannot be fully tested before launch. A 30-day availability target can only be confirmed after 30 days of operation. For these, you test the mechanisms that support the target before launch, and you monitor the SLI afterward.
Here is a pass/fail check for the latency requirement, built on measurements from a load test.
import math
def percentile(samples, p):
ordered = sorted(samples)
rank = math.ceil(p / 100 * len(ordered))
return ordered[rank - 1]
def check_latency_slo(samples_ms, p=99, limit_ms=100):
observed = percentile(samples_ms, p)
return observed <= limit_ms, observed
samples = [12, 15, 14, 18, 20, 22, 25, 30, 40, 250]
ok, observed = check_latency_slo(samples)
print("PASS" if ok else "FAIL", f"p99={observed} ms")This prints FAIL p99=250 ms. With only ten samples, the 99th percentile equals the maximum, so one slow request fails the check. A real load test needs thousands of samples before a p99 is stable.
Acceptance criteria
For functional requirements, write acceptance criteria in a given/when/then form. Each criterion becomes one test case.
id: FR-3
text: A user can delete a short URL they created.
acceptance:
- given: user A created short URL abc123
when: user A deletes abc123
then: the response is success and later requests for abc123 return 404
- given: user A created short URL abc123
when: user B tries to delete abc123
then: the response is 403 and abc123 still redirects
- given: abc123 does not exist
when: user A deletes abc123
then: the response is 404The second and third cases cover edge cases the requirement text did not mention. Writing acceptance criteria is a validation technique in its own right. When you cannot write the "then" line, the requirement is incomplete.
Review techniques
Combine several methods, since each finds different problems.
- Walkthrough with stakeholders. Read each requirement aloud and ask the owner to confirm it. This finds unnecessary and wrong requirements.
- Peer review with a checklist. Use the properties table above. This finds ambiguity and missing numbers.
- Prototype or mock. Show a mock API or screen. People notice missing behavior when they can see it.
- Back-of-envelope check. Run the capacity estimate. If the numbers imply a cost or size nobody expected, a requirement is probably too strict or an assumption is wrong.
- Traceability check. Every requirement should trace to a stakeholder need and to at least one test. Every test should trace to a requirement. An untested requirement or an unowned test points to a gap.
Common errors
- Validating only once. Requirements change. Re-run the checks whenever scope or assumptions change.
- Treating the lint as a review. Tools catch form problems, not wrong requirements.
- Testing with unrealistic data. A latency test on an empty database says nothing about production behavior.
- Averaging instead of using percentiles. A mean of 25 ms hides the 250 ms outlier in the sample above.
- Leaving NFR tests until the end. A load test in the last week finds problems that need architectural changes.
Exercise
A requirements document says "The system must handle high traffic and respond quickly." Rewrite it as two measurable requirements for a URL shortener, and name one test for each.
Show answer
Example answer. Throughput: "The service sustains 7,000 redirect requests per second at peak with a success rate of at least 99.9%, measured at the load balancer." Test: a load test at 7,000 requests per second, then at a higher multiple, checking the success ratio. Latency: "99% of redirects complete in under 100 ms, measured at the service edge over 30 days." Test: a load test that records latencies and computes the 99th percentile, followed by production monitoring of the same SLI. Any version is acceptable if it has a number, a unit, a measurement location and a window or load level, and if the test can fail.
Exercise
Why can a lint script not replace a stakeholder review of requirements?
Show answer
A script checks form: missing IDs, duplicates, vague words and missing numbers. It cannot know whether a requirement matches what stakeholders need, whether a number is realistic, or whether two requirements conflict in meaning. Those are validation questions that need people.