type ContactType =
  | Twitter
  | Blog
  | GitHub

Full name: index.ContactType
union case ContactType.Twitter: ContactType
union case ContactType.Blog: ContactType
union case ContactType.GitHub: ContactType
val getContactInfo : _arg1:ContactType -> string

Full name: index.getContactInfo

Intro to Property Based Testing

About me

  • Shane Charles
  • White Light Computing, Inc.
  • Functional programming enthusiast
  • Board member for Winnipeg .Net UG
1: 
2: 
3: 
4: 
5: 
6: 
type ContactType = | Twitter | Blog | GitHub

let getContactInfo = function
  | Twitter -> "@dead_stroke"
  | Blog    -> "http://geekeh.com"
  | GitHub  -> "shanecharles"

What is property based testing?

The thing that QuickCheck does.

~ almost everybody

Not Quite

1: 
2: 
3: 
4: 
public class SomeClass 
{
    public string PropertyToTest { get; set; }
}

Merriam-Webster Definition of Property

  • a quality or trait belonging and especially peculiar to an individual or thing
  • an attribute common to all members of a class

Property Based Testing

A trait belonging to a process with results common to all members of a set of inputs.

Example

Any positive number multiplied by negative one will have a result less than zero.

  • Process: (*) -1
  • Input Set: Choose [1 .. ]
  • Result: < 0

We Already Have TDD

  • unit tests
  • integration tests

Test, Fail, Fix

fail

Test, Fail, Fix

fail

The Power of TDD

  • Build code one failure at a time
  • Growth with stability
  • Regression checks

TDD has Limits

  • Testing through specific examples
    • How many is enough?
  • Active becomes passive testing
  • Legacy code with no tests

Property Advantages

  • Generative testing
  • Always active testing
  • Generate actions or commands
  • Race conditions

Where to start

  • Get a chunk of code or a library you commonly use
    • Don't start with nothing (this isn't TDD)
Find your Testing Library

libs

http://hypothesis.works/articles/quickcheck-in-every-language/

Make Some Assumptions

What do you think is true about the code or process?

Generate inputs

haskell

Question Everything

  • Is it an actual failure or a problem with property?

Properties can require a lot of thought and can be difficult to come up with.

Patterns for Properties

  • Starting point for creating properties

There and Back

  • Serialization and deserialization
  • Set value and get value

There and Back

Different Paths Same Destination

  • x + y = y + x
  • x + x = x * 2

Same Destination

Some Things Never Change

  • Size of collection
  • elements of a collection after a sort

Some Never Change

Hard to Prove Easy to Verify

  • String tokenizer
    • Verify by concatenating the tokens
  • Sorting

verify

Test Oracle

  • Compare your results to a different 'proven' solution
    • Performance optimizations

oracle

Should not crash

  • Generate http requests
    • Get, Post, etc.
    • Accepted HttpStatuses

crash

Summary

  • Coming up with Properties can be difficult
    • Forces us to better understand the domain
  • Not meant for building
  • The more complex the problem the better

Use Both

Unit Test

tdd

Property Based Test

property-based-testing

Extra Resources