Latest / Software Testing with Fexingo: QA, Automation, and Reliable Software Engineering / How to Write Testable Code From Day One
Transcript
- Lucas: We talk a lot on this show about writing better tests—shift-left, risk matrices, bug taxonomies. But there's a prior question that often gets skipped: Is the code itself testable in the first place? Luna: You mean like, can you even write a unit test without jumping through hoops? Lucas: Exactly. Testability isn't a testing problem—it's a design problem. If your code wasn't written with testing in mind, you're not just fighting flaky tests; you're fighting the architecture itself. Luna: I've definitely been on projects where a simple unit test required instantiating ten objects and a database connection. The test file was longer than the code it was testing. Lucas: Right. That's the smell. And it has real costs: a 2023 study from the University of Zurich found that teams with low-testability code spend 34 percent more time on bug-fixing and refactoring compared to teams that prioritize testability early. Luna: That's a big number. But let's be concrete—what makes code inherently testable? Lucas: I'd point to three principles. First, dependency injection: if your function or class creates its own dependencies internally, you can't substitute them in a test. But if you pass them in, you can mock or stub whatever you need. Luna: So rather than `new Database` inside the method, you accept a database interface as a parameter. Lucas: Correct. Second, interface segregation—meaning you shouldn't have a single fat interface that does everything. If your class depends on a huge interface, your test has to mock methods it doesn't even use. Luna: That leads to brittle tests that break when the interface changes for reasons unrelated to your code. Lucas: Exactly. And third, avoid global state. Static variables, singletons, environment variables—they make tests order-dependent and hard to reason about. Luna: Global state is the enemy of deterministic tests. I've debugged enough 'works on my machine' issues to know that. Lucas: Let me give you a concrete case. There's a fintech startup I've been following—let's call them PayFlow—that processed payments for small businesses. Their original codebase was a Rails monolith with fat controllers, hard-coded API keys, and database calls everywhere. Luna: Sounds like a testing nightmare. Lucas: It was. Their QA lead told me unit tests took three minutes to run for a single module because each test booted the entire framework. And the test suite had a 12 percent flakiness rate. Luna: Twelve percent flaky—that's basically unusable for CI. You'd start ignoring test failures. Lucas: Exactly what happened. Developers began merging with red tests because they didn't trust the suite. So they did a six-month refactor focused entirely on testability. Luna: What specific changes did they make? Lucas: First, they extracted payment processing into a separate service with a clear interface. Second, they replaced environment variables with a configuration object injected at startup. Third, they introduced dependency injection across the board—no more `new` calls inside business logic. Luna: And the results? Lucas: Unit test runtime dropped from three minutes to seven seconds for the same module. Flakiness dropped to under one percent. And here's the kicker: the team's feature velocity actually increased, because developers could safely refactor without fear of breaking something. Luna: That's the hidden dividend of testability—not just better tests, but faster iteration. Lucas: Absolutely. And it ties back to something we've said before: good testing starts before a single line of test code is written. Luna: But what about teams that inherit a legacy monolith? You can't just wave a wand and add dependency injection. Lucas: That's a real constraint. The approach there is called 'seam carving'—you identify points in the code where you can insert an interface without changing behavior. Michael Feathers wrote about this in 'Working Effectively with Legacy Code'. Luna: So you create a test harness around a small piece, get it under test, then gradually widen the coverage. Lucas: Exactly. One pattern is to extract a method that accesses a database, make it virtual, then override it in a test subclass. It's not elegant, but it's a first step. Luna: And eventually you can refactor the production code once the tests give you confidence. Lucas: Right. The key is to never accept 'we can't test this' as a permanent state. Every codebase can be made more testable, one seam at a time. Luna: That's a good mindset. And I think this conversation is exactly the kind of thing that makes our show useful—practical, not just theoretical. Lucas: Glad you said that. Because if you're finding these episodes valuable, there's a small way to keep them coming ad-free and independent. It's at buy me a coffee dot com slash fexingo. Luna: Yeah, listener support is what lets us keep the show focused on the actual problems developers face, not on selling ads. No pressure, just if it's helped you. Lucas: Back to testability. One thing I've noticed is that teams often conflate testability with test coverage. They think a high coverage percentage means the code is testable. Luna: But you can have 90 percent coverage on untestable code—it just means you wrote a lot of brittle, integration-heavy tests. Lucas: Exactly. Coverage measures what you tested, not how easy it was to test. I've seen teams celebrate hitting 80 percent coverage on a monolith, but each test takes five seconds and fails randomly. Luna: So what's a better metric for testability? Lucas: I'd look at two things. First, the time to write a unit test for a new function. If it takes more than a few minutes, there's a design issue. Second, the dependency count per class—more than five dependencies is usually a red flag. Luna: Those are actionable. You can track them in a CI pipeline or an architecture analysis tool. Lucas: Some teams use a 'testability review' as part of code review. Before you merge a pull request, you ask: can I unit test the new logic without mocking the entire universe? Luna: That's a powerful gate. It forces developers to think about design upfront. Lucas: And it catches problems before they compound. The fintech startup I mentioned now requires a testability assessment for any new service before it goes to production. Luna: What does that assessment look like? Lucas: A simple checklist: Is every external dependency injectable? Are there no static method calls to infrastructure? Can you run the unit tests without a database or network? If the answer to any is no, the design gets flagged. Luna: That's actually a short list. I bet most teams could adopt it next sprint. Lucas: Absolutely. And the payoff is immediate—you spend less time wrestling with test setup and more time testing the actual logic. Luna: So to wrap up: testability is a design property, not a testing metric. Invest in it early, and your test suite becomes an asset instead of a liability. Lucas: Well said. And if you're a developer or QA engineer, I'd encourage you to look at your own codebase with fresh eyes tomorrow. Pick one module that's hard to test and ask: what's the smallest seam I can introduce to make it testable? Luna: That's a challenge I'm going to take myself. Thanks, Lucas. Lucas: Thanks, Luna. See you next time.