5 ms·
Test names should be sentences: https://bitfieldconsulting.com/posts/test-names https://bitfieldconsulting.com/posts/test-names
by gus_leonel 2y ago
Test names should be sentences: https://bitfieldconsulting.com/posts/test-names https://bitfieldconsulting.com/posts/test-names
- nuancebydefault 2y agoIn fact that is not very hard to do and provides a great advantage!
- wnmurphy 2y ago100%. Test names should include the word "should" and "when". Then you get a description of the expected behavior.
- wubrr 2y agoShallow/meh, article. Demonstrates complete lack of familiarity with many popular testing frameworks/approaches, and proposes a subpar solutions for problems that have already been solved in superior ways. Your test description/documentation should be sentences, but there is absolutely zero reason to try to encode that into the name of your test function. Not to mention this article then suggests using another tool to decode this function name into a proper sentence for reporting... ok now you completely lost the ability to ctrl+f and jump to the function... terrible advice all around. Why not just use a testing framework that actually supports free-form sentence descriptions/documentation for your tests?
- bunderbunder 2y agoIf my unit testing framework supports free-form sentence descriptions, I'll use it. But I won't use that feature as a wedge issue. It doesn't bother me all that much to have test functions with names like `test_transaction_fails_if_insufficient_funds_are_available()`. Other features of the test framework might have a much bigger impact on my developer experience.
- wubrr 2y agoPretty much every major language/framework now supports free-form test names/descriptions, including JUnit, which is referenced in the article ^ (again, highlighting the author's ignorance). Just because something doesn't bother you personally doesn't mean it's a good thing to follow, especially when its clearly inferior to other options. > It doesn't bother me all that much to have test functions with names like `test_transaction_fails_if_insufficient_funds_are_available() I mean, that's one example where you have one outcome based on one parameter/state. Expand this to a 2-field outcome based on 3 state conditions/parameters and now you have a 100-character long function name.
- bunderbunder 2y agoYou seem to be focusing on the most obtuse possible way of structuring descriptive test names for rhetorical purposes. I don't know if that's intentional or not, but either way it's not terribly convincing? If you name tests according to business domain concepts rather than tying it to individual parameters through some formulaic rubric, it's often possible to come up with test names that are both more concise and easier to understand than the specific way of naming tests that you've been consistently steering the conversation back toward throughout this thread.
- wubrr 2y ago> You seem to be focusing on the most obtuse possible way of structuring descriptive test names for rhetorical purposes. No, I'm focusing on the most realistic and common ways this kind of pattern actually exists (based on my experience). > If you name tests according to business domain concepts rather than tying it to individual parameters through some formulaic rubric, While you say I'm focusing on 'the most obtuse possible way...', this kind of comment makes it seem like you haven't focused on any actual way at all. You're speaking in very ambiguous and vague terms, which actually can't be applied and enforced in practice. If you're actually trying to write a suite of unit tests around, say a function, with 3 parameters and multiple possible outcome states - you can't name your function the same for the different combinations of inputs/outputs, and you can't just handwave a 'business domain concepts' name into existence to cover each case - that just turns into an exercise of finding synonyms, abbreviations, and vague generalizations - it doesn't solve the fact that you still need all of the same test cases and they all still need to have unique function names. You haven't actually thought through what you're proposing here.
- sksxihve 2y agoWhen a unit test fails in code I'm working on I don't read the name of the test, I jump to the line in the file for the test and read the code so I never really understood what people find advantageous for this naming convention. I've worked at companies that required this style naming for tests and it was an unholy mess, and it only works if the unit test is small enough that the name is still a reasonable length which at that point the code should be clear enough to understand what is being tested anyway.
- wubrr 2y agoNames, descriptions for tests are useful for many purposes, I'll leave it at that. The point I'm making (and I think you are agreeing with me) is that trying to stuff a test description into a test function name is cumbersome and pointless. There are far better ways of adding descriptions/documentation for unit tests and pretty much every major language/testing framework supports these, nowadays.
- globnomulous 2y agoNot just complete sentences, test names should describe in plain English, with no reference to code or variable names, exactly what's being tested and exactly the expected outcome: "when [something happens], [the result is x]"