
Test-Driven Design Example: Experiment-First A/B Testing

The clearest test-driven design example is a developer writing a check that fails, then writing only enough code to make it pass. Marketers can use the same discipline on experiments: define the pass/fail result before you build the variant. That one habit separates a testing program that produces decisions from one that produces dashboards. Below is a short example from software, then a full worked example of the same loop on a landing page. With Stellar, you can run that loop without waiting on a developer.
TL;DR
- Test-driven development means writing a failing test first, then the minimum code to pass it, then cleaning up. The idea is to decide what "done" means before you build.
- Experiment-first A/B testing works the same way: write the hypothesis, primary metric and decision rule before you touch the page.
- Small, single-change variants are the marketing version of small code steps. They show you what caused a result.
- Write the rule on paper first. Then a visual editor and goal tracking turn it into a live test in an afternoon.
Table of Contents
- What test-driven design actually means
- A short example from code
- The marketing translation: red, green, refactor for experiments
- A worked example: an ecommerce product page
- Small steps and triangulation
- Two rules from TDD that experimenters break most often
- When the test-first approach fits
- Run the loop in Stellar
- Our perspective
- FAQ
- Sources
What test-driven design actually means
Test-driven development (TDD) is a process in which you write a failing test before the code that makes it pass, working in short red-green-refactor cycles, as the University of Maryland's course notes describe it. Red is the failing test. Green is the smallest change that passes. Refactor is the cleanup once everything is green.
The "design" part is why people use the phrase. Writing the check first forces you to decide what the thing should do, including inputs, outputs and edge cases, before you get attached to an implementation. The same course notes make this point. The Google Testing Blog goes further: the test acts as a coded specification, and the place where design starts.
You do not need to write code to use the idea. A marketer's version of a specification is a hypothesis with a number attached to it.

A short example from code
Here is the pattern in its smallest form. The Microsoft Engineering Fundamentals Playbook builds a password validator that must accept strings longer than eight characters containing at least one number. The steps go like this:
- Red. Write a test for empty input. It fails against a stub.
- Green. Add the bare minimum of code to pass it.
- Red again. Add a test for a too-short password. It fails.
- Green again. Add just enough code for that test, and no more.
- Repeat for a missing digit, then refactor, with every test still passing.
The example's key lesson is the discipline of the bare minimum. Each step is small, so when something breaks you know which change did it. The TDD MOOC states the same idea as three rules: no production code without a failing test, no more test than is needed to fail, and no more code than is needed to pass.
That is all the software you need for this article. What matters for you is what this pattern protects against: building first and deciding afterward whether it worked.
The marketing translation: red, green, refactor for experiments
Most teams run experiments in the opposite order. They redesign a page, launch it, watch the numbers move and then pick the metric that tells the best story. Test-first thinking reverses that sequence.
| TDD step | Experiment-first equivalent | What it prevents |
|---|---|---|
| Write the failing test | Write the hypothesis, primary metric, minimum effect worth acting on and the decision rule | Choosing the winning metric after the fact |
| See it fail for the right reason | Confirm the baseline: current conversion rate, traffic and tracking that fires correctly | Measuring a lift against a broken or unknown baseline |
| Make it pass with the minimum change | Build one variant that changes one thing | Not knowing which change caused the result |
| Run all tests again | Check the result against the rule you set, on all pre-agreed goals | Cherry-picking secondary metrics |
| Refactor | Roll out the winner, clean up losing variants, log the learning | A graveyard of half-finished tests |
The Singapore government's Software Delivery Playbook says red means seeing the test fail for the right reasons. For an experiment, the "right reasons" check is your baseline. If your goal tracking is not firing, or the page already converts differently by device, the test is failing for the wrong reasons and you should fix that before you build anything.
A worked example: an ecommerce product page
The following is an illustrative scenario, not a customer case. A store sells a mid-priced item, and product page visitors add to cart at a rate the team has measured. The team suspects shoppers hesitate over shipping costs.
Step 1: Write the failing test
Before opening any editor, the team writes down:
- Hypothesis: Showing the shipping cost and delivery window under the price will increase add-to-cart clicks, because shoppers currently discover shipping late and abandon.
- Primary metric: Add-to-cart rate per visitor.
- Guardrail metric: Completed purchases, so a lift in clicks that does not turn into orders is caught.
- Minimum effect worth shipping: The smallest relative lift that justifies the change. The team chooses this number before launch.
- Decision rule: Run until the pre-set sample is reached. Ship only if the primary metric clears the threshold and the guardrail does not drop.
This is the experiment equivalent of the failing test. It is red because the current page, by definition, does not meet the target.
Step 2: Confirm it fails for the right reason
Next, check that the goals fire on the add-to-cart click and the purchase, and that traffic is split as intended. A mismatch between the planned and observed split is a sample ratio mismatch, the kind of logging error covered in our quick triage guide for SRM, A/A and logging errors. Catching it before launch is cheaper than discovering it after two weeks of traffic.
Step 3: Make it pass with the minimum change
Build one variant: the shipping line under the price, and nothing else. No new headline, no new button color, no rearranged images. This is the marketing version of the bare-minimum rule. If you change four things and the variant wins, you have a winner but no explanation, and you cannot reuse the learning on the next page.
This is also the step where marketers most often lose days waiting for a developer. A no-code visual editor removes that wait, which we cover in A/B testing without dev support.
Step 4: Run it against the rule
When the pre-set sample is reached, compare the result to the rule you wrote in step 1. Resist the urge to stop early because the dashboard looks good on day three. The rule exists because you wrote it before you had a favorite outcome. For reading the numbers, our guide on how to analyze test results covers significance, segments and what to do with a flat result.
Step 5: Refactor
In code, refactoring means improving structure without changing behavior. In experimentation it means three things: ship the winner as the permanent page, delete the dead variant, and record the hypothesis, result and what you learned. The log is what makes the next test better than the last one.
Small steps and triangulation
The TDD MOOC describes a technique called triangulation: as tests get more specific, the code gets more generic. You start with a naive, hard-coded answer, then add tests that expose where the naive answer breaks, and the solution grows toward something general.
Experiments follow the same arc. Your first test on the shipping message is a naive version: one line of text. If it wins, the next tests probe the edges:
- Does it hold on mobile as well as desktop?
- Does it work for new visitors and returning ones?
- Does a free-shipping threshold message beat a flat shipping line?
Each test is small and each one narrows what you know. After three or four, you have a general principle about how your shoppers treat shipping costs, rather than one lucky result. Our agile marketing experimentation guide lays out how to run this as a weekly rhythm.
Two rules from TDD that experimenters break most often
Never change the test and the code at the same time. The xUnit.net documentation says that during one refactoring step you change either the test code or the production code, never both. The marketing equivalent: do not change the success metric or the audience while the experiment is running. If the goal moves mid-test, the result no longer answers the question you asked.
Test behavior, not implementation details. Jamie Ingram warns that tests tied to implementation details are fragile, and that you should check inputs against outputs. For marketers: judge a variant by what visitors do (orders, sign-ups, clicks that matter), not by how the variant is built. A variant that wins on engagement but loses on purchases fails the test you wrote.
When the test-first approach fits
Kent Beck, who popularized TDD, notes that it works when you can predict or discover the inputs, outputs and test cases, and when tests are fast and deterministic. Experiments meet the first condition well: you know the action you want, whether that is a click, an order or a form submission. The second condition is a traffic question. A page needs enough visitors to reach a trustworthy sample in a reasonable time. Low-traffic pages are better suited to larger, bolder changes, with the rule and the metric fixed in advance all the same.
The measurement layer matters as much as the variant. If you also report on campaigns and content, this overview of analytics for content marketing shows how to connect page-level results to wider growth reporting. For the basics, start with marketing analytics for beginners.
Run the loop in Stellar
Every step above needs three things: a way to build a variant quickly, goal tracking you trust and a readout you can check against your rule. Stellar is an A/B testing platform built around those three. It has a no-code visual editor for web and mobile, advanced goal tracking, real-time analytics and a script of only 5.4KB, so the variant you build does not slow the page you are measuring. It connects directly to WordPress, Shopify, Webflow, Wix, Squarespace, Framer and Bubble.

Pricing is based on monthly tracked users. The Sandbox plan is $0 per month and is meant for sites below 25,000 monthly tracked users. Paid plans are Plus at $29 per month, Pro at $99 per month and Scale at $399 per month, with Enterprise available on request. You can see every tier on the Stellar plans page and start your first experiment-first test on the free plan.
Our perspective
Most experimentation programs we hear about do not fail on ideas. They fail on sequence. The page gets built, the test gets launched and only then does someone ask what would count as a win. That is the exact habit test-driven design exists to break.
We built Stellar for the marketer who wants to work in the right order. Write the hypothesis and the goal first. Then use the no-code visual editor to make the one change you meant to make, without a ticket in a developer's queue. Then read the real-time analytics against the rule you set at the start.
Speed matters here for a reason beyond convenience. The shorter the gap between writing the test and seeing the result, the more cycles you complete, and the cycle is where the learning happens. Our script is 5.4KB because we want the measurement to stay out of the way of the experience you are measuring.
If you are new to this, the free plan lets you run the whole loop on a real page. Write the rule, build the variant, watch it resolve, log what you learned.
Juan
FAQ
What is a simple test-driven design example?
The classic small example is a function that validates a password. You write a test for empty input, watch it fail, add the minimum code to pass it, then add a test for short passwords and repeat. The Microsoft playbook walks through this. The marketing parallel is one hypothesis, one metric and one variant per test.
What are the steps of the TDD cycle?
There are three: red, green and refactor. You write a test that fails, write the least code that makes it pass, then clean up while everything stays green. Then you repeat for the next small piece. For experiments, that means write the rule, build the minimum variant, then ship the winner and log the result.
Is test-driven design the same as behavior-driven development?
Not quite. Cucumber's comparison says behavior-driven development describes behavior from the end user's standpoint, with collaborators defining acceptance criteria. TDD checks smaller pieces in isolation and can be done by one person. A marketing hypothesis that states what a visitor will do and why is closer to the behavior-driven style.
Do I need to write code to use this approach in A/B testing?
No. The discipline is in the order of work, not the language. Write the hypothesis, metric and decision rule first. Then use a no-code editor to build the variant. Our guide on the benefits of no-code testing explains how teams run this without engineering support.
How many changes should one variant contain?
One. A single change per variant tells you what caused the result, just as one failing test tells you which code to write. Once a change wins, test the next idea against the new baseline.
Sources
- 23 Software Testing, University of Maryland CMSC389A
- Test first is fun!, Google Testing Blog
- Test-Driven Development Example, Microsoft Engineering Fundamentals Playbook
- Chapter 1: What is TDD, Test-Driven Development MOOC
- Test-Driven Development, Software Delivery Playbook (Singapore)
- Test Driven Development with xUnit.net
- Test-Driven Development: Red, Green, Refactor!, Jamie Ingram
- TDD Prerequisites, Kent Beck
- Understanding the differences between BDD and TDD, Cucumber
- Analytics for Content Marketing: Boosting SaaS Growth
