Function naming for test implementations came up recently in the Clojure thread and in a stale GitHub issue. For those who haven’t been following, I’ll summarize the key points here. I look forward to hearing the opinions of other maintainers. Apologies for the length of the post. All views are very appreciated.
So, here’s how a typical function looks like:
(deftest does-not-detect-non-anagrams-with-identical-checksum
(testing "Does not detect non-anagrams with identical checksum"
(is (= [] (anagram/anagrams-for "mass" ["last"])))))
The part next to deftest
on the first line is the function name, and the “testing” line is optional.
Traditionally, on this track, function names are derived from the test description (shown as a string on the next line) by converting all text to lowercase and using a hyphen to separate the words. Here is how this test appears on the website:
Notice how, right below “Test 7”, the function name is displayed. I’ve seen other tracks (JS, iirc) using the test description in that position, which is something I’d like to see implemented here as well.
Unfortunately, I’m not in a position to make that change right now. I haven’t looked into the test runner, and I don’t believe I have either the expertise or the time to modify it.
In the other Clojure thread and on github, I argued that displaying the function name in this way doesn’t serve much of a purpose:
- It wastes time by forcing people to parse a function name that often doesn’t make much sense.
- It can become very long, particularly in nested descriptions.
- It is difficult to maintain manually (yes, we still write the tests manually). Often, tests are copy-pasted, leading to duplicated function names because similar descriptions are used. This is a significant issue because two identical function names cause the second function to override the first, resulting in the first test being skipped without any error notification.
To address this, and given the lack of consensus on function naming conventions, I decided to adopt a test-<uuid>
pattern for naming. With this change, the first line of the above test becomes:
(deftest test-1d0ab8aa-362f-49b7-9902-3d0c668d557b
...)
Obviously, this approach isn’t perfect either. The function name still appears on the website, and it doesn’t make sense. However, my reasoning is that people will also quickly learn to ignore that part and focus on expanding the test to view the “code run”.
Additionally, if we reach a point where every test includes a “testing” section that describes the test in plain English, we could eventually replace the displayed function name on the website with the proper test description. This would also allow us to simplify the “code run” by omitting the “testing” line entirely.
For these reasons, I have concluded that the function name doesn’t matter all that much. As long as the names are unique, I’m personally fine with it.
That said, I’m currently the only active maintainer, and I’d like to know how others would handle this situation. What would you consider an appropriate name for the function? Do you think deriving function names from descriptions is good or bad practice? Do you think the test-<uuid>
pattern is a good approach?
Please share your thoughts.