property based testing for better code · property based testing for better code. lots of tests...

40
@jessitron Property Based Testing for Better Code

Upload: others

Post on 25-May-2020

13 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Property Based Testing for Better Code · Property Based Testing for Better Code. lots of tests maintenance burden didn't test seams. Scala ScalaCheck Property Based Testing. Scala

@jessitron

Property Based Testing for Better Code

Page 2: Property Based Testing for Better Code · Property Based Testing for Better Code. lots of tests maintenance burden didn't test seams. Scala ScalaCheck Property Based Testing. Scala

lots of tests

maintenance burden

didn't test seams

Page 3: Property Based Testing for Better Code · Property Based Testing for Better Code. lots of tests maintenance burden didn't test seams. Scala ScalaCheck Property Based Testing. Scala

Scala

ScalaCheck

Property Based Testing

Page 4: Property Based Testing for Better Code · Property Based Testing for Better Code. lots of tests maintenance burden didn't test seams. Scala ScalaCheck Property Based Testing. Scala

Scala

ScalaCheck

Property Based Testing

Page 5: Property Based Testing for Better Code · Property Based Testing for Better Code. lots of tests maintenance burden didn't test seams. Scala ScalaCheck Property Based Testing. Scala
Page 6: Property Based Testing for Better Code · Property Based Testing for Better Code. lots of tests maintenance burden didn't test seams. Scala ScalaCheck Property Based Testing. Scala
Page 7: Property Based Testing for Better Code · Property Based Testing for Better Code. lots of tests maintenance burden didn't test seams. Scala ScalaCheck Property Based Testing. Scala
Page 8: Property Based Testing for Better Code · Property Based Testing for Better Code. lots of tests maintenance burden didn't test seams. Scala ScalaCheck Property Based Testing. Scala

Define Success

Page 9: Property Based Testing for Better Code · Property Based Testing for Better Code. lots of tests maintenance burden didn't test seams. Scala ScalaCheck Property Based Testing. Scala

Design

Documentation

Quality

Enable Change

Page 10: Property Based Testing for Better Code · Property Based Testing for Better Code. lots of tests maintenance burden didn't test seams. Scala ScalaCheck Property Based Testing. Scala

Generators Properties

Page 11: Property Based Testing for Better Code · Property Based Testing for Better Code. lots of tests maintenance burden didn't test seams. Scala ScalaCheck Property Based Testing. Scala

Generators

Page 12: Property Based Testing for Better Code · Property Based Testing for Better Code. lots of tests maintenance burden didn't test seams. Scala ScalaCheck Property Based Testing. Scala

GeneratorsDocument: what is valid input data?

Design: build up from components

Quality: automatic corner cases

Enable: tests of larger components

Page 13: Property Based Testing for Better Code · Property Based Testing for Better Code. lots of tests maintenance burden didn't test seams. Scala ScalaCheck Property Based Testing. Scala

Enable: tests of larger components

Page 14: Property Based Testing for Better Code · Property Based Testing for Better Code. lots of tests maintenance burden didn't test seams. Scala ScalaCheck Property Based Testing. Scala
Page 15: Property Based Testing for Better Code · Property Based Testing for Better Code. lots of tests maintenance burden didn't test seams. Scala ScalaCheck Property Based Testing. Scala
Page 16: Property Based Testing for Better Code · Property Based Testing for Better Code. lots of tests maintenance burden didn't test seams. Scala ScalaCheck Property Based Testing. Scala
Page 17: Property Based Testing for Better Code · Property Based Testing for Better Code. lots of tests maintenance burden didn't test seams. Scala ScalaCheck Property Based Testing. Scala
Page 18: Property Based Testing for Better Code · Property Based Testing for Better Code. lots of tests maintenance burden didn't test seams. Scala ScalaCheck Property Based Testing. Scala
Page 19: Property Based Testing for Better Code · Property Based Testing for Better Code. lots of tests maintenance burden didn't test seams. Scala ScalaCheck Property Based Testing. Scala
Page 20: Property Based Testing for Better Code · Property Based Testing for Better Code. lots of tests maintenance burden didn't test seams. Scala ScalaCheck Property Based Testing. Scala
Page 21: Property Based Testing for Better Code · Property Based Testing for Better Code. lots of tests maintenance burden didn't test seams. Scala ScalaCheck Property Based Testing. Scala

Game

Rules # turns

Player PlayerStrategy Strategy

Page 22: Property Based Testing for Better Code · Property Based Testing for Better Code. lots of tests maintenance burden didn't test seams. Scala ScalaCheck Property Based Testing. Scala

Rules

Page 23: Property Based Testing for Better Code · Property Based Testing for Better Code. lots of tests maintenance burden didn't test seams. Scala ScalaCheck Property Based Testing. Scala

case class Rules(temptationToDefect: Points, rewardForMutualCooperation: Points, punishmentForMutualDefection: Points, suckersPenalty: Points)

Page 24: Property Based Testing for Better Code · Property Based Testing for Better Code. lots of tests maintenance burden didn't test seams. Scala ScalaCheck Property Based Testing. Scala

property("The game is fair") = forAll {(rules: Rules, moves: MoveSet) => val oneWay = Rules.score(rules, moves) val theOtherWay = Rules.score(rules, moves.swap) !

oneWay.swap == theOtherWay }

object RuleTest extends Properties("Prisoners Dilemma") {

}

Page 25: Property Based Testing for Better Code · Property Based Testing for Better Code. lots of tests maintenance burden didn't test seams. Scala ScalaCheck Property Based Testing. Scala

property("Defection is always better for me") = forAll {(rules: Rules, theirMove: Move) => val ifIDefect = Rules.score(rules, (Defect, theirMove))._1 val ifICooperate = Rules.score(rules, (Cooperate, theirMove))._1 !

ifIDefect > ifICooperate }

object RuleTest extends Properties("Prisoners Dilemma") {

}

Page 26: Property Based Testing for Better Code · Property Based Testing for Better Code. lots of tests maintenance burden didn't test seams. Scala ScalaCheck Property Based Testing. Scala

A => B

Page 27: Property Based Testing for Better Code · Property Based Testing for Better Code. lots of tests maintenance burden didn't test seams. Scala ScalaCheck Property Based Testing. Scala

Properties

Page 28: Property Based Testing for Better Code · Property Based Testing for Better Code. lots of tests maintenance burden didn't test seams. Scala ScalaCheck Property Based Testing. Scala

Properties

Document: what is important?

Quality: they are always true

Page 29: Property Based Testing for Better Code · Property Based Testing for Better Code. lots of tests maintenance burden didn't test seams. Scala ScalaCheck Property Based Testing. Scala

property("The sucker always cooperates") = forAll(strategyGen, Gen.posNum[Int]) { (opponent: Strategy, turns: Int) => val allMoves:Stream[MoveSet] = Strategy.moves(Strategy.sucker, opponent).take(turns) val myMoves = allMoves.map (_._1) !

myMoves.forall(_ == Cooperate) }

Page 30: Property Based Testing for Better Code · Property Based Testing for Better Code. lots of tests maintenance burden didn't test seams. Scala ScalaCheck Property Based Testing. Scala

Complete properties

Incomplete properties

Relational properties

Page 31: Property Based Testing for Better Code · Property Based Testing for Better Code. lots of tests maintenance burden didn't test seams. Scala ScalaCheck Property Based Testing. Scala

forAll { (list: List[Int] => list.reverse.reverse = list }

Page 32: Property Based Testing for Better Code · Property Based Testing for Better Code. lots of tests maintenance burden didn't test seams. Scala ScalaCheck Property Based Testing. Scala

forAll { (input: Input) => oldWay(input) =? newWay(input) }

Page 33: Property Based Testing for Better Code · Property Based Testing for Better Code. lots of tests maintenance burden didn't test seams. Scala ScalaCheck Property Based Testing. Scala

forAll { (output: Output) => val input = from(output) subject(input) ?= output }

Page 34: Property Based Testing for Better Code · Property Based Testing for Better Code. lots of tests maintenance burden didn't test seams. Scala ScalaCheck Property Based Testing. Scala

property("All games end within the time limit") = forAll { (rules: Rules, players: Seq[Player], limit: Duration) => classify(players.size < 10, "small", "large") { val timer = new Timer() val output = Game.eachOnEach(rules)(sys, players, limit) val timeTaken = timer.check val timeOver = timeTaken - limit !

classify (timeOver < (fudge/2), "comfortable", "barely") { (timeTaken <= (limit + fudge)) :| s"$timeTaken was longer than $limit" }}}

Page 35: Property Based Testing for Better Code · Property Based Testing for Better Code. lots of tests maintenance burden didn't test seams. Scala ScalaCheck Property Based Testing. Scala
Page 36: Property Based Testing for Better Code · Property Based Testing for Better Code. lots of tests maintenance burden didn't test seams. Scala ScalaCheck Property Based Testing. Scala

Properties

Document: what is important?

Design: what do we need to know?

Quality: they are always true

Enable: changes to less important bits

Page 37: Property Based Testing for Better Code · Property Based Testing for Better Code. lots of tests maintenance burden didn't test seams. Scala ScalaCheck Property Based Testing. Scala

What does it make free?

What does it make explicit?

What does it make impossible?

Page 38: Property Based Testing for Better Code · Property Based Testing for Better Code. lots of tests maintenance burden didn't test seams. Scala ScalaCheck Property Based Testing. Scala

Scala

ScalaCheck

Property Based Testing

Page 39: Property Based Testing for Better Code · Property Based Testing for Better Code. lots of tests maintenance burden didn't test seams. Scala ScalaCheck Property Based Testing. Scala

http://www.artima.com/shop/scalacheck

Page 40: Property Based Testing for Better Code · Property Based Testing for Better Code. lots of tests maintenance burden didn't test seams. Scala ScalaCheck Property Based Testing. Scala

@jessitron

https://github.com/jessitron/scalacheck-prisoners-dilemma