@@ -238,6 +238,76 @@ user=> system
238
238
#:tutorial.print{:hello nil}
239
239
----
240
240
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
+
241
311
=== Modules
242
312
243
313
A *module* groups multiple components together. Duct provides a number
@@ -251,6 +321,7 @@ We'll first add the new dependency:
251
321
----
252
322
{:deps {org.clojure/clojure {:mvn/version "1.12.1"}
253
323
org.duct-framework/main {:mvn/version "0.2.0"}
324
+ org.duct-framework/test {:mvn/version "0.1.0"}
254
325
org.duct-framework/module.logging {:mvn/version "0.6.5"}}
255
326
:aliases {:duct {:main-opts ["-m" "duct.main"]}}}
256
327
----
@@ -440,7 +511,8 @@ option keys are supported:
440
511
A Duct application has some number of active profiles, which are
441
512
represented by unqualified keywords. When run via the `--main` flag, an
442
513
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.
444
516
445
517
You can add additional profiles via the `--profiles` argument. Profiles
446
518
are an ordered list, with preceding profiles taking priority.
0 commit comments