你好Clojure专家..!,
更新:2018年8月3日CDT 15:00-改正问题以使其更具描述性。
更新:CDT 2018年8月3日下午4:10-添加了负面的场景测试以更加合理。
我有一个功能/被测物(jvm语言),我想使用Clojure生成性测试API(目前)测试肯定的情况。
正面场景测试:假设jvm语言是五个{“ clojure”“ haskell”“ erlang”“ scala”“ python”}之一,则结果应该为true。
我的问题/要求:对于每次执行(每次运行),我都应该使用每种可能的有效方案(在下面的列表中列出)测试我的被测函数,莱因测试),这样我就可以摆脱常规单元测试。
根据我的一点测试知识,我认为在同一执行中使用相同的测试场景多次测试我的函数没有价值。如果您认为我的思考过程有误,请教育我。
如果没有,这不是一个很棒的功能吗?
(ns spec.gen-test.explore
(:require [clojure.test :refer :all]
[clojure.spec :as spec]
[clojure.test.check.generators :as gen]
[clojure.test.check.properties :as prop :include-macros true]
[clojure.test.check.clojure-test :refer [defspec]]))
(def languages-set #{"clojure" "haskell" "erlang" "scala" "python"})
(spec/def ::jvm-languages
(spec/and string?
languages-set))
;;Generative test case for jvm-languages spec:
(defspec jvm-languages-positive-generative-test
5
(prop/for-all [language (gen/elements ["clojure" "haskell" "erlang"
"scala" "python"])]
(println "Testing for language:" language)
(spec/valid? ::jvm-languages language)))
(defspec jvm-languages-negative-generative-test
100
(prop/for-all [language (gen/such-that
#(not (contains? languages-set %))
(gen/one-of
[ gen/string
(gen/elements ["" nil])
gen/int
gen/keyword
gen/char-alphanumeric
gen/double ]))]
(println "Testing for language:" language)
(not (spec/valid? ::jvm-languages language))))
(run-tests)
Output:
#'spec.gen-test.explore/jvm-languages-positive-generative-test
Testing for language: scala
Testing for language: haskell
Testing for language: erlang
Testing for language: erlang
Testing for language: scala
{:result true, :num-tests 5, :seed 1533232724897, :test-var "jvm-languages-generative-test"}
答案 0 :(得分:0)
由于您仍然想测试每种情况,所以我只是对集合进行迭代。
您可以使用is
和every?
获得一些相当不错的阅读代码:
(ns your-ns
(:require [clojure.test :refer [is testing]]))
(let [test-cases ["clojure" "haskell" "erlang" "scala" "python"]]
(testing "if my things are working."
; Just iterating the test cases using every?
(is (every? #(your-test-predicate-here %) test-cases))))