|
| 1 | +# Testing Your Workflows |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +This Temporal Clojure SDK provides a test framework to facilitate Workflow unit-testing. The test framework includes an in-memory implementation of the Temporal service. |
| 6 | + |
| 7 | +You can use the provided environment with a Clojure unit testing framework of your choice, such as `clojure.test`. |
| 8 | + |
| 9 | +## Example |
| 10 | + |
| 11 | +```clojure |
| 12 | +(ns my.tests |
| 13 | + (:require [clojure.test :refer :all] |
| 14 | + [taoensso.timbre :as log] |
| 15 | + [temporal.client.core :as c] |
| 16 | + [temporal.testing.env :as e] |
| 17 | + [temporal.workflow :refer [defworkflow]] |
| 18 | + [temporal.activity :refer [defactivity] :as a])) |
| 19 | + |
| 20 | +(def task-queue "MyTaskQueue") |
| 21 | + |
| 22 | +(defactivity greet-activity |
| 23 | + [ctx {:keys [name] :as args}] |
| 24 | + (log/info "greet-activity:" args) |
| 25 | + (str "Hi, " name)) |
| 26 | + |
| 27 | +(defworkflow greeter-workflow |
| 28 | + [ctx {:keys [args]}] |
| 29 | + (log/info "greeter-workflow:" args) |
| 30 | + @(a/invoke greet-activity args)) |
| 31 | + |
| 32 | +(deftest my-test |
| 33 | + (testing "Verifies that we can invoke our greeter workflow" |
| 34 | + (let [{:keys [client] :as env} (e/create {:queue-name task-queue}) |
| 35 | + (c/create-workflow client greeter-workflow {:task-queue task-queue})] |
| 36 | + (c/start workflow {:name "Bob"}) |
| 37 | + (is (= @(c/get-result workflow) "Hi, Bob"))))) |
| 38 | +``` |
| 39 | + |
| 40 | +The primary difference between this test and a real-world application is the use of ([temporal.testing.env/create](https://cljdoc.org/d/io.github.manetu/temporal-sdk/CURRENT/api/temporal.testing.env#create)) function. This method simultaneously creates an instance of the in-memory Temporal service *and* a client already connected to this environment. |
| 41 | + |
| 42 | +Typical tests may opt to create the testing environment within a [fixture](https://clojuredocs.org/clojure.test/use-fixtures), but this is left as an exercise to the reader. The testing environment may be cleanly shutdown with ([stop](https://cljdoc.org/d/io.github.manetu/temporal-sdk/CURRENT/api/temporal.testing.env#stop)) |
0 commit comments