Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
334 views
in Technique[技术] by (71.8m points)

Run tests from Clojure Repl and Leiningen

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)
question from:https://stackoverflow.com/questions/21294294/run-tests-from-clojure-repl-and-leiningen

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In your example above the repl is in the wrong namespace. It may work better if you switch the repl to the core_test namespace. and then run (run-tests).

(in-ns 'first-project.core-test)
(run-tests)

Another fun way of developing tests is to just run them from the REPL until they work, because tests are normal functions with some extra metadata.

(in-ns 'first-project.core-test)
(my-test)

Remember you have to load the file in addition to calling in-ns Let's say your test file is tests/first_project/core_test.clj, then you will need to call

(load "tests/first_project/core_test")
(in-ns 'first-project.core-test)
(my-test)

Keep in mind that _ in the file system becomes - in the namespace and / becomes ..


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...