←back to thread

Unit tests as documentation

(www.thecoder.cafe)
174 points thunderbong | 2 comments | | HN request time: 0.417s | source
Show context
bunderbunder ◴[] No.41874483[source]
I share this ideal, but also have to gripe that "descriptive test name" is where this falls apart, every single time.

Getting all your teammates to quit giving all their tests names like "testTheThing" is darn near impossible. It's socially painful to be the one constantly nagging people about names, but it really does take constant nagging to keep the quality high. As soon as the nagging stops, someone invariably starts cutting corners on the test names, and after that everyone who isn't a pedantic weenie about these things will start to follow suit.

Which is honestly the sensible, well-adjusted decision. I'm the pedantic weenie on my team, and even I have to agree that I'd rather my team have a frustrating test suite than frustrating social dynamics.

Personally - and this absolutely echoes the article's last point - I've been increasingly moving toward Donald Knuth's literate style of programming. It helps me organize my thoughts even better than TDD does, and it's earned me far more compliments about the readability of my code than a squeaky-clean test suite ever does. So much so that I'm beginning to hold hope that if you can build enough team mass around working that way it might even develop into a stable equilibrium point as people start to see how it really does make the job more enjoyable.

replies(20): >>41874655 #>>41874662 #>>41874705 #>>41875392 #>>41875790 #>>41875904 #>>41875926 #>>41876835 #>>41876977 #>>41877265 #>>41877415 #>>41877434 #>>41877459 #>>41877538 #>>41878062 #>>41878426 #>>41878897 #>>41879455 #>>41879817 #>>41880385 #
wubrr ◴[] No.41874705[source]
> It's socially painful to be the one constantly nagging people about names, but it really does take constant nagging to keep the quality high.

What do test names have to do with quality? If you want to use it as some sort of name/key, just have a comment/annotation/parameter that succinctly defines that, along with any other metadata you want to add in readable English. Many testing frameworks support this. There's exactly zero benefit toTryToFitTheTestDescriptionIntoItsName.

replies(7): >>41874814 #>>41874867 #>>41875382 #>>41876013 #>>41876871 #>>41876888 #>>41877002 #
seadan83 ◴[] No.41875382[source]
What kinds of things would you say are best as annotation vs in the test method name? Would you mind giving a few examples?

Also, are you a fan of nesting test classes? Any opinions? Eg:

Class fibrulatatorTest {

  Class highVoltages{

      Void tooMuchWillNoOp() {}
      Void maxVoltage() {}
} }
replies(2): >>41876463 #>>41881313 #
wubrr ◴[] No.41881313[source]
Like others have already stated/provided examples of[0] - the test function names are generally irrelevant. Many testing frameworks use a single/same test function name, or a completely unnamed function/lambda, while providing any needed context/documentation as params or annotations.

Realistically, many unit tests are far more complicated (in terms of business logic) than functions where names actually matter, like 'remove()', 'sort()', 'createCustomer()', etc. I've worked in several places where people aggressively pushed the 'encode test description in test name' BS, which invariably always leads to names like 'testThatCreatingACustomerFromSanctionedCountryFailsWithErrorX'. It's completely absurd.

> Also, are you a fan of nesting test classes? Any opinions?

It really depends on the framework you're using, but in general nesting of tests is a good thing, and helps with organizing your tests.

[0] https://news.ycombinator.com/item?id=41871629#41877015

replies(1): >>41883124 #
the_af ◴[] No.41883124[source]
> Like others have already stated/provided examples of[0] - the test function names are generally irrelevant. Many testing frameworks use a single/same test function name, or a completely unnamed function/lambda, while providing any needed context/documentation as params or annotations.

I think what you're focusing on is just syntax sugar. Those examples with the 'describe'/'it' pattern are just another way to provide names to test cases, and their goal is exactly the same. If you didn't have this syntactic support, you'd write the function names representing this.

It's exactly the same thing: documenting the test case in the code (so not a separate document), with its name.

The distinction between "comment" and "function name" becomes less relevant once one realizes a function's name is just another comment.

replies(1): >>41883527 #
wubrr ◴[] No.41883527[source]
> I think what you're focusing on is just syntax sugar. Those examples with the 'describe'/'it' pattern are just another way to provide names to test cases, and their goal is exactly the same.

The goal may be the same/similar, but one of the approaches is clearly superior to the other for multiple reasons (as stated by me and other many times in this comment tree). Also, I don't think you quite understand what 'syntactic sugar' means.

> If you didn't have this syntactic support, you'd write the function names representing this.

It's not any kind of 'syntactic support'. Pretty much every modern language/testing framework supports adding free-form test descriptions and names through various means.

> It's exactly the same thing: documenting the test case in the code (so not a separate document), with its name.

It's very clearly not the same at all lmao. And a test name, test description, other useful test documentation/metadata are also not the same.

> The distinction between "comment" and "function name" becomes less relevant once one realizes a function's name is just another comment.

Huge differences between a function name, a comment, and an annotation. HUGE. Read the other comments in this thread to understand why. If you actually worked in an environment where stuffing a test description into a test name is the preferred approach for a non-trivial amount of time, you'd know that once you get past a certain level of complexity your test names explode to 100+ character monsters, if only to differentiate them from the other tests, testing a different combination of states/inputs and outputs, etc.

replies(1): >>41883736 #
the_af ◴[] No.41883736[source]
Sorry, I thought you were debating in good faith. I now see the tone of your responses to everyone here.

Good luck with that!

replies(1): >>41888710 #
wubrr ◴[] No.41888710[source]
Good faith debating is when you actually try to honestly consider and understand the other side's point. Not when you make dismissive blanket (and factually incorrect) statements while refusing to rationally engage with the counter-arguments.

Tone is generally completely independent of good faith.

Go heal that ego and try again.

replies(2): >>41889559 #>>41890705 #
1. seadan83 ◴[] No.41890705[source]
Hate jumping in, though both of you...

The uninviting tone discourages further discussion. I really appreciated where this convo was going until..

> Read the other comments in this thread to understand why.

This could be restated in a non aggressive way. Eg: 'Other comments go into more details why'

> If you actually worked in an environment

Presumptive statements like this are unhelpful. We need to remember we do not know the context and experiences of others. Usually better to root discussion from ones own experience and not to presume the others perspective.

replies(1): >>41892814 #
2. wubrr ◴[] No.41892814[source]
You're right, thanks.