Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test naming inconsist with function naming. #255

Open
Tillaert opened this issue Sep 20, 2023 · 4 comments
Open

Test naming inconsist with function naming. #255

Tillaert opened this issue Sep 20, 2023 · 4 comments

Comments

@Tillaert
Copy link

The guide advises to name tests:

(deftest foo-test ...)

But functions as

(defn foo ...)

Shouldn't it be:

(defn foo-function ...)

to be consistent?

However, there is no need postfix testnames with -test, since the tests are marked in their meta-data, and the test runner can locate them this way. Clojure does not locate tests by name, such as is the case in Python and Javascript, for example, where this is required.

@seancorfield
Copy link
Collaborator

The function is foo, the test for foo is foo-test. That's pretty consistent.

Whilst it is true there is no need to follow any specific naming code for tests, encouraging folks to write a dedicated test for each function f, named f-test, encourages single-purpose tests: each test will exercise a specific, related function. This also allows for the source ns to be referred in (via :refer :all) without test names conflicting with the functions being tested -- although my personal preference is to :require the source ns :as sut for system-under-test.

Note that some test runners assume that tests are in namespaces with a -test suffix (i.e., in <the.ns>_test.clj), so there's an adjacent precedent for -test suffices also carrying across to the test functions themselves.

@Tillaert
Copy link
Author

I think you are confusing tradition with consistency. Consistency is: All things are post-fixed with the thing they are. Tradition is: some things are post-fixed others are not, because we always have done it that way, and we still do this even when there is no reason to do this.

Furthermore, the test foo might not test a function foo. It might be (deftest make-sure-all-dishes-are-in-loaded-in-the-dishwasher ...), which tests a specific user scenario. Postfixing test here creates the situation where you repeat the word test. This is undesired from a human language point of view. (The same reason we do not always put the word -function afther a function.)

With testing and is we have multiple tests in a single deftest, so to describe what it is, you should postfix the name of a deftest with -tests.

It is true that in the case where the test file does a :refer :all, this allows to distinguish between the function and the test, but nothing is preventing you in that case to add -test. I agree that :as sut is better, since it is short and very distinctive.

The point I want to make is that it should not be 'mandatory' to always postfix test functions with -test, as there is no technical reason to do this. If the name is decriptive and unique, there should be no reason to add anything.

(I am not talking about namespace naming, as you correctly pointed out, there is a technical reason.)

@danielcompton
Copy link
Collaborator

However, there is no need postfix testnames with -test, since the tests are marked in their meta-data, and the test runner can locate them this way. Clojure does not locate tests by name, such as is the case in Python and Javascript, for example, where this is required.

Tests should have a different name from the function that they are testing, if only for the reason that deftest defines a function with the name of the test. You can wind up in very strange situations when you call the test instead of the function accidentally. Having a style guide rule that all tests are suffixed with -test helps avoid this, and also gives a pleasing symmetry between functions and their tests.

The point I want to make is that it should not be 'mandatory' to always postfix test functions with -test, as there is no technical reason to do this. If the name is decriptive and unique, there should be no reason to add anything.

These guidelines are part of the Clojure style guide, which documents generally accepted practices and conventions in the Clojure community. As far as I can tell, the currently documented policies match the community consensus. If you don't like them, you don't have to use them though.

See the note about consistency which acknowledges that you should still use your best judgement. If your best judgement is to not use -test, then feel free to do that. I don't think that it would be a great offence to name a test without a -test suffix if the test had a very long and descriptive name.

@Tillaert
Copy link
Author

I agree with you, however, often best judgement is cudgeled by a 'the styleguide says'.

Of course, we should give different things different names, to avoid name clashes, but that is not limited to tests. Scoping issues also happen in a let, and we do not have a mandatory postfix there.

Currently the styleguide says:

;; good
(deftest something-test ...)

;; bad
(deftest something-tests ...)
(deftest test-something ...)
(deftest something ...)

So why is there this hard distinction for tests, but not for other items that can cause scoping issues?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants