Property-based testing

 

Test like a scientist

What's this?  

What's this?  

Checks that a function abides by a property or hypothesis.  

What's this?  

The property is abstracted from any input and output data.

Common properties

Associative

a + (b + c) = (a + b) + c

listA.append(listB.append(listC)) == (listA.append(listB).append(listC)

Let's see an example

Some benefits

  • Cover the scope of all possible inputs.
     
  • Approach to an automated exploratory testing.
     
  • Reproducible and replayable.
     
  • Use of generators.

And in our "real" world?
Examples

  • Complex calculation functions.
     
  • App status manage functions.
      
  • Domain mapping testing.
     
  • E2E tests. (API, UI etc.)

Let's practice?!

Documentation

Common properties

Commutative

a + b = b + a

Math.max(a,b) == Math.max(b,a)

Common properties

Idempotent

f(a) = f(f(a))

Math.abs(x) == Math.abs(Math.abs(x))

Common properties

Bilbo testing (There And Back Again)

list == list.Reverse().Reverse()

book == book.toDto().toDomain()

Property-based testing - Test like a scientist

By Aritz Águila

Property-based testing - Test like a scientist

CATE for a Property-based testing

  • 878