Variable Name Conventions in Tests
Found a question about how to “best” write the tests for the Fizz Buzz kata. The author provides his approach and questions as a GitHub Gist. One interesting takeaway is his use of the name of his constants, e.g.
anyNumberDivisibleBy3 = 3. It expresses very well what kind of placeholder this number is supposed to be.
I added this to my arsenal of naming conventions when writing tests.
The last thing I discovered in 2014 coming from J. B. Rainsberger’s What your tests don’t need to know will hurt you that stuck: the “irrelevant” prefix, as in:
let irrelevantDate: Date = Date(timeSinceReferenceDate: 123)
This prefix tells me which part of a function call or object setup I should gloss over, and which are the important variables for a test case. Love it, 10/10, ★★★★★, can recommend.
J. B. Rainsberger also commented on the Gist and suggests to change test case names to be less technical, and convey more of the intent:
should return FizzBuzz when it would otherwise want to return both Fizz and Buzz tells the reader that the algorithm should concatenate the result, and this is a bit easier to grok than
should return FizzBuzz when the number is divisible by 3 and 5, which sounds just like another arbitrary rule. The latter still fine, but it lacks the beauty and brevity of the former. You don’t have to understand what “3 and 5” means, you don’t have to have the context of the other tests. It’s self-sufficient.
Do you have naming conventions like
irrelevantY you can recommend? Please share in the comments or on Twitter (@ctietze).
Receive new posts via email.