SlideShare a Scribd company logo
Property-based
Testing
Having over 10 years of experience with
Microsoft Technologies, Miguel has many
specializations, including C#, F#, Azure, and
DevOps practices.
MSDEVMTL co-organizer
Director of engineering at Nexus Innovations
https://blog.miguelbernard.com
https://www.linkedin.com/in/miguelbernard/
@MiguelBernard88
https://github.com/mbernard
State of unit testing
// Given
var input = 3;
// When
var result =
Calculator.Calculate(input);
// Then
Assert.Equal(0, result);
// Given
var input = 3;
// When
var result =
Calculator.Calculate(input);
// Then
Assert.Equal(5, result);
// Given
var input = 3;
// When
var result =
Calculator.Calculate(input);
// Then
Assert.Equal(5, result);
// Given
var input = 3;
// When
var result =
Calculator.Calculate(input);
// Then
Assert.Equal(3, result);
Property based testing
Property based testing
Property based testing
Property based testing
Property based testing
Property based testing
Property based testing
How many
tests are
enough?
I thought I was happy…
Problems with example-based tests (EBT)
DON’T SCALE DON’T HELP YOU TO
FIND EDGE CASES
BRITTLE
DON’T EXPLAIN
WHAT WAS THE
REQUIREMENT
OFTEN SPECIFIC TO
AN
IMPLEMENTATION
What’s Property-based testing?
What it’s not
•
•
•
•
•
•
More like
Property
Property-based test
(PBT)
Example with
ADD()
Commutativity
2+3 = 3+2 2-3 != 3-2 2*3 = 3*2
Associativity
Add(input1, input2)
Add(input2, input3)
(2+3)+4 = 2+(3+4) (2-3)-4 != 2-(3-4) (2*3)*4 = 2*(3*4)
Identity
2+0 = 2 2-0 = 2 2*0 != 2
Recap
Haaaa, so properties
are for math stuff
Common patterns
Different paths,
same result
Full circle
Constance
Execute twice
gets same
result
(Idempotence)
Hard to find,
easy to validate
Oracle
PBTs advantages
General, and thus are less brittle
Provide a better and more concise description of requirements than a bunch of examples
1 PBT can replace many, many, example-based tests
Reveal overlooked edge cases
Force you to think
Ensure deep understanding of requirements
More thinking = less typing
Force you to have a clean design
Why don’t we use
PBTs already then?
no kidding it’s really hard
• EBTs are still good for a lot of things
• Test known corner cases
• Regression
• Easier to understand for newcomers
Demo
The Diamond Kata
Given a letter, print a diamond starting with ‘A’ with the
supplied letter at the widest point.
public class Diamond
{
public static IEnumerable<string> Generate(char c)
{
yield return "A";
}
}
First NuGet
1. Non-Empty
[Property(Arbitrary = new[] { typeof(LetterGenerator) })]
public Property NotEmpty(char c)
{
return Diamond.Generate(c)
.All(s => s != string.Empty)
.ToProperty();
}
public static class LetterGenerator
{
public static Arbitrary<char> Generate() =>
Arb.Default.Char().Filter(c => c >= 'A' && c <= 'Z');
}
2. First line contains A
3. Last line contains A
[Property(Arbitrary = new[] { typeof(LetterGenerator) })]
public Property FirstLineContainsA(char c)
{
return Diamond.Generate(c)
.First().Contains('A’)
.ToProperty();
}
[Property(Arbitrary = new[] { typeof(LetterGenerator) })]
public Property LastLineContainsA(char c)
{
return Diamond.Generate(c)
.Last().Contains('A’)
.ToProperty();
}
4. Height equals Width
[Property(Arbitrary = new[] { typeof(LetterGenerator) })]
public Property DiamondWidthEqualsHeight(char c)
{
var diamond = Diamond.Generate(c).ToList();
return diamond.All(row => row.Length == diamond.Count)
.ToProperty();
}
5. Outside space
padding is
symmetric on each
side of a row
[Property(Arbitrary = new[] { typeof(LetterGenerator) })]
public Property SpacesPerRowAreSymmetric(char c)
{
return Diamond.Generate(c)
.All(row =>
CountLeadingSpaces(row) == CountTrailingSpaces(row))
.ToProperty();
}
6. Symmetric
around horizontal
axis
[Property(Arbitrary = new[] { typeof(LetterGenerator) })]
public Property SymmetricAroundHorizontalAxis(char c)
{
var diamond = Diamond.Generate(c).ToArray();
var half = diamond.Length / 2;
var topHalf = diamond[..half];
var bottomHalf = diamond[(half + 1)..];
return topHalf.Reverse()
.SequenceEqual(bottomHalf)
.ToProperty();
}
7. Symmetric
around vertical axis
[Property(Arbitrary = new[] { typeof(LetterGenerator) })]
public Property SymmetricAroundVerticalAxis(char c)
{
return Diamond.Generate(c).ToArray()
.All(row =>
{
var half = row.Length / 2;
var firstHalf = row[..half];
var secondHalf = row[(half + 1)..];
return firstHalf.Reverse().SequenceEqual(secondHalf);
}).ToProperty();
}
8. Input letter row
contains no outside
padding spaces
[Property(Arbitrary = new[] { typeof(LetterGenerator) })]
public Property InputLetterRowContainsNoOutsidePaddingSpaces
(char c)
{
var inputLetterRow = Diamond.Generate(c)
.ToArray()
.First(x => GetCharInRow(x) == c);
return (inputLetterRow[0] != ' ' && inputLetterRow[^1] != ' ')
.ToProperty();
}
First failure :’(
private static IEnumerable<string> Generate(char c)
{
yield return " A ";
yield return $"{c} {c}";
yield return " A ";
}
Works…. Most of the time
private static IEnumerable<string> Generate3(char c)
{
if (c == 'A')
{
yield return "A";
}
else
{
yield return " A ";
yield return $"{c} {c}";
yield return " A ";
}
}
Rock on!
9. Rows contain
letters in ascending
then descending
order
[Property(Arbitrary = new[] { typeof(LetterGenerator) })]
public Property RowsContainCorrectLetterInCorrectOrder
(char c)
{
var expected = new List<char>();
for (var i = 'A'; i < c; i++) expected.Add(i);
for (var i = c; i >= 'A'; i--) expected.Add(i);
var actual = Diamond.Generate(c).ToList()
.Select(GetCharInRow);
return actual.SequenceEqual(expected).ToProperty();
}
Recap
Few simple tests (10 tests, ~4-5 lines / test)
No need to change the tests when changing the implementation
Makes it easier to do TDD
Questions?
Resources
https://github.com/mbernard/property
-based-testing

More Related Content

PPTX
Constructor in c++
ODP
Beginning Scala Svcc 2009
PPTX
Lambda Expressions in C++
PPT
Functional techniques in Ruby
PDF
Function, Class
PPTX
Linq inside out
PDF
Fun with Lambdas: C++14 Style (part 1)
PDF
Introduction to Elixir
Constructor in c++
Beginning Scala Svcc 2009
Lambda Expressions in C++
Functional techniques in Ruby
Function, Class
Linq inside out
Fun with Lambdas: C++14 Style (part 1)
Introduction to Elixir

What's hot (19)

PDF
Web futures
PPT
Wakanday JS201 Best Practices
PPTX
Fun with Lambdas: C++14 Style (part 2)
PPTX
Domain specific languages - progressive f sharp tutorials nyc 2012
PDF
Time for Functions
PPT
Visual studio 2008
PDF
2 BytesC++ course_2014_c9_ pointers and dynamic arrays
PPTX
C sharp 8.0 new features
PDF
Generic programming and concepts that should be in C++
KEY
Scala implicits
PDF
Intro to Functional Programming
PDF
Fantom - Programming Language for JVM, CLR, and Javascript
PDF
Kotlin Starter Pack
PPTX
Library functions in c++
PPTX
New C# features
PPTX
Introduction to c++
PDF
Advanced Tagless Final - Saying Farewell to Free
PDF
Functions in C++
PPTX
Templates in C++
Web futures
Wakanday JS201 Best Practices
Fun with Lambdas: C++14 Style (part 2)
Domain specific languages - progressive f sharp tutorials nyc 2012
Time for Functions
Visual studio 2008
2 BytesC++ course_2014_c9_ pointers and dynamic arrays
C sharp 8.0 new features
Generic programming and concepts that should be in C++
Scala implicits
Intro to Functional Programming
Fantom - Programming Language for JVM, CLR, and Javascript
Kotlin Starter Pack
Library functions in c++
New C# features
Introduction to c++
Advanced Tagless Final - Saying Farewell to Free
Functions in C++
Templates in C++

Similar to Property based testing (20)

PPTX
Writing Good Tests
PDF
An introduction to property based testing
PDF
Hunting Bugs While Sleeping: Property-Based Testing with C#
PDF
An introduction to property-based testing
PDF
Transferring Software Testing Tools to Practice (AST 2017 Keynote)
PDF
Functions for nothing, and your tests for free
PPTX
Advances in Unit Testing: Theory and Practice
PDF
Shift-Left Testing: QA in a DevOps World by David Laulusa
PPTX
Tdd guide
PDF
Unit testing (workshop)
PPTX
Navigating the xDD Alphabet Soup
PDF
Test Driven Development
PPTX
Building unit tests correctly with visual studio 2013
PPTX
Pragmatic metaprogramming
PDF
The lazy programmer's guide to writing thousands of tests
PPTX
Introduction to TDD
PDF
Property based Testing - generative data & executable domain rules
PDF
Test driven development
PDF
Unit Testing Best Practices
PPTX
Tdd is not about testing (OOP)
Writing Good Tests
An introduction to property based testing
Hunting Bugs While Sleeping: Property-Based Testing with C#
An introduction to property-based testing
Transferring Software Testing Tools to Practice (AST 2017 Keynote)
Functions for nothing, and your tests for free
Advances in Unit Testing: Theory and Practice
Shift-Left Testing: QA in a DevOps World by David Laulusa
Tdd guide
Unit testing (workshop)
Navigating the xDD Alphabet Soup
Test Driven Development
Building unit tests correctly with visual studio 2013
Pragmatic metaprogramming
The lazy programmer's guide to writing thousands of tests
Introduction to TDD
Property based Testing - generative data & executable domain rules
Test driven development
Unit Testing Best Practices
Tdd is not about testing (OOP)

More from MSDEVMTL (20)

PPTX
Intro grpc.net
PPTX
Grpc and asp.net partie 2
PPTX
Improve cloud visibility and cost in Microsoft Azure
PPTX
Return on Ignite 2019: Azure, .NET, A.I. & Data
PPTX
C sharp 8.0 new features
PPTX
Asp.net core 3
PDF
MSDEVMTL Informations 2019
PPTX
Common features in webapi aspnetcore
PPTX
Groupe Excel et Power BI - Rencontre du 25 septembre 2018
PPTX
Api gateway
PPTX
Common features in webapi aspnetcore
PPTX
Stephane Lapointe: Governance in Azure, keep control of your environments
PPTX
Eric Routhier: Garder le contrôle sur vos coûts Azure
PDF
Data science presentation
PPTX
Michel Ouellette + Gabriel Lainesse: Process Automation & Data Analytics at S...
PPTX
Open id connect, azure ad, angular 5, web api core
PPTX
Yoann Clombe : Fail fast, iterate quickly with power bi and google analytics
TXT
CAE: etude de cas - Rolling Average
PDF
CAE: etude de cas
PPTX
Dan Edwards : Data visualization best practices with Power BI
Intro grpc.net
Grpc and asp.net partie 2
Improve cloud visibility and cost in Microsoft Azure
Return on Ignite 2019: Azure, .NET, A.I. & Data
C sharp 8.0 new features
Asp.net core 3
MSDEVMTL Informations 2019
Common features in webapi aspnetcore
Groupe Excel et Power BI - Rencontre du 25 septembre 2018
Api gateway
Common features in webapi aspnetcore
Stephane Lapointe: Governance in Azure, keep control of your environments
Eric Routhier: Garder le contrôle sur vos coûts Azure
Data science presentation
Michel Ouellette + Gabriel Lainesse: Process Automation & Data Analytics at S...
Open id connect, azure ad, angular 5, web api core
Yoann Clombe : Fail fast, iterate quickly with power bi and google analytics
CAE: etude de cas - Rolling Average
CAE: etude de cas
Dan Edwards : Data visualization best practices with Power BI

Recently uploaded (20)

PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PDF
17 Powerful Integrations Your Next-Gen MLM Software Needs
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PPTX
Transform Your Business with a Software ERP System
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PPTX
Advanced SystemCare Ultimate Crack + Portable (2025)
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PPTX
CHAPTER 2 - PM Management and IT Context
PDF
Autodesk AutoCAD Crack Free Download 2025
PPTX
L1 - Introduction to python Backend.pptx
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PPTX
history of c programming in notes for students .pptx
PPTX
Log360_SIEM_Solutions Overview PPT_Feb 2020.pptx
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
Designing Intelligence for the Shop Floor.pdf
PDF
AutoCAD Professional Crack 2025 With License Key
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
iTop VPN Free 5.6.0.5262 Crack latest version 2025
wealthsignaloriginal-com-DS-text-... (1).pdf
17 Powerful Integrations Your Next-Gen MLM Software Needs
Design an Analysis of Algorithms I-SECS-1021-03
Transform Your Business with a Software ERP System
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Advanced SystemCare Ultimate Crack + Portable (2025)
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
CHAPTER 2 - PM Management and IT Context
Autodesk AutoCAD Crack Free Download 2025
L1 - Introduction to python Backend.pptx
Design an Analysis of Algorithms II-SECS-1021-03
Navsoft: AI-Powered Business Solutions & Custom Software Development
Internet Downloader Manager (IDM) Crack 6.42 Build 41
history of c programming in notes for students .pptx
Log360_SIEM_Solutions Overview PPT_Feb 2020.pptx
Operating system designcfffgfgggggggvggggggggg
Designing Intelligence for the Shop Floor.pdf
AutoCAD Professional Crack 2025 With License Key
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
iTop VPN Free 5.6.0.5262 Crack latest version 2025

Property based testing

Editor's Notes

  • #18: Trop d’exemples à construire pour bien couvrir Integration des features = multiplication des paths possible How many tests would you have to write to exhaustively prove that these two classes both work correctly under all supported configurations? “More than a human can feasibly write” is the correct answer. Now imagine having to do this for three, four, or dozens of features all working together simultaneously. You simply can’t write manual tests to cover all of the scenarios that might occur in the real world. It’s not feasible.
  • #22: Trop vite
  • #36: By generating random input, property-based tests often reveal issues that you have overlooked, such as dealing with nulls, missing data, divide by zero, negative numbers, etc. more thinking, less typing