Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Clojure のテストについて
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
ayato
April 21, 2016
Programming
770
2
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Clojure のテストについて
ayato
April 21, 2016
More Decks by ayato
See All by ayato
マイクロサービス内で動くAPIをF#で書いている
ayato0211
1
1.6k
Clojureという言語が私逹にもたらしたもの
ayato0211
6
3.2k
3年間考え続けてきたWebアプリケーションにおけるテストの話
ayato0211
3
310
Re:REPL-Driven Development
ayato0211
3
1.4k
Meta Template Engine
ayato0211
2
1.2k
超変換! Hiccup data structure!!
ayato0211
2
670
About Integrant
ayato0211
0
610
Muscle Assert
ayato0211
0
330
Clojureを用いたWebアプリケーション開発
ayato0211
2
3.2k
Other Decks in Programming
See All in Programming
テーブルをDELETEした
yuzneri
0
130
ソフトウェア設計に溶けるインフラ ― AWS CDK のインフラ認識論
konokenj
3
720
20260722_microCMSで考える、AI時代のコンテンツ運用設計
yosh1
0
190
『コードを書く以外の』エンジニアリング〜課金基盤移行プロジェクト推進のためのTips4選
yuriko1211
0
560
全PRの83%がAIレビューだけでマージできるようになった開発組織はその後どうなったか
athug
0
1k
AI時代に設計が 最大の生産性レバーになる 意図駆動開発とデータを消さない設計|Don't Delete Your Data or Your Intent — Design as the Deepest Lever in the AI Era
tomohisa
0
200
TSX の <Hoge<Fuga>> という構文に驚いた話 / tsx-type-argument-syntax
kanaru0928
0
180
AI時代のPHPer生存戦略 ~「言語、もうなんでもよくない?」に本気で向き合う~
vivion
0
220
Built Our Own Background Agent at LayerX #aidevex_findy
layerx
PRO
9
4.3k
Google Apps Script で Ruby を動かす
kawahara
0
120
AI Engineeringは、AIプロダクトだけのものか? 〜AIがソフトウェアを作る時代の新しい当たり前〜 / No AI in your product. AI Engineering in your development.
rkaga
4
320
使用 Meilisearch 建立新聞搜尋工具
johnroyer
0
200
Featured
See All Featured
Into the Great Unknown - MozCon
thekraken
41
2.6k
Dominate Local Search Results - an insider guide to GBP, reviews, and Local SEO
greggifford
PRO
0
240
Building Adaptive Systems
keathley
44
3.2k
The #1 spot is gone: here's how to win anyway
tamaranovitovic
3
1.1k
Fantastic passwords and where to find them - at NoRuKo
philnash
52
3.8k
How to Build an AI Search Optimization Roadmap - Criteria and Steps to Take #SEOIRL
aleyda
1
2.1k
Site-Speed That Sticks
csswizardry
13
1.4k
Producing Creativity
orderedlist
PRO
348
40k
KATA
mclloyd
PRO
35
15k
Ten Tips & Tricks for a 🌱 transition
stuffmc
0
160
Noah Learner - AI + Me: how we built a GSC Bulk Export data pipeline
techseoconnect
PRO
0
340
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
230
23k
Transcript
$MPKVSFͷςετ ʹ͍ͭͯ $MPKVSFೱ͍Ί ୈճौ୩KBWB !BZBUP@Q$ZCP[V4UBSUVQT *OD
BZBUP@Q ͋ͽʔͰ͢ &NBDTͱ$MPKVSF͕͖Ͱ͢ $MPKVSFྺͷ$MPKVSFΤϯδχΞͰ͢ ݄͔Βฐࣾ$MPKVSJBOਓʹͳΓ·͢
ຖճ$MPKVSFͷΛͯ͠ΔํͰ͢
࣍ ͦͦ$MPKVSFͬͯͳΜͩ $MPKVSFͰͷςετʹ͍ͭͯ DMPKVSFUFTUΛͬͨجຊతͳςετͷॻ͖ํ ΑΓ࣮ફతͳॻ͖ํ ଞͷςετϑϨʔϜϫʔΫʹ͍ͭͯ
ͦͦ$MPKVSF ͬͯͳΜͩ
ͦͦ$MPKVSFͬͯͳΜͩ -JTQ ಈత։ൃ ؔܕϓϩάϥϛϯά ࣮ߦ࣌ϙϦϞʔϑΟζϜ ฒߦϓϩάϥϛϯά +7.ϗετ PS+BWB4DSJQU /&5
$MPKVSFͰͷ ςετʹ͍ͭͯ
3&1-ฤ 3&1-%SJWFO%FWFMPQNFOU ୯७ͳಈ࡞֬ೝͰςετέʔεॻ͘ΑΓ 3&1-Ͱ֬ೝͨ͠ํ͕ૣ͍
ςετίʔυฤ ࠓͷຊฤ 3%%Λओு͢Δ͜ͱଟ͍͚ͲςετΛશ͘ ॻ͔ͳ͍Θ͚Ͱͳ͍ ଞͷݴޠʹൺΔͱςετ͕ॻ͖͍͢ͱ ײ͡Δͱ͜Ζଟ͍
DMPKVSFUFTUΛͬͨ جຊతͳ ςετͷॻ͖ํ
(require '[clojure.test :refer :all]) (is (= 4 (+ 2 2)))
(is (instance? Long 256)) (is (.startsWith "abcde" "ab")) Ξαʔγϣϯ DMPKVSFUFTUωʔϜεϖʔεΛ͏ JTϚΫϩ͚֮ͩ͑Ε؆୯ʹςετ͕ग़དྷΔ
user> (is (= 5 (+ 2 2))) FAIL in ()
(form-init305064439250635979.clj:3) expected: (= 5 (+ 2 2)) actual: (not (= 5 4)) false Ξαʔγϣϯ 3&1-Ͱ࣮ߦՄೳ
(is (thrown? ArithmeticException (/ 1 0))) (is (thrown-with-msg? ArithmeticException #"Divide
by zero" (/ 1 0))) Ξαʔγϣϯ ྫ֎ͷ߹ಛघͳUISPXO ͳͲΛ͏
(is (= 5 (+ 2 2)) "Arithmetic") ςετͷυΩϡϝϯτԽ JTϚΫϩୈೋҾʹΞαʔγϣϯͷ આ໌ΛՃग़དྷΔ
(testing "Arithmetic" (testing "with positive integers" (is (= 4 (+
2 2))) (is (= 7 (+ 3 4)))) (testing "with negative integers" (is (= -4 (+ -2 -2))) (is (= -1 (+ 3 -4))))) ςετͷυΩϡϝϯτԽ UFTUJOHϚΫϩͰςετͷάϧʔϓԽ͕ग़དྷΔ จࣈྻGBJMVSFSFQPSUTʹؚ·ΕΔ Ϩϙʔτ34QFD෩
(with-test (defn my-function [x y] (+ x y)) (is (=
4 (my-function 2 2))) (is (= 7 (my-function 3 4)))) ςετͷఆٛ XJUIUFTUΛ͏ͱؔͷఆٛͱಉ࣌ʹ ςετΛهड़ग़དྷΔ ௨ৗ͏͜ͱͳ͍ ίʔυશମͷݟ௨͕͠ѱ͘ͳΔ
(deftest addition (is (= 4 (+ 2 2))) (is (=
7 (+ 3 4)))) (deftest subtraction (is (= 1 (- 4 3))) (is (= 3 (- 7 4)))) ςετͷఆٛ EFGUFTUϚΫϩΛ͏ ςετରͱςετΛ͚ͯهड़ग़དྷΔ ௨ৗͪ͜ΒΛ͏ͷ͕Ұൠత
(defn my-fixture [f] ;; 前処理 (f) ;; テストの実行 ;; 後処理
) (use-fixtures :each my-fixture) ςετϑΟΫενϟ ϑΟΫενϟࣗମؔΛͻͱͭड͚औΔ ؔͱͯ͠ఆٛͰ͖Δ ωʔϜεϖʔεʹରͯ͠Ξλον͢Δ
ΑΓ࣮ફతͳॻ͖ํ
!U@XBEB͞ΜͷΛղ͘ Լهͷ༷ΛςεςΟϯάϑϨʔϜϫʔΫΛͬͯςετίʔυΛ ॻ͖ͳ͕Β࣮͍ͯͩ͘͠͞ɻ ʲ༷ʳʮݱࡏ࣌ࠁʯʹԠͯ͡ɺѫࡰͷ༰ΛԼهͷΑ͏ʹͦΕͧΕฦ ͢ػೳΛ࡞͍ͨ͠ɻ λΠϜκʔϯ"TJB5PLZPͱ͢Δ ே Ҏ্ະຬ ͷ߹ɺʮ͓Α͏͍͟͝·͢ʯͱฦ͢
ன Ҏ্ະຬ ͷ߹ɺʮ͜Μʹͪʯͱฦ͢ Ҏ্ະຬ ͷ߹ɺʮ͜ΜΜʯͱฦ͢ ྫ࣌ʹHSFFUFSHSFFU ΛݺͿͱ͜Μʹͪͱฦ͢ IUUQTDPEFJRKQNBHB[JOF
(ns demoapp.greeter) (defn current-date-time [] (java.time.LocalDateTime/now)) (defn greet [] (let
[current-hour (.getHour (current-date-time))] (cond (<= 5 current-hour 11) "おはようございます" (<= 12 current-hour 17) "こんにちは" :else "こんばんは"))) ςετରͷίʔυ
(ns demoapp.greeter-test (:require [clojure.test :refer :all] [demoapp.greeter :as g :refer
:all]) (:import [java.time LocalDateTime])) (deftest greet-test (are [dt-str expect] (with-redefs [g/current-date-time (constantly (LocalDateTime/parse dt-str))] (= (greet) expect)) "2016-04-22T00:00" "こんばんは" "2016-04-22T04:59" "こんばんは" "2016-04-22T05:00" "おはようございます" "2016-04-22T11:59" "おはようございます" "2016-04-22T12:00" "こんにちは" "2016-04-22T17:59" "こんにちは" "2016-04-22T18:00" "こんばんは" "2016-04-22T23:59" "こんばんは")) ςετίʔυ
͍ͬͯΔςΫχοΫ BSFϚΫϩʹΑΔύϥϝλϥΠζυςετ XJUISFEFGTʹΑΔςετμϒϧͷ࣮ݱ
BSFϚΫϩ Έࣗମ୯७ JTϚΫϩͷܗʹల։͞ΕΔ ;; これが (are [a b x] (=
(+ a b) x) 1 2 3 5 10 15) ;; こう展開される (do (is (= (+ 1 2) 3)) (is (= (+ 5 10) 15)))
XJUISFEFGT 7BSΛ࠶ఆٛ͢Δ CPEZ෦ͷΈͰ༗ޮ (defn add3 [x] (+ x 3)) (with-redefs
[add3 (fn [x] (+ x 10))] (= (add3 10) 20)) ;; => true
(do (is (with-redefs [g/current-date-time (constantly (LocalDateTime/parse "2016-04-22T00:00"))] (= (greet) "こんばんは")))
;; ... 中略 (is (with-redefs [g/current-date-time (constantly (LocalDateTime/parse "2016-04-22T23:59"))] (= (greet) "こんばんは")))) ͭ·Γ͜͏ͳΔ
ଞͷ ςετϑϨʔϜϫʔΫ ʹ͍ͭͯ
લఏͱͯ͠ ࠷ۙͰ৽͍͠%4-ΛΘͳ͍Ͱ DMPKVSFUFTUΛ͏ͷ͕ྲྀߦΓ IUUQTSBTUFSJ[FJPCMPHDMPKVSFUIFHPPE QBSUTIUNM ͍ͬͯΔଆͷײ֮ͱͯ͠ DMPKVSFUFTUͰΓΔͱײ͡Δ͜ͱଟ͍
TQFDMK 34QFDΛ$MPKVSFͱ$MPKVSF4DSJQUʹ ͖࣋ͬͯͨͱ͍͏ײ͡ͷςετϑϨʔϜϫʔΫ ͋·Γಛ༗ͷػೳΈ͍ͨͳͷ͕ͳ͍ͷͰ ͏ϝϦοτ͕ྑ͔͘Βͳ͍
.JEKF ϙϐϡϥʔͳςετϑϨʔϜϫʔΫ GBDUBDUVBMFYQFDU ͱॻ͚Δ ߏͷൺֱ͕༰қͰ͋Δͷͱ.FUBDPOTUBOUT ͕ศརͰ͋Δ Ϧϩʔυ͢Δͱςετ͕࣮ߦ͞Εͯ͠·͏ υΩϡϝϯτ͕͋·Γ៉ྷʹ·ͱΊΒΕͯͳ͍
(def complex-data {"平成" [ {:id 1 :name "ayato_p"} {:id 2
:name "alea12"} ] "昭和" [ {:id 99 :name "foo"} {:id 100 :name "bar"} ]}) (fact complex-data => (contains {"平成" (contains [{:id 1 :name "ayato_p"}])})) .JEKF
(def members {1 "ayato_p" 2 "alea12" 3 "zer0_u"}) (defn get-id
[m] (get m :id)) (defn find-member [m] (get members (get-id m))) (fact (find-member ...m...) => "ayato_p" (provided (get-id ...m...) => 1)) .JEKF
GVEKF ܰྔ൛.JEKF _LCKBS ґଘ͕ؔগͳ͍ͷಛ ·ͩࢼͨ͜͠ͱͳ͍͚Ͳྑͦ͞͏
DMPKVSFUFTUDIFDL 2VJDL$IFDLGPS$MPKVSF $MPKVSFຊମͷςετʹΘΕ͍ͯΔ
·ͱΊ
·ͱΊ $MPKVSFͰςετΛॻ͘ͷ؆୯ ͱΓ͋͑ͣ໎ͬͨΒDMPKVSFUFTUΛ͏ UFTUDIFDLҰॹʹ͑Δ