简体   繁体   中英

Clojure test namespace convention

I have traditionally used the same folder structure for production and test code as demonstrated below:

src/myproject/core.clj
test/myproject/core_test.clj

For test files I have added _test in the filename.

I recently noticed that several projects follow this structure (this is also what Leiningen generates by default):

src/myproject/core.clj
test/myproject/test/core.clj

Is there a convention regarding this or some clear advantage of using one over the other?

I believe this is just convention - I don't think there is any technical advantage either way.

I personally prefer the first version for entirely non-technical reasons:

  • It seems redundant to have two "test" directories in the path.
  • It can cause confusion to have the test .clj files with the same names as the main .clj files
  • Sometimes you want to create tests that don't perfectly line up with specific namespaces, eg full_system_test.clj for end-to-end testing
  • It's easier to pattern-match on all the *_test.clj files

Also worth noting that the Maven standard directory layout convention is also used in quite a few Clojure projects (this can be handy if you build polyglot projects that also contain Java source code):

src/main/clojure/myproject/core.clj
src/test/clojure/myproject/core_test.clj
src/main/resources/....
src/test/resources/....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM