18
votes

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?

1
As long as everybody working on the project follows the same convention, and it's easy to find the test ns for a given production ns, I'd say it's a matter of personal preference. This isn't Java where the packages need to line up for protected/default access. - Alex

1 Answers

18
votes

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, e.g. 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/....