Latest / Software Testing with Fexingo: QA, Automation, and Reliable Software Engineering / Why Test Automation Must Handle State Transitions
Transcript
- Lucas: If today's tech conversation gave you something usable—a new testing pattern, a debugging trick, something you can actually apply—then I want to be straight with you about how this show stays on the air. Luna: Let me guess—no ads, no corporate sponsor, just a small group of listeners keeping it going? Lucas: Exactly. A handful of people chip in monthly at buy me a coffee dot com slash fexingo, and that's what funds the research, the editing, the whole operation. It's a pretty lean model, but it works because of them. Luna: And we don't ask often. But if you've been listening for a while and getting value, it's a great way to keep this ad-free. Lucas: Alright—back to testing. Today I want to talk about a bug class that's cost me more late nights than almost anything else: state transitions. The kind where a test passes in isolation, but fails in a suite because the app is in a different state than you assumed. Luna: Oh, I've lived that. You run the test—green. Then you run the whole suite—red. And the flaky test dashboard blames you. Lucas: Right. And it's rarely a flaky test. It's a state dependency that your test script didn't account for. Let me give you a concrete example. A major retailer—I can't name them, but think top-five US e-commerce—had a holiday promotion where certain items were supposed to be discounted 20 percent. Their automation suite ran fine in staging. But in production, during the first hour of the sale, some items showed up at 90 percent off. Luna: Yikes. That's a fire drill. Lucas: It was a fire drill. The root cause? A state transition bug. The user flow was: browse, add item, go to cart, apply promo code. But the automation suite had a shared session cache. One test would add an item, then the next test—expecting a fresh cart—would inherit that same session. The promo code from the first test leaked into the second, and the system applied the discount twice. Luna: So the second test thought it was starting from a clean slate, but the app was still in the 'promo applied' state. Lucas: Exactly. And the fix wasn't to write more tests. It was to model the states explicitly. The team built a state machine—literally an enum of possible states: browsing, cart empty, cart populated, promo applied, checkout. Then they made every test declare its starting state and verify its ending state. If a test tried to run from 'promo applied' when it needed 'browsing,' the framework would reset the session first. Luna: That feels obvious in hindsight, but I've worked on teams where 'state' was just whatever the UI showed at that moment. Lucas: And that's the norm. Most test automation treats state as implicit. You navigate to a page, you assume the page is in a known configuration. But the browser might have cached data, the server might have a stale session, or—like in this retailer's case—a previous test left a footprint. State transition testing forces you to make that explicit. Luna: I've got a story from healthcare tech that fits here. A startup I consulted for built a patient portal where users could view lab results. The test suite was written in Cypress, and every test started by logging in, navigating to the results page, and checking a specific result. Green all day. Lucas: Classic happy-path testing. Luna: Exactly. But in production, users don't always go straight to results. They might open the portal, get distracted, come back—and the session has timed out. Or they click 'back' and land on a stale page. One user pattern was: view a result, then immediately view a second result from a different date. The UI had a state machine for showing results, but it assumed you'd always close one before opening another. Lucas: So the state machine itself was buggy. Luna: Yeah. The 'current result' state didn't reset when the user selected a new one. So the second result would render on top of the first, with overlapping text. Patients couldn't read the values. The test suite never caught it because every test only checked one result at a time. They never tested the transition from 'result A' to 'result B'. Lucas: That's a classic state transition gap. And it's exactly why line coverage or even branch coverage won't save you. You need transition coverage. How many state transitions have you tested? From state X to state Y, not just the states themselves. Luna: How do you measure that practically? I've seen teams try to map every possible transition, and it becomes a spider web. Lucas: You don't need all transitions. You need the risky ones. Start with the ones that involve user actions that could happen in any order—like adding an item, applying a promo, removing the item, then changing the promo. Or, in Luna's healthcare case, viewing one result, then another without closing the first. A good heuristic is to look at your state machine and ask: which transitions cross between 'modes' of the application? Those are the ones that break. Luna: So you prioritize transitions that go from a non-default state to another non-default state. Lucas: Right. The default state—like 'browsing' or 'logged in with no results'—is safe because every user starts there. But transitions like 'promo applied' to 'cart empty' are dangerous because the system might not clean up the promo flag. Or 'result A displayed' to 'result B displayed' might not clear the old data on screen. Luna: How do you even model that in a test framework without making everything super complex? Lucas: The simplest approach is a state enum and a precondition check. In your test setup, you call a function like 'ensureState'. That function checks the current app state—maybe by reading a hidden DOM attribute or an API endpoint—and if it's not the expected state, it performs resets. That could be clearing cookies, reloading the page, or calling a test-only API to reset the server session. Luna: That's what the retail team did. They added a reset endpoint that wiped the session. Lucas: Exactly. But the key is that the reset is explicit. You don't assume the test starts clean—you enforce it. And then after each test, you verify the end state. So you're not just checking that a button works; you're checking that the system is in the expected state after the transition. Luna: One thing I've seen teams struggle with is maintaining the state model. As features get added, new states appear. How do you keep the enum from ballooning? Lucas: You don't model every micro-state. You model user-visible states. If the user can perceive a difference, it's a state. If it's an internal loading spinner that lasts 200 milliseconds, that's probably not worth a state. Also, you can group states. For example, 'cart with items' and 'cart with promo applied' might be separate states, but 'cart with one item' and 'cart with two items' might be the same state if the behavior is identical. Luna: So the granularity is driven by risk. Lucas: Yes. And by the transitions that have historically caused bugs. For the retail team, the risky transitions were any that involved the promo code. For the healthcare team, it was switching between results. So you focus your transition coverage there. Luna: I want to circle back to something you said earlier—the idea that tests should declare their starting state. Does that mean every test has a precondition that resets the state? Lucas: Ideally, yes. But pragmatically, you can have a default reset that runs before each test—like a beforeEach that calls 'ensureState'. Then only the tests that need a non-default state declare a different precondition. That keeps the boilerplate low. Luna: And how do you handle state in parallel test runs? If tests run in parallel, they might step on each other's sessions. Lucas: That's a whole other layer. The safest approach is to isolate sessions per test—either by using unique user accounts or by running each test in a separate browser context. Some frameworks like Playwright support browser contexts natively, so each test gets a fresh state. But if you're sharing sessions, you absolutely need the state model to reset after each test, or you'll get cross-contamination. Luna: I've seen teams solve that by running tests sequentially on the same session, which defeats the purpose of parallelization. Lucas: Right. But with state transition testing, you can actually run sequential tests that build on each other—like a test that logs in, then a test that adds an item, then a test that checks out—as long as the state machine is explicit about what state each test expects. That's a valid pattern for end to end flows. But if you want parallel, you need isolation. Luna: Let's talk about tooling. Are there tools that help with state transition testing, or is it mostly a design pattern? Lucas: It's mostly a design pattern, but some frameworks support it. For example, in Cypress, you can use custom commands to wrap state checks. In Playwright, you can use fixtures to manage state. There's also a library called 'Stately' that lets you model state machines in JavaScript and use them in tests. But the core idea is simple: make state explicit, test transitions, not just states. Luna: I've also seen teams use property-based testing to generate random transition sequences and check for invariants. That's like fuzzing for state machines. Lucas: Yes! That's a really advanced technique. You define the state machine and the valid transitions, then a tool like 'fast-check' or 'Hypothesis' generates random sequences. Then you check that certain invariants hold—like 'total price never negative' or 'logged-in user always sees a logout button'. That catches edge cases no human would think to test. Luna: But that requires a formal state model upfront. Lucas: It does. But if you've already built the enum and the transition map, you're halfway there. The property-based approach just automates the exploration of the transition graph. Luna: So the takeaway for our listeners: if you're dealing with flaky tests that pass in isolation but fail in a suite, look at state. Model your application's user-visible states, test the transitions between them, and reset explicitly. Lucas: And don't assume the test starts clean. Check. Because the bug that costs you a 90-percent discount might be hiding in a state you forgot to reset. That's it for this episode. Next time, we'll talk about contract testing for microservices—how to catch integration bugs before they reach production. Luna: Looking forward to it. Until then, happy testing.