Run tests from Clojure Repl and Leiningen

anotherCode245 picture anotherCode245 · Jan 22, 2014 · Viewed 17.1k times · Source

As a newbie to clojure, I have used leiningen to create a sample project with

lein new app first-project

which gave me this directory

.
├── doc
│   └── intro.md
├── LICENSE
├── project.clj
├── README.md
├── resources
├── src
│   └── first_project
│       └── core.clj
├── target
│   └── repl
│       ├── classes
│       └── stale
│           └── extract-native.dependencies
└── test
    └── first_project
        └── core_test.clj

Without modifying any files, I can lauch successfully the only failing test with

lein test
...
Ran 1 tests containing 1 assertions.
1 failures, 0 errors.
Tests failed.

But I am unable to do the same from the REPL using run-tests

lein repl
first-project.core=> (use 'clojure.test)
nil
first-project.core=> (run-tests)

Testing first-project.core

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

I tried (but does not work)

(require 'first-project.core-test)

Answer

David J. picture David J. · Jun 21, 2014

Start a REPL with lein repl, then use require

(require '[clojure.test :refer [run-tests]])
(require 'your-ns.example-test :reload-all)
(run-tests 'your-ns.example-test)

I prefer to stay in the user namespace, as opposed to changing it with in-ns as mentioned by another answer. Instead, pass the namespace as an argument to run-tests (as shown above).

I'd also recommend staying away from (use 'clojure.test); that is why I suggested (require '[clojure.test :refer [run-tests]]) above. For more background, read http://dev.clojure.org/jira/browse/CLJ-879