Latest / Software Testing with Fexingo: QA, Automation, and Reliable Software Engineering / How to Test Your Own Code Without Breaking It
Transcript
- Lucas: So last week a buddy of mine — he's a backend engineer at a small fintech — told me about this bug that slipped past him. He wrote a new payment validation function, ran his unit tests, all green, merged it. Two days later, a customer got charged twice. Luna: Ouch. And his tests didn't catch it? Lucas: Right, because his tests were checking exactly what he expected the code to do, not what it actually did. And that's the classic trap of testing your own code — you're too close to it. You have a mental model of how it works, so you write tests that confirm that model, not ones that challenge it. Luna: Yeah, I've seen that a thousand times. It's like when you proofread your own essay — you read what you meant to write, not what's actually on the screen. Lucas: Exactly. And in the testing world, we call that confirmation bias. It's not that developers are careless — it's that our brains fill in gaps. We see the happy path, we miss the edge cases. And for something like payments, the happy path is only half the story. Luna: So what did your buddy do wrong specifically? Was it a missing null check or something? Lucas: The function was supposed to reject any amount over a certain limit. But it only checked the amount, not whether the payment was already in a pending state. So a retry from the same user slipped through. His unit test fed it a valid amount and expected a pass, so it passed. But it never simulated the retry scenario. Luna: So his test was essentially a mirror of his own assumption. Classic. Lucas: Right. And that's why a lot of teams have tried to move away from self-testing altogether — some even enforce that you can't test your own code. But that's not always practical, especially in small teams or for quick fixes. Luna: So what's the alternative? Because I think most of us are stuck with self-testing at least some of the time. Lucas: Well, there's a structured way to approach it. A few years ago, a team at a large e-commerce company I consulted with started using a self-review checklist — not for code style, but specifically for testing. And it cut their escaped bugs by about thirty percent. Luna: Thirty percent? That's significant. What was on that checklist? Lucas: Three things. First, they forced themselves to write at least one test that would fail if the code was wrong. Not just a happy path — a test that actually tries to break it. Second, they had to list every input variable and ask, what happens if this is null, or zero, or negative, or a string instead of a number? Luna: So basically, think like a hacker. Lucas: Exactly. And the third thing was the kicker: they had to write a test that simulated a real-world scenario — like a retry, a timeout, a duplicate request. Not just a function call with parameters, but a sequence of events. Luna: That third one is what would have caught your buddy's bug, right? Lucas: Absolutely. If he'd written a test that first created a pending payment, then called the validation again, it would have failed immediately. And that's the thing — it's not about being a better coder, it's about being a better adversary to your own code. Luna: I love that phrase: being an adversary. I actually use a technique I call 'red teaming my own pull request.' Before I merge, I take a step back and literally try to break the feature — I open the API, send weird requests, try to cause a race condition. Lucas: And does it work? Luna: It's caught more than a few issues. But it takes discipline, because it's tempting to just trust your tests and ship. And there's another layer — time pressure. When you're behind schedule, you're far more likely to skip the adversarial testing and just write the basic test. Lucas: And that's exactly why this episode is worth a minute of your time. If you're a developer who writes tests for your own code, or a QA engineer who relies on developers' self-tests, this could save you from a late-night incident. Luna: And if it does, maybe consider buying us a coffee? A handful of listeners chip in monthly through buy me a coffee dot com slash fexingo, and that literally funds making this many of these episodes. Lucas: Yeah, it's a small group, but it keeps the show ad-free and lets us dig into topics like this without any sponsor strings. No pressure, but if you get value, it means a lot. Luna: Alright, back to the adversary mindset. So where do you start if you're a developer who wants to get better at testing your own code? Lucas: I'd say start with the checklist I mentioned. It's simple: one failing test, edge cases for every input, and one realistic scenario. Write those three before you even write the feature code, if you can. That's a form of test-driven development, actually. Luna: So write the tests first, then code until they pass. That's a paradigm shift for a lot of people. Lucas: It is. And it works because it forces you to think about behavior before implementation. You're not testing what you wrote — you're defining what the code should do, and then writing the minimum to satisfy it. That's a subtle but powerful mental switch. Luna: But even with that, you could still miss something, right? No one can think of every edge case. Lucas: Right. That's why pair review still matters. But there's also a trick: after you write your tests, walk away for an hour, then come back and read the test names like you're reading someone else's code. You'd be surprised how often you spot a missing scenario. Luna: I do something similar — I print my code and review it on paper. It forces a different part of your brain to engage. Lucas: That's a great one. And there's a growing body of research that shows distance from your work — even just time — improves your ability to spot errors. It's the same reason code reviews work. Luna: But what about the practical reality of small teams? You can't always wait an hour or have someone else review everything. Lucas: True. In those cases, prioritize the risk. If it's a payment or auth logic, take the extra time. If it's a display helper, maybe a basic test is enough. It's about allocating your testing budget where the blast radius is biggest. Luna: That's a good rule of thumb. I'd add: document your assumptions in the test. If you're assuming a decimal amount, write that comment. It helps future you and your reviewers see what you were thinking. Lucas: Great point. And one more thing — don't be afraid to write tests that you expect to fail. A failing test is a learning tool, not a signal that you're a bad developer. It's a way to expose the unknown. Luna: So what's the takeaway for someone listening who's about to write tests for their own feature? Lucas: I'd say: treat your own code with suspicion. Write at least one test that tries to break it, and simulate a real-world sequence, not just a function call. That's the difference between testing and just going through the motions. Luna: And if you do that, you might just save yourself from a double-charge incident. Or at least, you'll have a better story for your next standup. Lucas: Ha. That's the upside. But seriously, self-testing is a skill, and like any skill, it improves with practice and the right techniques. The checklist is a good start. Luna: And if you have your own self-testing tricks, we'd love to hear them. You can find us on social or at our website. Lucas: But for now, go write a failing test. It's more fun than it sounds. Luna: And it'll definitely make your future self thank you.