QuickCheck Mini for Elixir. Thomas Arts Quviq AB

Similar documents
TESTING WEB SERVICES WITH WEBDRIVER AND QUICKCHECK

Testing. Wouter Swierstra and Alejandro Serrano. Advanced functional programming - Lecture 2. [Faculty of Science Information and Computing Sciences]

QuickCheck, SmallCheck & Reach: Automated Testing in Haskell. Tom Shackell

Simulink Verification and Validation

Testing the Hard Stuff and Staying Sane. John Hughes

MTAT Agile Software Development

QuickCheck: Beyond the Basics

A Small Web Server. Programming II - Elixir Version. Johan Montelius. Spring Term 2018

Software System Design and Implementation

WebDriver: Controlling your Web Browser

Phoenix Is Not Your Application. Lance Halvorsen ElixirConf EU 2016

More on functional programming

Hex. Package

Two Testing Tools for the Erlang Ecosystem

It s good to be here... I almost wasn t. Beautiful Tests by Bruce A. Tate icanmakeitbe*er

iex(1)> defmodule RepeatN do def repeat_n(function, count) do ...(1)> end repeat_n(function, count - 1) {:module, RepeatN,...}

ENHANCED EMBEDDED SYSTEMS NERVES PROJECT

Papers we love - Utrecht

Darcs what, why and how. Ganesh Sittampalam London HUG, 24 th April 2013

Don t Judge Software by Its (Code) Coverage

First name (printed): a. DO NOT OPEN YOUR EXAM BOOKLET UNTIL YOU HAVE BEEN TOLD TO BEGIN.

Quiz 3; Tuesday, January 27; 5 minutes; 5 points [Solutions follow on next page]

Macros, and protocols, and metaprogramming - Oh My!

The Actor Model, Part Two. CSCI 5828: Foundations of Software Engineering Lecture 18 10/23/2014

Property Based Testing : Shrinking Risk In Your Code. Amanda Pariveda Solutions Consultant Mined Minds Co-Founder

Induction and Semantics in Dafny

Property-Based Testing for Coq. Cătălin Hrițcu

Property-based testing, race conditions, and QuickCheck. John Hughes

UNIVERSITY OF EDINBURGH COLLEGE OF SCIENCE AND ENGINEERING SCHOOL OF INFORMATICS INFR08013 INFORMATICS 1 - FUNCTIONAL PROGRAMMING

PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions

How does ML deal with +?

AVL Trees. Programming II - Elixir Version. Johan Montelius. Spring Term 2018

A CutEr Tool. Kostis Sagonas

CS Lecture 18: Card Tricks. Announcements. Slides by D. Gries, L. Lee, S. Marschner, W. White

WE ARE ALL BLESSED. Bruce Tate

Sorting. Weiss chapter , 8.6

Introduction to Software Testing Chapter 4 Input Space Partition Testing

Python Development with PyDev and Eclipse -

Reverse Sort. array = (1..1_000).to_a. array.sort do item, other other <=> item end

Code BEAM SF Hype For Types. Using Dialyzer to bring type checking to your Elixir code

Breaking your code in new and exciting ways. Michael Newton

Topic 7: Algebraic Data Types

Work-in-progress: "Verifying" the Glasgow Haskell Compiler Core language

The Big Python Guide

CSC148-Section:L0301

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #29 Arrays in C

The Actor Model. CSCI 5828: Foundations of Software Engineering Lecture 13 10/04/2016

UNIVERSITY OF EDINBURGH COLLEGE OF SCIENCE AND ENGINEERING SCHOOL OF INFORMATICS INFR08013 INFORMATICS 1 - FUNCTIONAL PROGRAMMING

List are immutable Lists have recursive structure Lists are homogeneous

A programming example. A Ray Tracer. ray tracing. Architecture

AUTO-CAAS: Model-Based Fault Prediction and Diagnosis of Automotive Software

Growing and Shrinking Polygons for Random Testing of Computational Geometry Algorithms

More on Strings & Arrays

QCon London An Introduction to Property Based Testing. Aaron Bedra Chief Security Officer,

Priority Queues (Heaps)

Testing, Debugging, and Verification exam DIT082/TDA567. Day: 9 January 2016 Time: Will be published mid February or earlier

Parametricity. Types Are Documentation. Tony Morris

CS 1110 Prelim 2 November 6th, 2012

Loop Example 1: Sum from 1 to N

Better sorting algorithms (Weiss chapter )

Beyond Blocks: Python Session #1

Introduction to Programming, Aug-Dec 2006

CS205: Scalable Software Systems

A PropEr Talk. Kostis Sagonas. With PropEr help by Manolis Papadakis Eirini Arvaniti

More Complex Versions of the if Statement. Class 13

Testing for the Unexpected

Be#er Tes(ng with Less Work: QuickCheck Tes(ng in Prac(ce. John Hughes

CS 1110 Prelim 1 October 4th, 2012

isinstance and While Loops

Arrays and Strings

Unit testing with pytest and nose 1

C++ ARRAYS POINTERS POINTER ARITHMETIC. Problem Solving with Computers-I

Monads and all that I. Monads. John Hughes Chalmers University/Quviq AB

Parametricity. Types Are Documentation. Tony Morris

Programming Paradigms Written Exam (6 CPs)

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 15 Testing

Haskell Program Coverage Toolkit

Life in a Post-Functional World

Informatics 1 Functional Programming Lecture 11. Data Representation. Don Sannella University of Edinburgh

Static Contract Checking for Haskell

The BEAM community and efene

CS 2505 Computer Organization I

Programming Paradigms Written Exam (6 CPs)

CSE 2123: Collections: Priority Queues. Jeremy Morris

COMP1730/COMP6730 Programming for Scientists. Strings

Advanced Topics in Programming Languages Lecture 2 - Introduction to Haskell

This page intentionally left blank

functional ErlangVM Parallelism

A general introduction to Functional Programming using Haskell

Quiz Stuff. Use a full sheet of 8½x11" paper. (Half sheet? Half credit!) ...

Chapter 4 Introduction to Control Statements

Object Oriented Software Design - I

Structure and Interpretation of Computer Programs

36 Modular Arithmetic

Issue with Implementing PrimeSieve() in Go

Testing, Debugging, and Verification

Mergesort again. 1. Split the list into two equal parts

Learn Functional Programming with Elixir

CS1 Lecture 3 Jan. 18, 2019

let s implement a memory Asynchronous vs Synchronous the functional way a first try

Transcription:

QuickCheck Mini for Elixir Thomas Arts Quviq AB

From Unit test to Property Most developers agree that writing unit tests is useful. but also quickly gets boring An example: Does Erlang lists:seq(n,m) do the same as Enum.to_list(n.. m)?

From Unit test to Property Write some unit tests defmodule Examples do use ExUnit.Case test "Erlang seq the same as Enum.to_list" do assert :lists.seq(1, 5) == Enum.to_list(1.. 5) assert :lists.seq(-10, 10) == Enum.to_list(-10.. 10) assert :lists.seq(164532, 164532) == end end Enum.to_list(164532.. 164532)

From Unit test to Property Automated Unit tests: assert :lists.seq(1, 5) == Enum.to_list(1.. 5) assert :lists.seq(-10, 10) == Enum.to_list(-10.. 10) assert :lists.seq(164532, 164532) == Enum.to_list(164532.. 164532) What is so specific for these values? How many tests shall we write?

From Unit test to Property Generate tests from a property defmodule ExamplesEqc do use ExUnit.Case use EQC.ExUnit property "Erlang Sequence" do forall {m, n} <- {int, int} do ensure :lists.seq(m, n) == Enum.to_list(m.. n) end end end int is a generator for an arbitrary integer value. live demo

Automatically generated test cases...failed After 7 tests. {1,0} not ensured: [] == [1, 0] 1) test Property Erlang Sequence (ExamplesEqc) test/examples_eqc.exs:5 forall({m, n} <- {int, int}) do end ensure(:lists.seq(m, n) == Enum.to_list(m.. n)) Failed for {1, 0} Finished in 0.1 seconds (0.06s on load, 0.05s on tests) 1 tests, 1 failures

From Unit test to Property Generate tests from a property defmodule ExamplesEqc do use ExUnit.Case use EQC.ExUnit property "Erlang Sequence" do forall {m, n} <- {int, int} do implies n >= m do ensure :lists.seq(m, n) == Enum.to_list(m.. n) end end end end

Automatically generated test cases...x..xx..x.xxx...xx...xx..xx.x.x..xx...x.xx...x...x.xxx..x..x xxxx.x.x...x.x...xxx.x.x.xx.x.x(x10).x..x.x(x1)... OK, passed 100 tests. Finished in 1.2 seconds (0.06s on load, 1.2s on tests) 1 test, 0 failures Actually 1 property, 0 failures

Properties of software A property is something that should hold... for any valid input you supply The classic example [ICFP2000 QuickCheck paper] Reversing a list twice gives the original list property "Reversing a list" do forall l <- list(int) do ensure Enum.reverse(Enum.reverse(l)) == l end end

Properties of software QuickCheck for Haskell since 2000 QuickCheck for Erlang since 2006 Now available for each language... also Elixir. Not using QuickCheck is violating the rule of using "best practice" Download QuickCheck Mini from www.quviq.com and example from github.com/thomasarts/mini

General properties Well designed software has certain patterns... use these for testing. Examples Inverse operations Idempotent operations equivalence properties action/observe properties course material 2015 Quviq AB

Inverse properties property "List reverse" do end forall l <- list(int) do ensure Enum.reverse(Enum.reverse(l)) == l end property "Base64" do end forall str <- list(char()) do you can build your own generators :base64.decode(:base64.encode(str)) == str end course material 2015 Quviq AB

Idempotent properties property "Sorting" do forall l <- list(int) do Enum.sort(Enum.sort(l)) == Enum.sort(l) end end course material 2015 Quviq AB

equivalence properties property "Erlang Sequence" do forall {m, n} <- {int, int} do end end ensure :lists.seq(m, n) == Enum.to_list(m.. n) course material 2015 Quviq AB

action/observe properties property "Sorting" do forall l <- list(int) do is_sorted(enum.sort(l)) end end course material 2015 Quviq AB

Mix files Add eqc_ex to your mix dependencies defmodule Mini.Mixfile do end use Mix.Project def project do end [... test_pattern: "*_{test,eqc}.exs"] defp deps do end [... {:eqc_ex, "~> 1.2.4"} ] # Set the environment variable ERL_LIBS to PATH_OF QuickCheck Mini

Reversing things defmodule StringEqc do use ExUnit.Case use EQC.ExUnit end property "Reverse strings" do end forall string <- utf8 do end ensure String.reverse(String.reverse(string)) == string Idempotence A generator for utf8 strings

Reversing things Oeps L...Failed After 20 tests. <<"ß«I">> not ensured: "Iß«" == "ß«I" Shrinking xxxxxxxxxxxxxxxxxxxxxxxxx...xxxxxxxxxxxxxxxxxxx(5 times) <<"ß«">> not ensured: " ß«" == "ß«" 1) test Property Reverse strings (StringEqc) test/string_eqc.exs:5 forall(string <- utf8) do end ensure String.reverse(String.reverse(string)) == string " ߇߆߅߄߃߂߁߀" Failed for This is fishy

Reversing things For the non-believers... defmodule StringTest do end use ExUnit.Case test "Reverse strings" do end string = "\x{07eb} " assert String.valid?(string) assert String.valid?(String.reverse(string)) Oh, well... must be unicode that's wrong then. assert String.reverse(String.reverse(string)) == string

Process Practical use of QuickCheck 1. Consider which property should hold (not which test should pass) 2. Check the property (100 tests) false true Error in code or property Find out failure characteristics: - Running more tests - Trying another property - Tracing the code Check coverage of test data with collect

QuickCheck: property-based testing Software has certain properties... things that should always hold for that software QuickCheck is a tool that automatically generates test cases from these properties. Model Test Test Test Test Test case case case case case Minimal Test case

Benefits Less time spent writing test code One model replaces many tests Better testing Lots of combinations you d never test by hand Less time spent on diagnosis Failures minimized automatically