Run one Clojure test (not all tests in a namespace), with fixtures, from the REPL

ぃ、小莉子 提交于 2019-12-05 11:57:27

问题


How do I run one test (not a whole namespace) from the Clojure REPL?

I've tried calling the function directly, e.g. (the-ns/the-test) but I need fixtures to run first. So I want to find a way to start the tests from clojure.test.

This is close but not a match for what I want to do: https://stackoverflow.com/a/24337705/109618

I don't see any mention of how to do it from the clojure.test API.


回答1:


There was a new function added in Clojure 1.6 to support this. clojure.test/test-vars will run one or more test vars with fixtures.

I think something like this should work:

(clojure.test/test-vars [#'the-ns/the-test])



回答2:


If you don't mind not running fixtures, you could do the following before you call run-tests:

(defn test-ns-hook []
  (my-test))

To remove the hook, you can do

(ns-unmap *ns* 'test-ns-hook)

If you still need your fixtures and want to stay with one test namespace, you could add an ns-unmap to remove all the tests/fixtures you don't want to run from the namespace before running your tests modelled on something like:

(doseq [v (keys (ns-publics 'my-ns))]
  (let [vs (str v)]
    (if (.startsWith vs "test-") (ns-unmap 'my-ns v))))

It might be easier to work with multiple namespaces, one of which contains all your tests and fixtures and in other namespace(s) refer to the tests and fixtures you want to run from you main test namespace. You could then use ns to switch to a specific test namespace or pass run-tests the namespace(s) you want to test:

(ns test-main
  (:require [clojure.test :refer :all]))

(deftest addition
  (is (= 4 (+ 2 2)))
  (is (= 7 (+ 3 4))))

(deftest subtraction
  (is (= 1 (- 4 3)))
  (is (= 3 (- 7 4))))

(run-tests)
;Runs all the tests

(ns test-specific
(:require [clojure.test :refer :all]
          [test-main :refer :all]))

(deftest arithmetic
  (subtraction))

(run-tests)
;Just runs the tests you want



回答3:


To run a single test in a namespace:

lein test :only namespace_name/testname

To run a all tests in one namespace

lein test :only namespace_name



回答4:


A nice alternative to clojure.test (if you're OK adding additional dependencies is eftest). This library is an alternative runner (it is compatible with clojure.test). It has flexible tests selectors (i.e. by folder, by namespace, by var).

In a REPL, you're able to execute something like this:

(require '[eftest.runner :refer [find-tests run-tests]]) (run-tests (find-tests #'foo.bar/baz))

See https://github.com/weavejester/eftest



来源:https://stackoverflow.com/questions/24970853/run-one-clojure-test-not-all-tests-in-a-namespace-with-fixtures-from-the-rep

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!