Property Based Testing

Kalil de Lima

  • Montevideo - Uruguay

  • Estudiante de Ing en computación - FING UdelaR

  • Desarrollador FULL STACK en Rootstrap

  • Organizador de Python MVD

Why is testing important?

Let's Try To WRite A Function

This Compiles, So IT SHould Work Right?

LET'S Write SOME TESTS JUST TO BE SURE

OK, TESTS Passed.
We're READY TO DEPLOY!

"Testing SHOWS THE PRESENCE, NOT THE ABSENCE of BUGS"

Edsger W. Dijkstra

PRoperty BASED TESTING

Example BASED TESTING

  • Exhaustiveness is left to the programmer
  • Edge cases need to be thought beforehand
  • Always runs the same set of tests

PROPERTY BASED TESTING

  • Exhaustiveness is done by a computer
  • Edge cases can be found within the same tests
  • A different test suite is ran every time

What IS PROPERTY BASED TESTING?

A bit of history

  • This testing style was started in Haskell in the 2000’sBy Koenn Claessen who initially wrote the QuickCheck Library.
  • Quickly became the standard way to test in Haskell.
  • Recovered relevance with this new wave of functional languages like Elixir, or Javascript.
  • Outside Haskell this style of testing is fairly new.

Normal FloW of A TEST CASE

  • Arrange

PREPARE THE DATA FOR THE OPERATION

  • ACT

PERFORM THE TESTED OPERATION

  • ASSERT

VERIFY THE CORRECTNESS OF THE RESULT

 

FloW of A PROPERTY BASED TEST CASE

  • GIVEN

THE TESTING FRAMEWORK PROVIDES SOME DATA

  • ACT

PERFORM THE TESTED OPERATION

  • ASSERT

VERIFY THE CORRECTNESS OF THE RESULT

 

WHAT CAN WE TEST WITH THIS?

PROPERTIES

INVARIANTS

An invariant is an affirmation that i can make about the software, it works like a mathematical theorem

 

Given:
Some preconditions

 

Then:
The INVARIANT hoLds true

 

SOME EXAMPLES

  • A store cannot sell more items that the amount it has in stock.
  • The transaction sum always is zero (an addition to an account and a subtraction from another account).
  • Your account balance should always be greater or equal to zero.
  • A sorted list has the same amount of items that the unsorted version.
  • Reversing a list twice should return the original list.
  • The sum of two odd numbers is an even number.

TOOLS

  • Hypothesis - Python/Ruby

  • JSVerify - Javascript

  • Excheck - Elixir

  • PROPTEST - RUST

SOME EXAMPLES

EXCHECK

HYPOTHESIS (RUBY)

A QUICK DEMO OF HOW IT WORKS

Some definitions Before we START

  1. A projection is a function whose result does not change if COMPOSED WITH ITSELF

  2. A vector is an element of a vector space that has a length and a direction

"TALK IS CHEAP. SHOW ME THE CODE"

Linus Torvalds

WRAPPING UP

  • ALWAYS LOOK FOR INVARIANTS IN YOUR CODE

  • THIS TYPE OF TEST TAKES CARE OF FINDING SOME EDGE CASES FOR YOU

  • PRETTY MUCH FREE TO INTEGRATE WITH EXISTING TESTS

  • EASY TO ADD FAILURES TO REGRESSIONS

WHEN IS THIS USEFUL?

  • OPEN SOURCE LIBRARIES

  • PLACES WITH LOTS OF DIFFERENT INPUTS

  • PLACES WITH WIDE, CONTINUOUS EDGE CASESS

QUESTIONS?

kalil.de.lima@fing.edu.uy

github.com/kaozdl

@KAoz_165

owu.herokuapp.com

THANKS FOR LISTENING!

kalil.de.lima@fing.edu.uy

github.com/kaozdl

@KAoz_165

owu.herokuapp.com

Property Based Testing

By Kalil De Lima

Property Based Testing

  • 186