- Hackr.io Newsletter
- Posts
- How to Write Tests That Actually Prevent Bugs
How to Write Tests That Actually Prevent Bugs
You can write a lot of tests and still miss real defects. The key is writing tests that target risky behavior, prove key invariants, and catch regressions before they ship.
New at Hackr.io
This week we are focusing on test quality over test quantity. You will learn how to aim tests at real failure modes, keep them fast and focused, and build a suite that blocks regressions without blocking progress.
Partner Message
Marketing ideas for marketers who hate boring
The best marketing ideas come from marketers who live it. That’s what The Marketing Millennials delivers: real insights, fresh takes, and no fluff. Written by Daniel Murray, a marketer who knows what works, this newsletter cuts through the noise so you can stop guessing and start winning. Subscribe and level up your marketing game.
The Scoop
Three Testing Habits That Stop Bugs
Target risky code paths
Identify the modules with the most change, complexity, or customer impact. Write tests that exercise edge cases, not just the happy path.
Assert behaviors, not implementation details
Test outcomes and contracts. Avoid coupling tests to private helpers or exact log messages that change often.
Make failures loud and useful
Each test should fail with a clear message that tells you what broke. Use names that describe behavior, for example
test_cancels_pending_order_on_timeout
Partner Message
Keep This Stock Ticker on Your Watchlist
They’re a private company, but Pacaso just reserved the Nasdaq ticker “$PCSO.”
No surprise the same firms that backed Uber, eBay, and Venmo already invested in Pacaso. What is unique is Pacaso is giving the same opportunity to everyday investors. And 10,000+ people have already joined them.
Created a former Zillow exec who sold his first venture for $120M, Pacaso brings co-ownership to the $1.3T vacation home industry.
They’ve generated $1B+ worth of luxury home transactions across 2,000+ owners. That’s good for more than $110M in gross profit since inception, including 41% YoY growth last year alone.
And you can join them today for just $2.90/share. But don’t wait too long. Invest in Pacaso before the opportunity ends September 18.
Paid advertisement for Pacaso’s Regulation A offering. Read the offering circular at invest.pacaso.com. Reserving a ticker symbol is not a guarantee that the company will go public. Listing on the NASDAQ is subject to approvals.
Full Python Course![]() And test your skills in real time with the free online Python editor. | Get full access to Python with Dr. Johns when you sign up for Hackr Premium. “Robert is a great teacher! The material is concise and easy to follow along with.” Dovi |
Skills
A Quick Example
Here’s a test with narrow coverage:
def test_apply_discount_simple(): assert apply_discount(100, 10) == 90
Here’s how to code better tests, behavior and boundaries:
import math
from hypothesis import given, strategies as st
# Contract: price >= 0, 0 <= pct <= 100, result never negative
@given(
price=st.floats(min_value=0, allow_infinity=False, allow_nan=False),
pct=st.floats(min_value=0, max_value=100)
)
def test_apply_discount_contract(price, pct):
out = apply_discount(price, pct)
assert out >= 0
assert out <= price
# Idempotence for zero and hundred percent
if pct == 0:
assert out == price
if pct == 100:
assert math.isclose(out, 0.0, rel_tol=0, abs_tol=1e-9)
That’s it for today.
Thanks for being part of the Hackr.io community. Keep learning, keep shipping, and keep your test suite fast and fearless.
The Hackr.io Team
P.S.
New here? Browse Python projects to build real-world skills you can bring to any industry.