This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-01-04
Channels
- # architecture (5)
- # aws (11)
- # aws-lambda (1)
- # beginners (108)
- # boot (11)
- # cider (37)
- # clara (19)
- # cljsrn (72)
- # clojure (170)
- # clojure-austin (2)
- # clojure-dev (1)
- # clojure-dusseldorf (2)
- # clojure-italy (1)
- # clojure-spec (41)
- # clojure-uk (24)
- # clojurescript (113)
- # component (2)
- # core-async (29)
- # cursive (9)
- # data-science (5)
- # datomic (72)
- # docs (23)
- # duct (61)
- # editors (1)
- # emacs (1)
- # events (5)
- # fulcro (77)
- # graphql (2)
- # hoplon (4)
- # jobs (3)
- # jobs-discuss (16)
- # leiningen (5)
- # off-topic (94)
- # onyx (37)
- # precept (5)
- # re-frame (17)
- # reagent (11)
- # shadow-cljs (18)
- # spacemacs (107)
- # specter (3)
- # unrepl (64)
- # yada (1)
I’ve previously written some code to play with the idea of executing tests via the docstring, similar to python’s doctest. I wonder if this is maybe something that would be good idea to try to integrate into @arrdem stacks structure or something. https://gist.github.com/pithyless/c7d6954c055ad45b6dfd2fca6ddfcce9
Interesting idea! You could definitely relax the language lines constraint. What has your experience been using this / transcriptor @pithyless?
@pithyless there's probably a few ways to simplify that. clojure.test checks every var in every ns for a :test
key on its metadata, then runs the fn mapped to by that metadata using fixtures defined in the same ns (if any)
so eg. you don't need a macro that calls deftest, it suffices to create a var with ^{:test (fn [] ....)}
metadata on it
and that var could be the same function you are testing
@noisesmith I thought ^:test
was a boolean flag that the tagged var was a test fn, what are you doing there?
double checking...
right, but if I misremembered it's helpful for me to see what the actual situation is
(defmacro deftest
"Defines a test function with no arguments. Test functions may call
other tests, so tests may be composed. If you compose tests, you
should also define a function named test-ns-hook; run-tests will
call test-ns-hook instead of testing all vars.
Note: Actually, the test body goes in the :test metadata on the var,
and the real function (the value of the var) calls test-var on
itself.
When *load-tests* is false, deftest is ignored."
{:added "1.1"}
[name & body]
(when *load-tests*
`(def ~(vary-meta name assoc :test `(fn [] ~@body))
(fn [] (test-var (var ~name))))))
+user=> (clojure.test/deftest foo-test (clojure.test/is true))
#'user/foo-test
+user=> (meta #'foo-test)
{:test #object[user$fn__147 0x2e61d218 "user$fn__147@2e61d218"], :line 2, :column 1, :file "NO_SOURCE_PATH", :name foo-test, :ns #object[clojure.lang.Namespace 0x20b12f8a "user"]}
so the value under the :test metadata contains the actual function to run
as I said
that means you can easily add an inline test to any function you like, if that suits your fancy
I mean - you can put ^{:test (fn ...)}
in your function definition, as I was suggesting
or a macro that makes a nicer syntax for that of course
just saying there's a more direct route than using deftest is all, especially if part of the root desire here is to define the test in the same code that defines the function