Welcome! Please see the About page for a little more info on how this works.

+1 vote
in Test by
closed by
When developing code, it is sometimes effective to focus on a single failing test, rather than running all tests in a namespace. This can be the case when running the tests takes some amount of time, or when running the tests produces a large volume of failures. The best option for running a single test with fixtures currently is `test-vars` ala:


(use 'clojure.test)
(def counter (atom 0))
(defn setup [f] (swap! counter inc) (f)) ;; a :once fixture with state
(use-fixtures :once setup)
(deftest ex (println "counter =" @counter))

(test-vars [#'ex])  ;=> counter = 1
(test-vars [#'ex])  ;=> counter = 2


However, this has the following issues:
- No test reporting feedback such as you get with run-tests (on success, there is no output)
- Need to specify var (not symbols) wrapped in a vector

*Proposed:* A new macro `run-test` that specifies a single symbol and does the same test reporting you get with `run-tests`. Usage:


(use 'clojure.test)
(def counter (atom 0))
(defn setup [f] (swap! counter inc) (f)) ;; a :once fixture with state
(use-fixtures :once setup)
(deftest ex (println "counter =" @counter))

(run-test ex)

;=> Testing user
;=> counter = 1

;=> Ran 1 tests containing 0 assertions.
;=> 0 failures, 0 errors.
;=> {:test 1, :pass 0, :fail 0, :error 0, :type :summary}

(run-test ex)

;=> Testing user
;=> counter = 2

;=> Ran 1 tests containing 0 assertions.
;=> 0 failures, 0 errors.
;=> {:test 1, :pass 0, :fail 0, :error 0, :type :summary}



*Patch:* CLJ-1908-3.patch

*Screened:* Alex Miller
closed with the note: Fixed in 1.11.0-alpha2

9 Answers

+1 vote
by
_Comment made by: alexmiller_

As far as I can tell, this is basically the same intent as CLJ-866 which was completed in Clojure 1.6. You can do this now with {{test-vars}}:


user=> (use 'clojure.test)
nil
user=> (def counter (atom 0))
#'user/counter
user=> (defn setup [f] (swap! counter inc) (f)) ;; a :once fixture with state
#'user/setup user=> (use-fixtures :once setup) {:clojure.test/once-fixtures (#object[user$setup 0x7106e68e "user$setup@7106e68e"])} user=> (deftest ex (println "counter =" @counter)) #'user/ex user=> (test-vars [#'ex])
counter = 1
nil
user=> (test-vars [#'ex])
counter = 2
nil
0 votes
by

Comment made by: hlewisship

Having trouble with the patch, in that, things that work at the REPL fail when executed via mvn test. Tracking down why is taking some time.

0 votes
by

Comment made by: hlewisship

Initial patch; code works but mvn test fails and I haven't figured out why.

0 votes
by

Comment made by: hlewisship

Thanks to Hiredman, was provided with insight that back ticks needed due to how Maven/Ant runs the tests. All tests now pass.

0 votes
by

Comment made by: hlewisship

I think there is some advantage to being able to run the tests using is symbol, not its var. Further, the change I've suggested also returns the same kind of data that run-tests does.

0 votes
by

Comment made by: alexmiller

Some changes needed on this patch before I will prescreen it:

  • Patch should be squashed to a single commit
  • Commit message in patch should start with "CLJ-1908"
  • Change run-test* to run-test-var
  • The docstring for run-test-var should be: "Run the test in Var v with fixtures and report." Kill the "called from" sentence".
  • The first sentence of the docstring for run-test should be: "Runs a single test in the current namespace." Remove "This is meant to be invoked interactively, from a REPL.". Last sentence is ok.
  • In run-test, replace * with the simpler {{(resolve test-symbol)}}.
0 votes
by

Comment made by: hlewisship

Thanks for the input; I'll have an updated patch shortly.

0 votes
by

Comment made by: hlewisship

Updated patch, squashed and reflecting all of Alex's comments.

0 votes
by
Reference: https://clojure.atlassian.net/browse/CLJ-1908 (reported by hlewisship)
...