Skip to content

Commit d922094

Browse files
committed
Fixes #10 by documenting with-test as an option
1 parent 70f12a7 commit d922094

File tree

2 files changed

+74
-2
lines changed

2 files changed

+74
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
* Add support for mocking return values in `side-effects`.
44
* Add support for optional message argument in `expect`. #9
5-
* Added article-style documentation for cljdoc.org. #6, #7, #8
5+
* Added article-style documentation for cljdoc.org. #6, #7, #8, #10
66
* Add example of `more->` equivalent to `thrown-with-msg?`. #5
77

88
# Version 1.1.2 -- 2019-12-07

doc/getting-started.md

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@ This library provides an expressive alternative to [`clojure.test`](http://cloju
77
You can add `expectations/clojure-test` to your project with either:
88

99
```clojure
10+
;; add this to :extra-deps under a :test alias:
1011
{expectations/clojure-test {:mvn/version "1.1.2"}}
1112
```
1213
for `deps.edn` or:
1314

1415
```clojure
16+
;; add this to :dev-dependencies (Leiningen)
1517
[expectations/clojure-test "1.1.2"]
18+
;; or add this to :dependencies (Boot)
19+
[expectations/clojure-test "1.1.2" :scope "test"]
1620
```
1721
for `project.clj` or `build.boot`.
1822

@@ -88,7 +92,7 @@ user=> (clojure.test/test-vars [#'simple-test])
8892
nil
8993
```
9094

91-
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:
9296

9397
```clojure
9498
user=> (clojure.test/run-tests)
@@ -126,6 +130,74 @@ The following is usually sufficient to run tests via Boot, assuming your `build.
126130
> boot test
127131
```
128132

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`.
140+
141+
That test file will generally start out with:
142+
143+
```clojure
144+
(ns path.to.my-code-test
145+
(:require [expectations.clojure.test :refer [defexpect expect expecting ,,,]
146+
[path.to.my-code :refer [the-functions you-want to-test]]]))
147+
```
148+
149+
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+
(ns my.cool.project
159+
(:require [clojure.test :refer [with-test]]
160+
[expectations.clojure.test :refer [expect]]))
161+
162+
(with-test
163+
(defn square [x] (* x x))
164+
(expect 1 (square 1))
165+
(expect 1 (square -1))
166+
(expect 100 (square 10)))
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:
184+
185+
```clojure
186+
{:aliases
187+
{:test
188+
{:extra-paths ["test"]
189+
:extra-deps
190+
{expectations/clojure-test {:mvn/version "1.1.2"}
191+
com.cognitect/test-runner
192+
{:git/url "https://github.com/cognitect-labs/test-runner.git"
193+
;; as at the time of writing -- check the test-runner repo for the latest:
194+
:sha "209b64504cb3bd3b99ecfec7937b358a879f55c1"}}
195+
:main-opts ["-m" "cognitect.test-runner"
196+
"-d" "src" "-d" "test" "-r" ".*"]}}}
197+
```
198+
199+
Note that you'll need both `src` _and_ `test` directories if you want `test-runner` to look in both places.
200+
129201
## Expecting Specs
130202

131203
If you are using Clojure 1.9 or later, you have access to Spec and `expect` those as well:

0 commit comments

Comments
 (0)