You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As you might imagine, you can run more than one test using `test-vars`. You can also run all the tests in the current namespace:
95
+
As you might imagine, you can run more than one test using `test-vars`. You can also run all the tests in the current namespace, which produces more informative output:
92
96
93
97
```clojure
94
98
user=> (clojure.test/run-tests)
@@ -126,6 +130,74 @@ The following is usually sufficient to run tests via Boot, assuming your `build.
126
130
> boot test
127
131
```
128
132
133
+
### Test Placement
134
+
135
+
While not directly related to how to run your tests, it's a common question asked by folks new to Clojure: where should I put my tests?
136
+
137
+
#### Standard Conventions
138
+
139
+
Most of the `clojure.test`-based tooling assumes that for each source file `src/path/to/my_code.clj` (which represents the namespace `path.to.my-code`), you will have a test file `test/path/to/my_code_test.clj` with the namespace `path.to.my-code-test`.
Following his convention means that all the tooling and IDE/editor integrations should work with no configuration: it's what everyone "expects".
150
+
151
+
#### Tests with Source Code
152
+
153
+
`clojure.test` has a macro called `with-test` that allows you to define tests inline following your function definition. Given that `clojure.test` ships directly with Clojure, this is reasonable because putting test code in your function definition's metadata doesn't add any dependencies and it has the benefit of being able to see the source of the function and the source of its test right next to each other. You can do that with Expectations too, since it is `clojure.test`-compatible, although it does mean your source code has an additional dependency -- but Expectations is fairly small (~300 lines) and has no additional dependencies.
154
+
155
+
However, if you put tests in your source files, using `with-test`, then most tooling won't know how to find those tests by default. Here's an example of an inline test and how to run it with Leiningen and the CLI (`deps.edn`):
156
+
157
+
```clojure
158
+
(nsmy.cool.project
159
+
(:require [clojure.test :refer [with-test]]
160
+
[expectations.clojure.test :refer [expect]]))
161
+
162
+
(with-test
163
+
(defnsquare [x] (* x x))
164
+
(expect1 (square1))
165
+
(expect1 (square-1))
166
+
(expect100 (square10)))
167
+
```
168
+
169
+
For Leiningen, you'll need to tell it to look for tests in `src` (as well as `test`) so add this to `project.clj`:
170
+
171
+
```clojure
172
+
:test-paths ["src""test"]
173
+
```
174
+
175
+
then you can just run `lein test` and it will check for tests inside the `src` test, find `my.cool.project/square` test metadata and run it as a test.
176
+
177
+
For the `clojure` CLI, you'll need to tell Cognitect's `test-runner` to look for tests in `src`_and_ you'll have to override it's default regex pattern for matching test namespaces:
178
+
179
+
```bash
180
+
clojure -A:test -d src -r ".*"
181
+
```
182
+
183
+
Of course, you can also update the `:test` alias to add those new options into `:main-opts` so that you don't need them on the command line:
0 commit comments