logoalt Hacker News

chriswarbotoday at 2:26 PM3 repliesview on HN

I agree. The same can be said for testing too: their main purpose is to find mistakes (with secondary benefits of documenting, etc.). Whenever I see my tests fail, I'm happy that they caught a problem in my understanding (manifested either as a bug in my implementation, or a bug in my test statement).


Replies

cogman10today at 2:40 PM

This ultimately is what shapes my view of what a good test is vs a bad test.

An issue I have with a lot of unit tests is they are too strongly coupled to the implementation. What that means is any change to the implementation ultimately means you have to change tests.

IMO, good tests are relatively immutable. You should be able to have multiple valid implementations. You should add new tests to describe the new functionality of that implementation, however, the old tests should remain relatively untouched.

If it turns out that a single change to an implementation requires you to change and update 20 tests, those are bad tests.

What I want as a dev is to immediately think "I must have broken something" when a test fails, not "I need to go fix 20 tests".

For example, let's say you have a method which sorts data.

A bad test will check "did you call this `swap` function 5 times". A good test will say "I gave the method this unsorted data set, is the data set sorted?". Heck, a good test can even say something like "was this large data set sorted in under x time". That's more tricky to do well, but still a better test than the "did you call swap the right number of times" or even worse "Did you invoke this sequence of swap calls".

show 2 replies
yrjrjjrjjtjjrtoday at 2:51 PM

The purpose of a car's crumple zone is to crumple.

skydhashtoday at 2:46 PM

Also agree. There’s also a diminishing returns with test cases. Which is why I focus mainly on what I do not want to fail. The goal is not really to prove that my code work (formal verification is the tool for that), but to verify that certain failure cases will not happen. If one does, the code is not merged in.