Tuesday 15 April 2014

unit testing - How to test for thrown exceptions in Clojure? -



unit testing - How to test for thrown exceptions in Clojure? -

when testing code cover failing conditions, too.

i'm new clojure, somehow possible test thrown exception?

i'd test next sample code:

(ns com.stackoverflow.clojure.tests) (defn casetest [x] (case x "a" "an a." "b" "a b." (-> (clojure.core/format "expression '%s' not defined." x) (illegalargumentexception.) (throw))))

with next test-methods:

(ns com.stackoverflow.clojure.tests-test (:require [clojure.test :refer :all] [com.stackoverflow.clojure.tests :refer :all])) (deftest casetest-tests (is (= "an a." (casetest "a"))) (is (= "a b." (casetest "b"))) (throws-excpeption illegalargumentexception. (casetest "c")) ; conceptual code ) (run-tests)

my project.clj looks like:

(defproject com.stackoverflow.clojure/tests "0.1.0-snapshot" :description "tests of clojure test-framework." :url "http://example.com/fixme" :license {:name "eclipse public license" :url "http://www.eclipse.org/legal/epl-v10.html"} :dependencies [[org.clojure/clojure "1.6.0"]])

you can test thrown exception (is (thrown? expectedexceptionclass body))

see http://clojure.github.io/clojure/clojure.test-api.html

unit-testing exception testing exception-handling clojure

No comments:

Post a Comment