338. What Code Should I Actually Test?
Read full transcript 20 segments
-
You've been told the right tests. Your team says it, every course seems to say it, and maybe even your boss has a coverage number on a dashboard. But, you have a deadline and a codebase with hundreds of methods. So, which one actually needs tests? Do you need to test the getters, the UI, that method that nobody understands? And how does an AI writing hundreds of tests for you in a minute actually help you and how much does it actually hurt you? So, testing everywhere isn't realistic and testing nothing really isn't safe. So, let's talk about how to decide in today's episode of Dev Questions. [music] Software development is more than just writing code. So, let's talk about the rest of it. Specifically, let's talk about how to decide what deserves a test.
-
And let's start with why we test at all. A test isn't there to hit a number, despite what your boss may say. It's there so you can change code without being afraid of what breaks. To be clear, I said before that getting a job done can mean skipping tests, and that's still true. But when you have to cut, you need to know what tests to cut and which ones not to cut. So, let's start with a mistake that people make when they decide to test. Number one, and again, this is going to be something your boss maybe asking for, but chasing a coverage number. And if your boss is chasing this number, it might be a good video or podcast episode to give to them. So, here's why chasing a coverage number isn't a good idea. First of all, it's it gets gamified, right? So, you're saying, hey, I'm 100% test coverage. What does that really mean? Here's a quick example. Let's say you have one method in your entire application. That's That's all your application is is one method. It's called divide. It takes in two doubles, uh double x to all y, and it said returns x divided by y. Okay? So, code coverage, 100% code coverage on your application would meaning having one test. And that test could pass in a four and a two and expect to get two back. 4 divided by 2 is 2.
-
You now have 100% code coverage. That does not mean anything. Because what happens if you pass in 4 and 0? What's the expectation? Well, you can't divide by zero. You get an exception. So, now your code will throw an exception under certain exceptional circumstances. But yet your code your tests don't cover that even though you have 100% test coverage. Because that number doesn't really mean anything. You're not actually providing value with that number. Instead, you need to be thinking about well that one method should have a few tests on it. Tests like what happens you do four and two? That should be two. What if you had um you know something where you had divide you know one and three? So, 1 divided by 3 that should return point you know 0.33 33 33 repeating forever.
-
How is that work? How much is returned and how is it rounded? And then you should also be passing in you know 4 and 0 because what happens when you divide by zero? Is it a correct expectation that it should turn an exception or should it return a zero or should it return something else? What happens if you pass in negative numbers and so on? You can have a lot of tests on that one simple method. But your code coverage would say 100% with just one test. So, chasing a coverage number it's it's meaningless. Number two, we're trying to test the framework. This is the mistake number two, testing the framework. Why does it matter? You know, testing if if entity framework saves correctly. Well, you're not really testing a whole lot because yeah, it does, right? So if you're testing the save mechanism in entity framework, what's going to happen if that test fails?
-
Are you going to submit a ticket to Microsoft, I guess, because that's some Microsoft bit of code? Or you going to go out to GitHub and try and fix it in entity framework and submit a patch? You know, like that's not really why you testing that. So when you're you know, a property and you're testing that the property actually sets what you asked it to. Well, it's an auto property. Of course it's going to. Why you testing that? If it doesn't do that, then there's a problem in .NET itself. That kind of test coverage is is meaningless. And what it does is it just fills up your test coverage. Again, goes back to that 100% goal, but it also makes for a whole lot of tests that are useless. Number three in mistakes is testing how the code works instead of what it does. And that sounds seemingly the same, right? How it works versus what it does. But how a the the internal details of how you get the job done aren't really what you want to be testing. This is where people try to test private methods and other things.
-
What you want to test is what does it actually do? What are the results? What's the you know, if I give it this input, how does what's the output? Not how does it arrive at that output? Because otherwise what happens is every time you refactor, it's going to break the the the testing versus if you're looking at just the outputs, it doesn't break. And so you have less fragility. And does it matter how it comes through that that output? Because really what you want to know is input in, output out, is it correct? That's you're looking for. And number four in the mistakes is mocking everything. Mocks can be valuable in certain circumstances. if you're not careful, what happens is you start testing the mocks and then tests pass and production breaks with a real database. That's not a good thing. So be very careful when you're using mocks, if you're using mocks at all. Number five, accepting tests that nobody's read. And this is where again the AI comes into play where um you know the AI writes a whole bunch of tests, but what it does is it um it asserts what the code does rather than what the code should do.
-
And that's a that's an important distinction because this is what it does, but that's a bad assumption. Well you wrote the test for the bad assumption, so now the code that has the bad assumption passes the test and so it sound like it works. We got a green check mark, but the reality is we should have a red X. We should be saying no, that's not how it works because that's a bad assumption. You need to prove what you what the the code should actually do, not test what the code actually does. So there's an important distinction there. This is why a lot of people follow the test-driven development style of you write the test, it fails, and then you write just enough to pass the test and then you refactor. So and the reason for that is because if you have a failing test, then you have code that you're writing, you're verifying the code actually satisfies the test. You still need to make sure the test asserts what it should do, but if you are writing the test first, then you're you're not saying this is what the code does because the code's not written yet.
-
I'm not asserting that test-driven development is the way to go, but that methodology of of how you write a test can be very valuable. So number six of mistakes is leaving tests for the end. You know that's first of all it's when deadlines come and you're like well I got to deploy and it's working so therefore I deploy and what you've done is you've not tested anything. Also waiting to test until the end you again are going to assert that the code does what it says you know what what the code actually does versus what it should do and you are going to also forget some of the things that the code should be doing so there's problems there. So let's decide um what to test and we're going to order this based upon the the highest value to the lowest value. So the most important thing first to the end. Okay the most important thing is um if you ask what would will this cost if it breaks. So um this might be a simple example is if you are taking money and depositing into a bank account um and you have a method to do this what would happen if that didn't work correctly? What if it doubled the money that went into the bank account from the deposit?
-
That'd be a problem right? So we should make sure we test this thoroughly to make sure that it is not going to break. Um what if you know if we don't test this and it breaks we're going to have a 2:00 a.m. phone call. That's kind of a big deal. I don't want that. I'm going to make sure that um I test those things thoroughly because I don't want to get up at 2:00 in the morning because a a backup job that that's really important to the system but is something that runs nightly is breaking. I want to test those things. So test what would cost you the most money or the most time. Um if it's expensive make sure you test it. Um number two business rules. Test business rules. If a business has a rule um make sure that it's tested. For example imagine you have a site that is I don't know selling courses um maybe like devforge.com. Um so you you might have multiple different discounts going on at any given time. Maybe you have a Black Friday sale like we have going on right now.
-
Um, or you might be also have like a a new user gets a discount for certain things, and and combine those discounts, if you're not careful, those discounts might combine into a negative number. Um, this is not a good thing. You do not want as a business owner to figure out that you're paying people to take your products. Just not good. So usually testing those business rules. Make sure that you know different combinations can lead to a negative price. Uh, make sure that different combinations don't lead to below a certain number if that's one of your business rules. You know, so maybe you say we'll never give more than 50% off. So if I have this coupon and this coupon and this discount and I combine them all and they go more than 50% you're going to have to a rule that says, hey, don't allow that and let the user know. And so you're going to test that to make sure it actually happened. So test business rules.
-
Number three, every bug you fix. So every bug you fix you should write a test for, ready to fail first, verifying that what happens actually happens. Here's a you know a quick note. Sometimes when you find a bug you get all excited like I found the bug and when I do this this happens. Test that because sometimes what you find it is not actually where you thought it was. It may be causing that bug, but maybe it's the browser. You know, so it's only in Edge and not in Firefox that this happens. So the issue happens this certain spot in the code, but only in Edge, not in Firefox. So like you want to test it first because then you're testing your assumptions. Make sure the test fails. Make sure it says yeah, you expected you know this number back and got that number instead. Make sure that the test as the code is written fails. [clears throat] Then fix the code and make sure that the the test now passes because now you've verified that your fix actually fixed the problem.
-
That also means that you have one more test in your test bank that verifies that bug will never come back, at least not that way, because you're testing to make sure that bug doesn't happen. This is how your test bank grows over time. Number four, code that changes often or that many things depend on. I don't know how many times I've changed code where I change one little thing and I have a cascade of issues, because I didn't realize that it wasn't just this one thing that was calling that method, but there were things, you know, in different apps or in different places all over that were also calling that method. And all of a sudden, now I've got to figure out how do I unwind all of this and and maybe they're working off of bad assumptions, but, you know, that's how they now work, right? So making it right might actually cause things to break. And so you want to test those things. So if you're testing things that change and, you know, many things depend on, well then when other things depend on that and you make a change and you say, oh, this should solve this problem, maybe it's an internal fix, make sure the outputs are still what's expected.
-
Because if they are, then you should be good or at least you should know that those other things are depending on a bad value. Number five, edge cases. Edge cases are messy. I'll give you one edge case that you probably haven't thought about and it only happens once every four years. And that is leap year. So leap year is the year when and there's a whole formula for figuring out what when a leap year is, when it's not. But that leap year is the year when we add an extra day. So we have a February 29th when normally don't. That messes with everything. The problem is is that if you put a fix in place, you say, well, yeah, that that should solve you know, the the leap year problem, but this is in October and it's a year and a half until leap year. How do you know? You need to test that thing because what you don't want to have happen is things that break, go down, have bad data on things you have to unwind all because you have February 29th instead of March 1st.
-
So leap year's one. Um then there's things like, you know, what happens when you cross midnight? Uh maybe the order gets placed at 11:59 p.m., but then gets filled at, you know, 12:01 a.m. Um so make sure those things are right. Like there can be some weird boundary issues there. Testing null and zero and empty and all the rest of these edge cases that can happen and will happen. The the hard part about edge cases is they don't happen all the time and so by writing tests, we can recreate them all the time and then make sure they are fixed from now on. Number six, legacy code you're about to change. This is one where let's say you have a .NET framework app and you're going to upgrade this thing. You're going to make sure that, you know, you're upgrading the library, the library of code that's got all the business logic and all the data access logic and all the other things that, you know, are really a big deal and it's the heart of your application.
-
When you go to change those and upgrade or or make a change to how things work, test it first. Have all the tests in place to make sure that your tests kind of pin in place what's happening today and this is how things work because then as things change, you can verify that the underlying logic hasn't changed. Like you know, maybe the code changes from being .NET framework 4.8 to .NET 11, but the actual logic hasn't changed at all. You need to make sure that's the case. Otherwise, you can have some really weird issues that are really hard to track down. Uh number seven, integration tests. So this is integration tests are larger than unit tests and you're going to want to test where the risks live. So this is uh your database, your API calls, configurations.
-
You want to make sure that you're testing these things not just isolated, not just testing one method at a time, but putting those things together. Um I don't have a visual illustration for you, but I've seen an image before online where, you know, you have uh a kitchen and the dishwasher can open or a cabinet can open, but you can't do both at the same time. So if you tested the dish dishwasher opening and closing, that's a unit test. If you tested that the cabinet could open and close, that's a unit test. Opening both of them at the same time, that's an integration test. And that's what fails even though those unit tests passed, right? So you want to make sure that you have integration tests in place. Just be careful because integration tests, end-to-end tests, uh UI tests, these are very expensive tests to write and they get more expensive the get larger because the fact that when you change how your system works, these tests break.
-
And that can be a good thing in the fact that you're changing your logic and it's saying, yeah, your logic changed. But that can be frustrating to maintain and it takes up more work, which is why this is lower on the list, but it's still important to do for certain areas. So the rest of these um go really light on. You know, if you're doing trivial getters, no, don't test those or if you really have to test it a little bit, um meaning maybe it does more than just a get, maybe it does a little bit of logic, test a little bit of logic, but be careful. UI layouts, don't test a UI layout unless you absolutely have to because UI layouts change all the time. A designer comes in, shifts things by five pixels, you can have problems. Or designer says, hey, let's put this navigation bar on the side instead of the top. Breaks things, right? Um be very careful with that. Um and then here's an important bit. And this comes near the end of this list.
-
Delete certain tests. Delete tests that cost more than they protect. So remember, tests are there to protect you. If they cost more than they give you, don't do them. This is where again your test coverage shouldn't be 100% because you're testing stupid stuff. They're flaky, they're slow, they're not testing anything. They're they're bogging you down, they're making you maintain code that you don't need to worry about. So delete them. Okay? Um the other thing to remember is that even if you have all of these automated tests, even if you have tons of unit tests and some integration tests and UI tests and end-to-end tests and all these tests, load tests, um tests aren't the only type of check you need to do. And some of testing is giving it to an actual person and having them try to break it. Because yes, some of these things can be put into automated tests, but sometimes you just need to put in the hands of a user and watch them break it.
-
Because that will give you the the assurances you need that you are testing the last little bit that's that's not really something you can just put automation around. So the bottom line is this. Tests are insurance and you don't insure everything equally. Before you write a test, ask what would cost if the test broke. Test the money, test the data, test the bugs you fix, and then go light on the rest. And when AI writes your tests, read them like it's real code because it is real code and you need to understand what it does and what it's actually testing because a lot of times you'll find it's not testing what you wanted to test. So don't chase a number. Don't chase the coverage count. Write the tests that change that allow you to change your code without fear. That's the big thing. Write your insurance, pay for your insurance so that when the problems happen, your insurance pays for it, not your production users.
-
Thanks for listening. As always, I am Tim Corey.
Summary
This episode of Dev Questions explores how to strategically decide which parts of a codebase actually deserve tests under time constraints. It highlights common testing mistakes, including chasing meaningless coverage numbers, testing frameworks, focusing on internal mechanics rather than outputs, over-using mocks, and blindly accepting unread AI-generated tests. The practical takeaway is that testing should provide the confidence to change code safely by focusing on inputs and correct expected behavior rather than trying to achieve arbitrary metrics.