←back to thread

Unit tests as documentation

(www.thecoder.cafe)
174 points thunderbong | 1 comments | | HN request time: 0.211s | source
Show context
jaredcwhite ◴[] No.41872507[source]
I very much disagree with this.

Good code can be documentation, both in the way it's written and structured and obviously in the form of comments.

Good tests simply verify what the author of the test believes the behavior of what is being tested should be. That's it. It's not documentation, it rarely "explains" anything, and any time someone eschews actually writing documentation in the form of good code hygiene and actual docs in favor of just writing tests causes the codebase to suffer.

replies(1): >>41874406 #
RangerScience ◴[] No.41874406[source]
I disagree in a very specific and limited way: good tests show you how to use the code, which can be as simple as just “here’s some typical parameters for this function.”

In more complex situations, good tests also show you the environmental set up - for example, all the various odd database records the code needs or expects.

It’s not everything you’d want out of a doc, but it’s a chunk of it.

replies(2): >>41875127 #>>41875744 #
bluefirebrand ◴[] No.41875127[source]
> good tests show you how to use the code

If you can't find examples of how to use the code in the code then why does the code even exist?

replies(2): >>41875986 #>>41877941 #
1. badmintonbaseba ◴[] No.41877941[source]
There are multiple problems with that:

1. The code uses internal interfaces, not meant to be used by users of the code.

2. The code might not use the high level public interfaces you are interested in. Those interfaces are meant to be used by users, and tested by tests.

Having said that reading the code itself is often fruitful. Not for example usages, but to just learn how the thing is implemented.