+ All Categories
Home > Documents > Clojure testing with Midje - inovex GmbH · Clojure testing with Midje Author: Tobias Bayer, inovex...

Clojure testing with Midje - inovex GmbH · Clojure testing with Midje Author: Tobias Bayer, inovex...

Date post: 31-Jul-2018
Category:
Upload: truongduong
View: 225 times
Download: 0 times
Share this document with a friend
29
Testing with Midje Tobias Bayer :clojureD 2015, Berlin
Transcript
Page 1: Clojure testing with Midje - inovex GmbH · Clojure testing with Midje Author: Tobias Bayer, inovex Subject: clojure, midje, testing Keywords: clojure, midje, testing Created Date:

Testing with Midje Tobias Bayer

:clojureD 2015, Berlin

Page 2: Clojure testing with Midje - inovex GmbH · Clojure testing with Midje Author: Tobias Bayer, inovex Subject: clojure, midje, testing Keywords: clojure, midje, testing Created Date:

BBC, http://ichef.bbci.co.uk/naturelibrary/images/ic/credit/640x395/h/hi/highland_midge/highland_midge_1.jpg

Page 3: Clojure testing with Midje - inovex GmbH · Clojure testing with Midje Author: Tobias Bayer, inovex Subject: clojure, midje, testing Keywords: clojure, midje, testing Created Date:

Midge Midje

Page 4: Clojure testing with Midje - inovex GmbH · Clojure testing with Midje Author: Tobias Bayer, inovex Subject: clojure, midje, testing Keywords: clojure, midje, testing Created Date:

Tobias Bayer inovex GmbH [email protected] codebrickie

Page 5: Clojure testing with Midje - inovex GmbH · Clojure testing with Midje Author: Tobias Bayer, inovex Subject: clojure, midje, testing Keywords: clojure, midje, testing Created Date:
Page 6: Clojure testing with Midje - inovex GmbH · Clojure testing with Midje Author: Tobias Bayer, inovex Subject: clojure, midje, testing Keywords: clojure, midje, testing Created Date:
Page 7: Clojure testing with Midje - inovex GmbH · Clojure testing with Midje Author: Tobias Bayer, inovex Subject: clojure, midje, testing Keywords: clojure, midje, testing Created Date:

Why Midje? Non-lispy expectation syntax (conj [1 2] 3) => [1 2 3] vs (is (= [1 2 3] (conj [1 2] 3)))

Page 8: Clojure testing with Midje - inovex GmbH · Clojure testing with Midje Author: Tobias Bayer, inovex Subject: clojure, midje, testing Keywords: clojure, midje, testing Created Date:

Why Midje? Helpers and tools Checking functions for expectations Simple stubbing and mocking Auto-testing ...

Page 9: Clojure testing with Midje - inovex GmbH · Clojure testing with Midje Author: Tobias Bayer, inovex Subject: clojure, midje, testing Keywords: clojure, midje, testing Created Date:

Facts Checkables Checkers Prerequisites

Basic Building Blocks

Page 10: Clojure testing with Midje - inovex GmbH · Clojure testing with Midje Author: Tobias Bayer, inovex Subject: clojure, midje, testing Keywords: clojure, midje, testing Created Date:

Midje equivalent of a test

Facts

Page 11: Clojure testing with Midje - inovex GmbH · Clojure testing with Midje Author: Tobias Bayer, inovex Subject: clojure, midje, testing Keywords: clojure, midje, testing Created Date:

(facts "about +" (fact "1 plus 2 equals 3" (+ 1 2) => 3) (fact (+ -1 -1) => -2))

Facts

Page 12: Clojure testing with Midje - inovex GmbH · Clojure testing with Midje Author: Tobias Bayer, inovex Subject: clojure, midje, testing Keywords: clojure, midje, testing Created Date:

(future-fact "it returns fancy" (fancy-function) => "fancy") ;; => WORK TO DO

Facts

Page 13: Clojure testing with Midje - inovex GmbH · Clojure testing with Midje Author: Tobias Bayer, inovex Subject: clojure, midje, testing Keywords: clojure, midje, testing Created Date:

(facts "about +" (fact "1 plus 2 equals 3" (+ 1 2) => 3))

Checkables

Page 14: Clojure testing with Midje - inovex GmbH · Clojure testing with Midje Author: Tobias Bayer, inovex Subject: clojure, midje, testing Keywords: clojure, midje, testing Created Date:

(facts "about +" (fact "1 plus 2 equals 3" (+ 1 2) => 3))

Checkables

Page 15: Clojure testing with Midje - inovex GmbH · Clojure testing with Midje Author: Tobias Bayer, inovex Subject: clojure, midje, testing Keywords: clojure, midje, testing Created Date:

(facts "about +" (fact (+ 1 1) =not=> 3))

Checkables

Page 16: Clojure testing with Midje - inovex GmbH · Clojure testing with Midje Author: Tobias Bayer, inovex Subject: clojure, midje, testing Keywords: clojure, midje, testing Created Date:

Can be used instead of values on the right side of a checkable

Checkers

Page 17: Clojure testing with Midje - inovex GmbH · Clojure testing with Midje Author: Tobias Bayer, inovex Subject: clojure, midje, testing Keywords: clojure, midje, testing Created Date:

(facts "about my list" (fact "it contains the value 5" '(1 2 3 4 5 6) => (contains 5)) (fact "it contains the values 2 and 3" '(1 2 3 4 5 6) => (contains 2 3)) (fact "it contains an odd number followed by an even number" '(1 2 3 4 5 6) => (contains odd? even?)))

Checkers - contains

Page 18: Clojure testing with Midje - inovex GmbH · Clojure testing with Midje Author: Tobias Bayer, inovex Subject: clojure, midje, testing Keywords: clojure, midje, testing Created Date:

(facts "about my map" (fact "the value of x is 1" {:x 1 :y 2} => (contains {:x 1})) (fact "the value of y is even" {:x 1 :y 2} => (contains {:y even?})))

Checkers - contains

Page 19: Clojure testing with Midje - inovex GmbH · Clojure testing with Midje Author: Tobias Bayer, inovex Subject: clojure, midje, testing Keywords: clojure, midje, testing Created Date:

(throwing-function) => (throws Exception) (throwing-function) => (throws Exception "foo") (throwing-function) => (throws Exception #"(foo|bar)")

Checkers - throws

Page 20: Clojure testing with Midje - inovex GmbH · Clojure testing with Midje Author: Tobias Bayer, inovex Subject: clojure, midje, testing Keywords: clojure, midje, testing Created Date:

(fact "insert-value has a side effect" (reset-values) => irrelevant (exists-value? "foo") => falsey (insert-value "foo") => irrelevant (exists-value? "foo") => truthy)

Checkers - anything/irrelevant

Page 21: Clojure testing with Midje - inovex GmbH · Clojure testing with Midje Author: Tobias Bayer, inovex Subject: clojure, midje, testing Keywords: clojure, midje, testing Created Date:

(defn each-element-is-one-of [expected-elements] (fn [actual] (every? (set expected-elements) actual))) (fact "each element is one of 1-10" '(1 2 3 4 5 6) => (each-element-is-one-of (range 1 11)))

Checkers - own checkers

Page 22: Clojure testing with Midje - inovex GmbH · Clojure testing with Midje Author: Tobias Bayer, inovex Subject: clojure, midje, testing Keywords: clojure, midje, testing Created Date:

OO-world: stubbing and mocking

Prerequisites

Page 23: Clojure testing with Midje - inovex GmbH · Clojure testing with Midje Author: Tobias Bayer, inovex Subject: clojure, midje, testing Keywords: clojure, midje, testing Created Date:

(defn find-user [username] ;; Code that finds the user in the database ) (defn authenticate [username password] (let [user (find-user username)] (if (= (:password user) password) user))) (fact "it authenticates a user with a valid password" (authenticate "the fly" "12345") => truthy (provided (find-user "the fly") => {:name "the fly" :password "12345"}))

Prerequisites - Stub

Page 24: Clojure testing with Midje - inovex GmbH · Clojure testing with Midje Author: Tobias Bayer, inovex Subject: clojure, midje, testing Keywords: clojure, midje, testing Created Date:

(fact "it calls the second function 5 times with argument 'foo'" (first-function "foo") => irrelevant (provided (second-function "foo") => irrelevant :times 5)) (fact "it never calls the third function with any argument" (first-function "foo") => irrelevant (provided (third-function anything) => irrelevant :times 0))

Prerequisites - Mock

Page 25: Clojure testing with Midje - inovex GmbH · Clojure testing with Midje Author: Tobias Bayer, inovex Subject: clojure, midje, testing Keywords: clojure, midje, testing Created Date:

Project setup ~/.lein/profiles.clj {:user {:plugins [[lein-midje "3.1.3"]]}}

Page 26: Clojure testing with Midje - inovex GmbH · Clojure testing with Midje Author: Tobias Bayer, inovex Subject: clojure, midje, testing Keywords: clojure, midje, testing Created Date:

Project setup lein new midje midje-intro

Page 27: Clojure testing with Midje - inovex GmbH · Clojure testing with Midje Author: Tobias Bayer, inovex Subject: clojure, midje, testing Keywords: clojure, midje, testing Created Date:

Project setup lein new midje midje-intro

Page 28: Clojure testing with Midje - inovex GmbH · Clojure testing with Midje Author: Tobias Bayer, inovex Subject: clojure, midje, testing Keywords: clojure, midje, testing Created Date:

Project setup lein new midje midje-intro

Page 29: Clojure testing with Midje - inovex GmbH · Clojure testing with Midje Author: Tobias Bayer, inovex Subject: clojure, midje, testing Keywords: clojure, midje, testing Created Date:

DEMO


Recommended