簡體   English   中英

從Clojure Repl和Leiningen運行測試

[英]Run tests from Clojure Repl and Leiningen

作為clojure的新手,我使用leiningen創建了一個示例項目

lein new app first-project

這給了我這個目錄

.
├── 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

無需修改任何文件,我就可以成功完成唯一失敗的測試

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

但是我無法使用運行測試在REPL中執行相同操作

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}

我嘗試過(但不起作用)

(require 'first-project.core-test)

lein repl啟動一個REPL,然后使用require

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

我更喜歡保留在user名稱空間中,而不是像另一個答案提到的那樣使用in-ns進行更改。 而是將名稱空間作為參數傳遞給run-tests (如上所示)。

我也建議遠離(use 'clojure.test) 這就是為什么我在上面建議(require '[clojure.test :refer [run-tests]]) 有關更多背景信息,請閱讀http://dev.clojure.org/jira/browse/CLJ-879

在上面的示例中,REPL位於錯誤的名稱空間中。 如果將repl切換到core_test命名空間,則可能會更好。 然后運行(run-tests)

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

開發測試的另一種有趣方式是僅在REPL上運行它們,直到它們起作用為止,因為測試是具有一些額外元數據的正常功能。

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

記住,除了調用in-ns外,您還必須加載文件。假設您的測試文件是tests/first_project/core_test.clj ,那么您將需要調用

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

請記住,文件系統中的_在命名空間中變為- ,並且/變為.

回顧一下:

require

僅當您之前發出in-ns才需要完全限定功能。 然后做:

(clojure.core/require '[clojure.core :refer [require]]
                      '[clojure.test :refer [run-tests]]
                      '[clojure.repl :refer [dir]])

; Load whatever is in namespace "foo.bar-test" and reload everything
; if `:reload-all` has been additionally given

(require 'foo.bar-test :reload-all) 
;=> nil

; List your tests for good measure (Note: don't quote the namespace symbol!)

(dir foo.bar-test)
;=> t-math
;=> t-arith
;=> t-exponential
;=> nil 

; Check meta-information on a test to verify selector for example 

(meta #'foo.bar-test/t-math)
;=> {:basic-math true, :test #object[foo.bar_tes...

; `run-tests` will probably run nothing because the current namespace
; doesn't contain any tests, unless you have changed it with "in-ns"

(run-tests) 
;=> Ran 0 tests containing 0 assertions.

; run tests by giving namespace explicitly instead

(run-tests 'foo.bar-test) 
;=> Ran 3 tests containing 29 assertions.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM