????????????д????????
????“????????”??????????????????????????????????????????????????????????????????д?????????????????? ???????磺TDD??????????????? ???????????“???”???????????????????????????????????£????????????????е????????????
???????????????????????????????????????????????
??????????????????By Example??
??????????????????·??Given??When??Then????????
????· Given????????????????
????· When?????????
????· Then????????
??????д???????????????Given????????????????????????????????????????????????????????????????????????????TDD?????????????á??????????????????????????????????????????????С?????????
???????????????????л?????
????· ????????????????????????????壬????????????????????????? ????????????????????д??????kata?????????????е?????????????“return 3 when add 1 and 2”??????????????????????????????????????——?????????????????????add??????????????????????????????????????????????????????????????????????????????????????????
????· ????????????????????????????????????????????дhappy path????? ????????????£????????????????????·?????в??????????????????
?????????????????????????????????????????????——??????????Generative Testing?????Property-Based Testing?????????????????????????????????????????????????????????????????
?????????????
????????????????????????·????????????????д????????????????????????????????????????ж???????????????????????Given??When??Then????????ó????????????β????????????????“?????????”?????——????????????????????????????????????????????????????????????????????????????????
????Given???
????Clojure 1.9??Alpha?????????Clojure.spec?????????????????
????;; ????????????????????????????????
????(s/def ::add-operators (s/cat :a int? :b int?))
????;; ????????????
????(gen/generate (s/gen ::add-operators))
????;; ?????????
????-> (1 -122)
????????????????????????????????????????????????specification??????????a??b?????????????????????????????????????????????????????У???????漰????????????????????????????????????????????????????????????????——????????????return 3 when add 1 and 2????????????return the sum of two integers???????????塣
????Then???
????????????????????????????????????Then??????????????????????????????????????????????????????????????????????
??????????????????????
????(defn add [a b]
????(+ a b))
??????????????????????????? ???????????a??b??a??b??????????????a??b???? ????test.check????????Clojure???????????
????(def test-add
????(prop/for-all [a (gen/int)
????b (gen/int)]
????(= (add a b) (+ a b))))
???????????????add?????????(+ a b)д??????????????????????????????塣??????????????????ü??????????????????? ???????????????a??b?????????????a?????b??
????(def test-add
????(prop/for-all [a (gen/int)
????b (gen/int)]
????(= (- (add a b) a) b))))
???????????????????????????????????任??????????quick-check?????????????????в????
????;; ???????100?????????add????
????(tc/quick-check 100 test-add)
????;; ??????
????-> {:result true?? :num-tests 100?? :seed 1477285296502}
?????????????????????????100?????????????????????????????????????????????????????add?????????????????????????????????????????????????????????????????????????
?????????????????????????????????????????????????????????y??????????????Ч????????磺????????????ж??????????pairwise???????????????ù?????У??????????????????????????????????????????????????????????????identity-nat??????????????????????????????????????????е??????Ч???????????????1???????α????????????Χ???????????????????Ч?????????-1?? ??Clojure???????????????
????(deftest test-with-identity-nat
????(testing "identity of natural integers"
????(is (= 1 (identity-nat 1))))
????(testing "throw exception for non-natural integers"
????(is (thrown? RuntimeException (identity-nat -1)))))
???????????????????????identity-nat??????????????????0????????????????????????????????????????????????????????п??????????????á?????????????????????????μ??????????????塣???????????????????????????д????????????????????????????????β???????????????辯?棬??????????????????????