What to Test (and What Not To)
You can't test everything, and you shouldn't try. Good testing is about covering what matters โ the important logic and the tricky edges โ without drowning in tests for trivial things.
A common beginner reaction to testing is either to test nothing or to try to test everything - both are mistakes. Good testing is about judgement: covering the code that matters (your real logic) and the cases most likely to hide bugs (the tricky edges), while not wasting effort testing trivial things. This session is about aiming your tests where they pay off, so you get most of the benefit for a sensible amount of effort.
assert average([]) == 0 - would have caught it in seconds, long before any user did. It's the perfect illustration of where bugs hide: not in the normal cases the code was written for, but in the edges - empty, zero, first, last - that are so easy to forget and so important to test.Test the logic that matters
Focus your tests on the code that does real work and could plausibly be wrong - the calculations, transformations, and decisions - not the trivial glue.
| Worth testing | Not worth much testing |
|---|---|
| Your calculations & business logic | Trivial getters/one-liners |
| Functions with branches / decisions | Code that just calls a library |
| Data transformations | Constants and config |
| Anything that's been buggy before | Things that can't really go wrong |
The 80/20 rule applies: a handful of well-chosen tests on your core logic catches the large majority of real bugs. Ask of each function: 'could this be wrong in a way that matters, and would a test catch it?' If yes, test it. If it's a trivial one-liner that just hands off to something else, testing it adds little.
Cover the edges
Most bugs hide at the boundaries and in the unusual cases, so those deserve special attention - not just the happy path.
def test_average():
assert average([2, 4, 6]) == 4 # normal case
assert average([5]) == 5 # single item
assert average([]) == 0 # EMPTY - the tricky edge!
assert average([-1, 1]) == 0 # negativesAim tests where they pay off
- Test the logic that matters - calculations, decisions, transformations.
- Skip trivial code - one-liners, constants, plain library hand-offs.
- Cover the edges - empty, zero, negatives, first/last, invalid input.
- The 80/20 rule - a few well-chosen tests catch most real bugs.
๐งฉ Do it with LarzOS
Turn this lesson into action with the exact free tools and AI workflows for the job:
๐ฏ Key takeaways
- You can't and shouldn't test everything - aim tests at the code that matters and could plausibly be wrong.
- Test your real logic (calculations, decisions, transformations); skip trivial one-liners and constants.
- Most bugs hide at the edges - test empty, zero, negatives, first/last, and invalid input, not just the happy path.
- The 80/20 rule: a handful of well-chosen tests catches the large majority of real bugs.
Frequently asked
How much of my code should be covered by tests - is there a target percentage?
There are coverage percentages people cite (you'll hear numbers like 70-80%), and 'code coverage' tools that measure what fraction of your code your tests exercise, but chasing a specific number is less useful than aiming your tests wisely - especially as a beginner. Here's the more helpful way to think about it: coverage percentage measures how much of your code runs during tests, but not whether you're testing the right things or testing them meaningfully, so a high number can give false confidence (you can have 100% coverage with tests that don't really check anything) and a moderate number can be perfectly healthy if the important logic is well tested. What matters more than the percentage is that your core logic and tricky edge cases are covered - the code that does real work and could plausibly be wrong in a way that matters. A project where the crucial calculations and decisions are thoroughly tested, including their edge cases, is in good shape even if trivial glue code is untested and the overall percentage is moderate. Conversely, testing every trivial getter to hit a high percentage wastes effort for little benefit. So rather than targeting a number, apply judgement: make sure the code that matters is tested, cover the boundaries and unusual cases where bugs hide, and don't sweat coverage of trivial code. For personal and learning projects, focus on testing your real logic well and you'll get most of the benefit; you don't need to obsess over coverage metrics. In professional settings, teams sometimes set coverage targets as a rough guardrail (to ensure people write some tests), and coverage tools are useful for spotting entire areas you forgot to test at all - but even there, thoughtful people treat the percentage as a hint, not a goal, because meaningful tests on important code beat a high percentage of shallow ones. The healthiest mindset: test what matters and the edges well, use coverage tools if helpful to find untested areas, but let quality and judgement drive your testing rather than chasing a magic number. A few well-aimed tests on your core logic and its edge cases is worth more than a high coverage percentage of trivial tests.
How do I figure out what the edge cases are for something I'm testing?
Edge cases are the inputs and situations at the boundaries or extremes of what your code handles, and there's a reliable checklist of categories to run through for almost anything you test. For collections (lists, strings, sets), the classic edges are: empty (a list with nothing in it - a huge source of bugs, since code often assumes there's at least one item), a single item (boundary between empty and many), and the first and last elements (where off-by-one errors strike). For numbers: zero (special in division, and often a boundary), negative numbers (does your code assume everything's positive?), very large or very small values, and the exact boundary values of any range (if valid ages are 0-150, test 0, 150, and just outside like -1 and 151). For any input at all: missing or None values, invalid or wrong-type input (text where a number's expected), and unusually large input. A great general technique is to think about the boundaries of each condition in your code: wherever there's a comparison or limit (if x > 10, if the list has items, if the string is long enough), the interesting cases are right at and just past that boundary, because that's where logic tips from one behaviour to another and where mistakes live. Another approach is to ask 'what's the smallest, simplest, and weirdest input this could get?' - the smallest (empty, zero, one item), the simplest normal case, and the weird ones (negatives, invalid, enormous). You can also learn edges from bugs: every time a bug surprises you, note what input triggered it, because that's an edge case you'll want to remember to test in future. Concretely, when testing a function, deliberately ask: what if the input is empty? just one thing? zero? negative? the maximum? invalid? missing? - and write a quick test for each that's relevant. You won't need every category for every function (a function that only takes a positive count doesn't need negative-number tests if negatives can't reach it), so apply the checklist with judgement. But running through 'empty, single, first/last, zero, negative, boundary values, invalid, missing' will surface the vast majority of edge cases for most code, and testing those is where you catch the bugs that the happy path never reveals. With practice, spotting edge cases becomes intuitive - you start seeing the boundaries in code automatically - but the checklist is a dependable way to find them until then.
Related lessons
Writing Your First Tests
Writing a test is simpler than it sounds: call your code, and assert the result is what yoโฆ
Testing & QualityTest-Driven Development
What if you wrote the test *before* the code? It sounds backwards, but writing the test fiโฆ
Debugging & Problem-SolvingCommon Bugs and How to Spot Them
A surprising amount of debugging is pattern recognition โ the same handful of mistakes cauโฆ