Skip to content

Commit 97d2cf3

Browse files
committed
Add section on Testing
1 parent 3a36377 commit 97d2cf3

File tree

1 file changed

+73
-1
lines changed

1 file changed

+73
-1
lines changed

index.adoc

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,76 @@ user=> system
238238
#:tutorial.print{:hello nil}
239239
----
240240

241+
=== Testing
242+
243+
Duct includes a test runner based on
244+
https://github.com/lambdaisland/kaocha[Kaocha]. By default it looks for
245+
test files in the `test` directory.
246+
247+
We can write a unit test for our '`Hello World`' function.
248+
249+
.test/tutorial/print_test.clj
250+
[,clojure]
251+
----
252+
(ns tutorial.print-test
253+
(:require [clojure.test :refer [deftest is]]
254+
[tutorial.print :as tp]))
255+
256+
(deftest unit-test
257+
(is (= "Hello World\n"
258+
(with-out-str (tp/hello {})))))
259+
----
260+
261+
And then run it with the `--test` option.
262+
263+
[,shell]
264+
----
265+
$ duct --test
266+
✓ Loading test environment
267+
[(.)]
268+
1 tests, 1 assertions, 0 failures.
269+
----
270+
271+
Duct also provides a `test` library for running tests on your entire
272+
system.
273+
274+
Add the dependency:
275+
276+
.deps.edn
277+
[,clojure]
278+
----
279+
{:deps {org.clojure/clojure {:mvn/version "1.12.1"}
280+
org.duct-framework/main {:mvn/version "0.2.0"}
281+
org.duct-framework/test {:mvn/version "0.1.0"}
282+
:aliases {:duct {:main-opts ["-m" "duct.main"]}}}
283+
----
284+
285+
This provides a function, `duct.test/run`, which will start the system.
286+
Our '`hello`' component doesn't need cleaning up after itself, but it's
287+
good practice to use the `duct.test/with-system` macro. This will
288+
**halt** the system after the macro's body completes (see the
289+
<<Integrant>> section for more information on halting).
290+
291+
292+
.test/tutorial/print_test.clj
293+
[,clojure]
294+
----
295+
(ns tutorial.print-test
296+
(:require [clojure.test :refer [deftest is]]
297+
[duct.test :as dt]
298+
[tutorial.print :as tp]))
299+
300+
(deftest unit-test
301+
(is (= "Hello World\n"
302+
(with-out-str (tp/hello {})))))
303+
304+
(deftest system-test
305+
(is (= "Hello World\nGoodbye.\n"
306+
(with-out-str
307+
(dt/with-system [_sys (dt/run)]
308+
(println "Goodbye.")))))
309+
----
310+
241311
=== Modules
242312

243313
A *module* groups multiple components together. Duct provides a number
@@ -251,6 +321,7 @@ We'll first add the new dependency:
251321
----
252322
{:deps {org.clojure/clojure {:mvn/version "1.12.1"}
253323
org.duct-framework/main {:mvn/version "0.2.0"}
324+
org.duct-framework/test {:mvn/version "0.1.0"}
254325
org.duct-framework/module.logging {:mvn/version "0.6.5"}}
255326
:aliases {:duct {:main-opts ["-m" "duct.main"]}}}
256327
----
@@ -440,7 +511,8 @@ option keys are supported:
440511
A Duct application has some number of active profiles, which are
441512
represented by unqualified keywords. When run via the `--main` flag, an
442513
implicit `:main` profile is added. When run via `(go)` at the REPL, an
443-
implicit `:repl` profile is added.
514+
implicit `:repl` profile is added. When run via `(duct.test/run)`, an
515+
implicit `:test` profile is added.
444516

445517
You can add additional profiles via the `--profiles` argument. Profiles
446518
are an ordered list, with preceding profiles taking priority.

0 commit comments

Comments
 (0)