Hacker News new | past | comments | ask | show | jobs | submit login

> 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.




Some languages / test tools don’t enforce testNamesLikesThisThatLookStupidForTestDescriptions, and you can use proper strings, so you can just say meaningful requirements with a readable text, like “extracts task ID from legacy staging URLs”.

It looks, feels, and reads much better.


With jest (Amonsts others), you can nest the statements. I find it really useful to describe what the tests are doing:

    describe('The foo service', () => {

      describe('When called with an array of strings', () => {

        describe('And the bar API is down', () => {

          it('pushes the values to a DLQ' () => {
            // test here
          })

          it('logs the error somewhere' () => {
            // test here
          })

          it('Returns a proper error message`, () => {
            // test here
          })
        })
      })
    })

You could throw all those assertions into one test, but they’re probably cheap enough that performance won’t really take a hit. Even if there is a slight impact, I find the reduced cognitive load of not having to decipher the purpose of 'callbackSpyMock' to be a worthwhile trade-off.


The `describe`/`it` nesting pattern is quite common (I currently use it in Jest and HSpec); but it doesn't solve the social problem. It's common to see tests like:

    describe("foo", () => {
      describe("called with true", () => {
        it("returns 1", () => {
          assert(foo(someComplicatedThing, true) === 1)
        })
      })
      describe("called with false", () => {
        it("returns 12", () => {
          assert(foo(someOtherIndecipherableThing, false) === 12)
        })
      })
    })
It's the same problem as comments that repeat what the code says, rather than what it means, why it's being done that way, etc. It's more annoying in tests, since useless comments can just be deleted, whilst changing those tests would require discovering better names (i.e. investigating what it means, why it's being done that way, etc.). The latter is especially annoying when a new change causes such tests to fail.

Tests with such names are essentially specifying the function's behaviour as "exactly what it did when first written", which is ignoring (a) that the code may have bugs and (b) that most codebases are in flux, as new features get added, things get refactored, etc. They elevate implementation details to the level of specification, which hinders progress and improvement.


At the end of the day, someone has to shoulder the burden of holding their colleagues to higher standards. I don’t think there’s a technical solution to this social problem.


It could also be a symptom of something else, like I’ve seen this happen when someone goes overboard on unit tests and they become so burdensome that other engineers just want to get it out of the way. They may not consciously realize it, but subconsciously they know that it’s BS and so they don’t mind BS names to just move on with actual productive work.

Not saying it’s always the case, but it could be. Higher standards are not always better, they have diminishing returns.


It's all a spectrum of trade-offs with different people having different opinions.

There could be some sort of formula to explain this better to determine how much effort to spend on tests vs features and product quality and importance of quality compared to that.


This is part of the job of being a team lead or manager. You have a standard, you need to get people to follow it (or consequences..)


Yeah, it doesn't solve the problem of low quality code/laziness, but it's a better tool/approach for documenting your tests than encoding the description/documentation into it's name.

Encoding such information into the name makes about as much sense as encoding constraints into SQL column names.


Yup, I've not actually seen any tool that enforces these kinds of test names. But yeah, trying to encode test description/documentation into it's name is like one of the worst common ways of documenting your tests.


That's not the point of the article. The code should be readable no exception. The only reason we should be ysing x y z are for coordinates ; i should be left for index_what ; same goes for parameters ; they should also contain what unit they are on (not scale, but scale_float) only exception I see are typed languages ; and even then I'm occasionally asked a detail about some obscure parameter that we set up a year ago. I understand it can sound goofy, but the extra effort is made towards other people working on the project, or future self. There is no way I can remember keys or where I left the meaning of those, and there is no justification to just write it down.

Readability of the code makes a lot of it's quality. A working code that is not maintainable will be refactored. A non working cofe that is maintainable will be fixed.


I'm obviously replying to GP's specific comment on test names. I fail to see how your reply relates to my comment at all.


It's funny, you are asking what test names have to do with quality, and you proceed with mentioning a really bad test name, 'toTryToFitTheTestDescriptionIntoItsName', and (correctly) stating that this has zero benefit.

Just like normal code, test methods should indicate what they are doing. This will help you colleague when he's trying to fix the failing test when you're not around. There are other ways of doing that of course which can be fine as well, such as describing the test case with some kind of meta data that the test framework supports.

But the problem that OP is talking about, is that many developers simply don't see the point of putting much effort into making tests readable. They won't give tests a readable name, they won't give it a readable description in metadata either.


> It's funny, you are asking what test names have to do with quality, and you proceed with mentioning a really bad test name, 'toTryToFitTheTestDescriptionIntoItsName', and (correctly) stating that this has zero benefit.

Not at all. Those kinds of names are like a de-facto standard for the people that try to push this kind of practice. Obviously the example I used is not related to any real test.

> This will help you colleague when he's trying to fix the failing test when you're not around.

Really? Encoding what a test function does in it's name is your recommendation for helping someone understand what the code is doing? There are far better ways of accomplishing this, especially when it comes to tests.

> There are other ways of doing that of course which can be fine as well

'Can be fine as well'? More like 'far superior in every possible way'.

> But the problem that OP is talking about, is that many developers simply don't see the point of putting much effort into making tests readable.

Not at all, making a test readable and trying to encode what it does into it's name are completely separate things.


Kotlin has an interesting approach to solving this. You can name functions using backticks, and in those backticks you can put basically anything.

So it's common to see unit tests like

  @Test
  fun `this tests something very complicated`() {
    ...
  }


You can do that in Java as well. Can't remember if it's exactly the same syntax


I don't think you can do it in Java specifically. But once upon a time it was rather popular to write test fixtures for Java code in Groovy, which does let you do it.


My bad, it seems I was misremembering


You can't put spaces in the function name, but you can set a display name for JUnit - https://junit.org/junit5/docs/5.0.3/api/org/junit/jupiter/ap...


My bad, it seems I was misremembering


It's important to this article because its claiming that the name is coupled functionally to what the code tests -- that the test will fail if the name is wrong.

I don't know if any test tools that work like that though.


That's not what the article claims at all.

It claims that, in order for tests to serve as documentation, they must follow a set of best practices, one of which is descriptive test names. It says nothing about failing tests when the name of the test doesn't match the actual test case.

Note I'm not saying whether I consider this to be good advice; I'm merely clarifying what the article states.


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() {}
} }


Table tests can enable useful test naming without a bunch of clunky named test functions. I use them most often in Go but I’m sure other languages have support

https://go.dev/wiki/TableDrivenTests


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


> 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.


> 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.


Sorry, I thought you were debating in good faith. I now see the tone of your responses to everyone here.

Good luck with that!


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.


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.


You're right, thanks.


Dismissive?


> What do test names have to do with quality?

The quality of the tests.

If we go by the article, specifically their readability and quality as documentation.

It says nothing about the quality of the resulting software (though, presumably, this will also be indirectly affected).


> The quality of the tests.

Very insightful, thanks.


Not sure if you felt I was being snarky, but I wasn't.

The article is discussing the quality of the tests, not quality in general and not the quality of the resulting software.

That was my point.


Saying 'stuffing a test description into the function name improves test quality because it improves test quality' is a cyclical, useless statement.

> The article is discussing the quality of the tests, not quality in general and not the quality of the resulting software.

All of my comments in this thread are about unit tests and test quality, not general software quality.

> That was my point.

I still don't see any valid point being made.


Sorry, I replied to you because I thought you were asking how it affected the final product, and I clarified, in case you had missed it, that it was about the quality of the tests as documentation.

Sorry this whole thing seems to upset you so much. Chill!


It's ok if you misunderstood my comment and the context of this comment chain.

No need to get snarky about it and project your own feelings onto others though.


Snarky?

I was actually surprised you reacted with sarcasm.

Why are you trying to pick a fight?




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: