如何在clojure中编写测试用例

时间:2014-03-12 08:15:54

标签: clojure

我是clojure的初学者 我写了

(expect true (valid? "henry" "as@gmail.com" "9999999999" "tesxt message")) 

(ns clojuregeek.test.contact)

当我在clojure中通过lein测试运行测试用例时,我发现: -

lein test clojuregeek.test.contact

Ran 0 tests containing 0 assertions.
0 failures, 0 errors.

failure in (contact.clj:32) : clojuregeek.test.contact
(expect
 true
 (valid? "henry" "as@gmail.com" "9999999999" "tesxt message"))

  act-msg: exception in actual: (valid? "henry" "as@gmail.com" "9999999999" "tesxt message")
    threw: class java.lang.ClassCastException - clojure.lang.Var$Unbound cannot be cast to java.util.concurrent.Future
           noir.validation$get_errors$doInvoke (validation.clj:94)
           noir.validation$errors_QMARK_$doInvoke (validation.clj:140)
           on (contact.clj:34)
           on (contact.clj:32)

Ran 1 tests containing 1 assertions in 178 msecs
0 failures, 1 errors.

1 个答案:

答案 0 :(得分:2)

在Clojure中开发petri网模拟器时,测试环境 Midje 是编写有用且简单的测试用例的最佳选择。也许你只是看看这个......

测试用例非常简单。您只需在test/core_tests.clj中创建事实,例如:

(ns your-namespace.core-tests
  (:use midje.sweet)
   :require [your-namespace.core :as core])

(fact "Testing valid?"
  (core/valid? "henry" "as@gmail.com" "9999999999" "tesxt message") => true)

;; some more facts...

如需准备,您需要修改project.clj

(defproject your-project "0.1.0-SNAPSHOT"
  :description ""
  :url "http://example.com/FIXME";TODO
  :license {:name "Eclipse Public License"
            :url "http://www.eclipse.org/legal/epl-v10.html"}
  :dependencies [[org.clojure/clojure "1.5.1"]
                 [midje "1.6.2"]]
  :main ^:skip-aot your-project.core
  :target-path "target/%s"
  :profiles {:uberjar {:aot :all}
             :dev {:dependencies [[midje "1.6.2"]]
                   :plugins [[lein-midje "3.1.3"]]}})

在此之后,您可以启动一个新的终端窗口并首先输入项目文件夹lein deps,然后输入:

lein midje :autotest
每次将文件保存到项目文件夹中时,

和Midje都将运行测试。

在我看来,编写简单实用的测试用例的最佳解决方案之一。