SlideShare a Scribd company logo
1 of 18
Download to read offline
Hunting Bugs While Sleeping
Property-Based Testing with C#
Paul Amazona
Developer
@whatevergeek
Property-Based Testing with
Hypothesis
https://tinyurl.com/hypothesis-prezo
Property-Based Testing
A type of testing that asserts based on
properties that describe the relationship
between the input and output
of the function being tested.
Input
Data
Function
Output
Result
Testing the Multiply Function
[Fact]
public void TestMultiplyUsingExample1()
{
int expected = 2*3;
int actual = Arithmetic.Multiply(2, 3);
Assert.Equal(expected, actual);
}
[Fact]
public void TestMultiplyUsingExample1()
{
int expected = 2*3;
int actual = Arithmetic.Multiply(2, 3);
Assert.Equal(expected, actual);
}
Testing the Multiply Function
Testing using Example Outputs
[Fact]
public void TestMultiplyUsingExample1()
{
int expected = 6;
int actual = Arithmetic.Multiply(2, 3);
Assert.Equal(expected, actual);
}
[Fact]
public void TestMultiplyUsingExample2()
{
int expected = 20;
int actual = Arithmetic.Multiply(4, 5);
Assert.Equal(expected, actual);
}
Parameterized Tests
[Theory]
[InlineData(2, 3, 6)]
[InlineData(4, 5, 20)]
public void TestMultiplyUsingExample1And2(int factor1, int factor2, int expected)
{
int actual = Arithmetic.Multiply(factor1, factor2);
Assert.Equal(expected, actual);
}
Multiplication Properties
• Commutative property
When two numbers are multiplied together, the product is the same regardless of the order of the
multiplicands.
For example 4 * 2 = 2 * 4
• Associative Property
When three or more numbers are multiplied, the product is the same regardless of the grouping of the
factors.
For example (2 * 3) * 4 = 2 * (3 * 4)
• Multiplicative Identity Property
The product of any number and one is that number.
For example 5 * 1 = 5.
• Distributive property
The sum of two numbers times a third number is equal to the sum of each addend times the third number.
For example 4 * (6 + 3) = 4*6 + 4*3
Property-Based Tests (PBT)
with Predetermined Inputs
[Theory]
[InlineData(2, 3)]
[InlineData(4, 5)]
public void TestMultiplyCommutativeProperty(int factor1, int factor2) =>
Assert.Equal(Multiply(factor1, factor2), Multiply(factor2, factor1));
Property-Based Tests (PBT)
with Predetermined Inputs
public static IEnumerable<object[]> GetPredeterminedFactorList() =>
Enumerable.Range(1, 100).Select(f => new object[] { f });
[Theory]
[MemberData(nameof(GetPredeterminedFactorList))]
public void TestMultiplyIdentityProperty(int factor) =>
Assert.Equal(Multiply(factor, 1), factor);
[Theory]
[InlineData(2, 3, 4)]
[InlineData(4, 5, 6)]
public void TestMultiplyDistributiveProperty(int factor1, int factor2, int factor3) =>
Assert.Equal(Multiply(factor1, (factor2 + factor3)),
Multiply(factor1, factor2) + Multiply(factor1, factor3));
Property-Based Tests (PBT)
with Randomized Inputs
[Property]
public void TestMultiplyCommutativeProperty(int factor1, int factor2) =>
Assert.Equal(Multiply(factor1, factor2), Multiply(factor2, factor1));
Property-Based Tests Libraries…
Language PBT Libraries
python hypothesis
.NET (C#, F#, etc) FsCheck
haskell quickcheck
java junit-quickchek
javascript fast-check
swift SwiftCheck
scala ScalaCheck
<PackageReference Include="FsCheck.Xunit" Version="2.14.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.0.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
Setting Up a .NET PBT Project
DEMO
• Create PBT Project
• Use Test Explorer
• Show failing scenario
• Adjust sample size
• CustomProperty
Hunting Bugs with CI
Pipelines Process Characteristics
Normal CI Build-> Tests
with Predetermined Inputs
• Trigger: merge/PR/etc
• More predictable duration
• Purpose: Detect problems
early
Bug Hunting CI Build->Test
with Randomized Inputs
• Trigger: Scheduled build
(every hour, every day, etc).
• Can take time depending on
sample size of randomized
inputs
• Purpose: Hunt Bugs
Summary and Links
• Testing using Example Outputs
• Parameterized Tests
• Property-based Tests with Predetermined Inputs
• Property-based Tests with Randomized Inputs
• Bug Hunting CI Pipeline
Python Version of the Talk:
https://tinyurl.com/bughunt-python-pbt
C# Demo Source Code:
https://github.com/whatevergeek/csharp_netcore_pbt_demo
Paul Amazona
@whatevergeek

More Related Content

What's hot

Python programming –part 3
Python programming –part 3Python programming –part 3
Python programming –part 3Megha V
 
Python For Data Science Cheat Sheet
Python For Data Science Cheat SheetPython For Data Science Cheat Sheet
Python For Data Science Cheat SheetKarlijn Willems
 
Pj01 4-operators and control flow
Pj01 4-operators and control flowPj01 4-operators and control flow
Pj01 4-operators and control flowSasidharaRaoMarrapu
 
Python Programming: Data Structure
Python Programming: Data StructurePython Programming: Data Structure
Python Programming: Data StructureChan Shik Lim
 
Python programming- Part IV(Functions)
Python programming- Part IV(Functions)Python programming- Part IV(Functions)
Python programming- Part IV(Functions)Megha V
 
Giving Clarity to LINQ Queries by Extending Expressions R2
Giving Clarity to LINQ Queries by Extending Expressions R2Giving Clarity to LINQ Queries by Extending Expressions R2
Giving Clarity to LINQ Queries by Extending Expressions R2Ed Charbeneau
 
Mixing Functional and Object Oriented Approaches to Programming in C#
Mixing Functional and Object Oriented Approaches to Programming in C#Mixing Functional and Object Oriented Approaches to Programming in C#
Mixing Functional and Object Oriented Approaches to Programming in C#Skills Matter
 
Arrays in python
Arrays in pythonArrays in python
Arrays in pythonmoazamali28
 
Python programming –part 7
Python programming –part 7Python programming –part 7
Python programming –part 7Megha V
 
Arrays In Python | Python Array Operations | Edureka
Arrays In Python | Python Array Operations | EdurekaArrays In Python | Python Array Operations | Edureka
Arrays In Python | Python Array Operations | EdurekaEdureka!
 
Python dictionary : past, present, future
Python dictionary: past, present, futurePython dictionary: past, present, future
Python dictionary : past, present, futuredelimitry
 
Python 2.5 reference card (2009)
Python 2.5 reference card (2009)Python 2.5 reference card (2009)
Python 2.5 reference card (2009)gekiaruj
 
Python Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, DictionaryPython Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, DictionarySoba Arjun
 
Cheat sheet python3
Cheat sheet python3Cheat sheet python3
Cheat sheet python3sxw2k
 

What's hot (20)

C Language Lecture 15
C Language Lecture 15C Language Lecture 15
C Language Lecture 15
 
C Language Lecture 20
C Language Lecture 20C Language Lecture 20
C Language Lecture 20
 
Python programming –part 3
Python programming –part 3Python programming –part 3
Python programming –part 3
 
Python For Data Science Cheat Sheet
Python For Data Science Cheat SheetPython For Data Science Cheat Sheet
Python For Data Science Cheat Sheet
 
Pj01 4-operators and control flow
Pj01 4-operators and control flowPj01 4-operators and control flow
Pj01 4-operators and control flow
 
Composing method
Composing methodComposing method
Composing method
 
Python Programming: Data Structure
Python Programming: Data StructurePython Programming: Data Structure
Python Programming: Data Structure
 
Python programming- Part IV(Functions)
Python programming- Part IV(Functions)Python programming- Part IV(Functions)
Python programming- Part IV(Functions)
 
8 python data structure-1
8 python data structure-18 python data structure-1
8 python data structure-1
 
Giving Clarity to LINQ Queries by Extending Expressions R2
Giving Clarity to LINQ Queries by Extending Expressions R2Giving Clarity to LINQ Queries by Extending Expressions R2
Giving Clarity to LINQ Queries by Extending Expressions R2
 
Mixing Functional and Object Oriented Approaches to Programming in C#
Mixing Functional and Object Oriented Approaches to Programming in C#Mixing Functional and Object Oriented Approaches to Programming in C#
Mixing Functional and Object Oriented Approaches to Programming in C#
 
Arrays in python
Arrays in pythonArrays in python
Arrays in python
 
Python programming –part 7
Python programming –part 7Python programming –part 7
Python programming –part 7
 
Python list
Python listPython list
Python list
 
Arrays In Python | Python Array Operations | Edureka
Arrays In Python | Python Array Operations | EdurekaArrays In Python | Python Array Operations | Edureka
Arrays In Python | Python Array Operations | Edureka
 
Python dictionary : past, present, future
Python dictionary: past, present, futurePython dictionary: past, present, future
Python dictionary : past, present, future
 
Python 2.5 reference card (2009)
Python 2.5 reference card (2009)Python 2.5 reference card (2009)
Python 2.5 reference card (2009)
 
Python Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, DictionaryPython Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, Dictionary
 
Cheat sheet python3
Cheat sheet python3Cheat sheet python3
Cheat sheet python3
 
Data Analysis in Python
Data Analysis in PythonData Analysis in Python
Data Analysis in Python
 

Similar to Hunting Bugs While Sleeping: Property-Based Testing with C#

Mixing functional programming approaches in an object oriented language
Mixing functional programming approaches in an object oriented languageMixing functional programming approaches in an object oriented language
Mixing functional programming approaches in an object oriented languageMark Needham
 
using python module: doctest
using python module: doctestusing python module: doctest
using python module: doctestmitnk
 
Pragmatic unittestingwithj unit
Pragmatic unittestingwithj unitPragmatic unittestingwithj unit
Pragmatic unittestingwithj unitliminescence
 
Functional programming basics
Functional programming basicsFunctional programming basics
Functional programming basicsopenbala
 
Software engineering ⊇ Software testing
Software engineering ⊇ Software testingSoftware engineering ⊇ Software testing
Software engineering ⊇ Software testingPavel Tcholakov
 
Generating characterization tests for legacy code
Generating characterization tests for legacy codeGenerating characterization tests for legacy code
Generating characterization tests for legacy codeJonas Follesø
 
JSConf: All You Can Leet
JSConf: All You Can LeetJSConf: All You Can Leet
JSConf: All You Can Leetjohndaviddalton
 
unittest in 5 minutes
unittest in 5 minutesunittest in 5 minutes
unittest in 5 minutesRay Toal
 
Integration testing - Yasub Hashmi
Integration testing  - Yasub HashmiIntegration testing  - Yasub Hashmi
Integration testing - Yasub HashmiPiyush Rahate
 
How to write clean tests
How to write clean testsHow to write clean tests
How to write clean testsDanylenko Max
 
Functional Core, Reactive Shell
Functional Core, Reactive ShellFunctional Core, Reactive Shell
Functional Core, Reactive ShellGiovanni Lodi
 
Using xUnit as a Swiss-Aarmy Testing Toolkit
Using xUnit as a Swiss-Aarmy Testing ToolkitUsing xUnit as a Swiss-Aarmy Testing Toolkit
Using xUnit as a Swiss-Aarmy Testing ToolkitChris Oldwood
 
Showdown of the Asserts by Philipp Krenn
Showdown of the Asserts by Philipp KrennShowdown of the Asserts by Philipp Krenn
Showdown of the Asserts by Philipp KrennJavaDayUA
 
Ruslan Shevchenko - Property based testing
Ruslan Shevchenko - Property based testingRuslan Shevchenko - Property based testing
Ruslan Shevchenko - Property based testingIevgenii Katsan
 
pytest로 파이썬 코드 테스트하기
pytest로 파이썬 코드 테스트하기pytest로 파이썬 코드 테스트하기
pytest로 파이썬 코드 테스트하기Yeongseon Choe
 
Use the following data set that compares age to average years lef.docx
Use the following data set that compares age to average years lef.docxUse the following data set that compares age to average years lef.docx
Use the following data set that compares age to average years lef.docxdickonsondorris
 
33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good Tests33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good TestsTomek Kaczanowski
 

Similar to Hunting Bugs While Sleeping: Property-Based Testing with C# (20)

Mixing functional programming approaches in an object oriented language
Mixing functional programming approaches in an object oriented languageMixing functional programming approaches in an object oriented language
Mixing functional programming approaches in an object oriented language
 
using python module: doctest
using python module: doctestusing python module: doctest
using python module: doctest
 
Property-based testing
Property-based testingProperty-based testing
Property-based testing
 
Pragmatic unittestingwithj unit
Pragmatic unittestingwithj unitPragmatic unittestingwithj unit
Pragmatic unittestingwithj unit
 
Functional programming basics
Functional programming basicsFunctional programming basics
Functional programming basics
 
Software engineering ⊇ Software testing
Software engineering ⊇ Software testingSoftware engineering ⊇ Software testing
Software engineering ⊇ Software testing
 
Generating characterization tests for legacy code
Generating characterization tests for legacy codeGenerating characterization tests for legacy code
Generating characterization tests for legacy code
 
JSConf: All You Can Leet
JSConf: All You Can LeetJSConf: All You Can Leet
JSConf: All You Can Leet
 
unittest in 5 minutes
unittest in 5 minutesunittest in 5 minutes
unittest in 5 minutes
 
Integration testing - Yasub Hashmi
Integration testing  - Yasub HashmiIntegration testing  - Yasub Hashmi
Integration testing - Yasub Hashmi
 
How to write clean tests
How to write clean testsHow to write clean tests
How to write clean tests
 
Functional Core, Reactive Shell
Functional Core, Reactive ShellFunctional Core, Reactive Shell
Functional Core, Reactive Shell
 
Google guava
Google guavaGoogle guava
Google guava
 
Using xUnit as a Swiss-Aarmy Testing Toolkit
Using xUnit as a Swiss-Aarmy Testing ToolkitUsing xUnit as a Swiss-Aarmy Testing Toolkit
Using xUnit as a Swiss-Aarmy Testing Toolkit
 
Showdown of the Asserts by Philipp Krenn
Showdown of the Asserts by Philipp KrennShowdown of the Asserts by Philipp Krenn
Showdown of the Asserts by Philipp Krenn
 
Ruslan Shevchenko - Property based testing
Ruslan Shevchenko - Property based testingRuslan Shevchenko - Property based testing
Ruslan Shevchenko - Property based testing
 
pytest로 파이썬 코드 테스트하기
pytest로 파이썬 코드 테스트하기pytest로 파이썬 코드 테스트하기
pytest로 파이썬 코드 테스트하기
 
Use the following data set that compares age to average years lef.docx
Use the following data set that compares age to average years lef.docxUse the following data set that compares age to average years lef.docx
Use the following data set that compares age to average years lef.docx
 
33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good Tests33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good Tests
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 

Recently uploaded

Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AIABDERRAOUF MEHENNI
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 

Recently uploaded (20)

Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
Exploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the ProcessExploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the Process
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 

Hunting Bugs While Sleeping: Property-Based Testing with C#

  • 1. Hunting Bugs While Sleeping Property-Based Testing with C# Paul Amazona Developer @whatevergeek
  • 2.
  • 3.
  • 5. Property-Based Testing A type of testing that asserts based on properties that describe the relationship between the input and output of the function being tested. Input Data Function Output Result
  • 6. Testing the Multiply Function [Fact] public void TestMultiplyUsingExample1() { int expected = 2*3; int actual = Arithmetic.Multiply(2, 3); Assert.Equal(expected, actual); }
  • 7. [Fact] public void TestMultiplyUsingExample1() { int expected = 2*3; int actual = Arithmetic.Multiply(2, 3); Assert.Equal(expected, actual); } Testing the Multiply Function
  • 8. Testing using Example Outputs [Fact] public void TestMultiplyUsingExample1() { int expected = 6; int actual = Arithmetic.Multiply(2, 3); Assert.Equal(expected, actual); } [Fact] public void TestMultiplyUsingExample2() { int expected = 20; int actual = Arithmetic.Multiply(4, 5); Assert.Equal(expected, actual); }
  • 9. Parameterized Tests [Theory] [InlineData(2, 3, 6)] [InlineData(4, 5, 20)] public void TestMultiplyUsingExample1And2(int factor1, int factor2, int expected) { int actual = Arithmetic.Multiply(factor1, factor2); Assert.Equal(expected, actual); }
  • 10. Multiplication Properties • Commutative property When two numbers are multiplied together, the product is the same regardless of the order of the multiplicands. For example 4 * 2 = 2 * 4 • Associative Property When three or more numbers are multiplied, the product is the same regardless of the grouping of the factors. For example (2 * 3) * 4 = 2 * (3 * 4) • Multiplicative Identity Property The product of any number and one is that number. For example 5 * 1 = 5. • Distributive property The sum of two numbers times a third number is equal to the sum of each addend times the third number. For example 4 * (6 + 3) = 4*6 + 4*3
  • 11. Property-Based Tests (PBT) with Predetermined Inputs [Theory] [InlineData(2, 3)] [InlineData(4, 5)] public void TestMultiplyCommutativeProperty(int factor1, int factor2) => Assert.Equal(Multiply(factor1, factor2), Multiply(factor2, factor1));
  • 12. Property-Based Tests (PBT) with Predetermined Inputs public static IEnumerable<object[]> GetPredeterminedFactorList() => Enumerable.Range(1, 100).Select(f => new object[] { f }); [Theory] [MemberData(nameof(GetPredeterminedFactorList))] public void TestMultiplyIdentityProperty(int factor) => Assert.Equal(Multiply(factor, 1), factor); [Theory] [InlineData(2, 3, 4)] [InlineData(4, 5, 6)] public void TestMultiplyDistributiveProperty(int factor1, int factor2, int factor3) => Assert.Equal(Multiply(factor1, (factor2 + factor3)), Multiply(factor1, factor2) + Multiply(factor1, factor3));
  • 13. Property-Based Tests (PBT) with Randomized Inputs [Property] public void TestMultiplyCommutativeProperty(int factor1, int factor2) => Assert.Equal(Multiply(factor1, factor2), Multiply(factor2, factor1));
  • 14. Property-Based Tests Libraries… Language PBT Libraries python hypothesis .NET (C#, F#, etc) FsCheck haskell quickcheck java junit-quickchek javascript fast-check swift SwiftCheck scala ScalaCheck
  • 15. <PackageReference Include="FsCheck.Xunit" Version="2.14.0" /> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.0.1" /> <PackageReference Include="xunit" Version="2.4.1" /> <PackageReference Include="xunit.runner.visualstudio" Version="2.4.1"> Setting Up a .NET PBT Project DEMO • Create PBT Project • Use Test Explorer • Show failing scenario • Adjust sample size • CustomProperty
  • 16.
  • 17. Hunting Bugs with CI Pipelines Process Characteristics Normal CI Build-> Tests with Predetermined Inputs • Trigger: merge/PR/etc • More predictable duration • Purpose: Detect problems early Bug Hunting CI Build->Test with Randomized Inputs • Trigger: Scheduled build (every hour, every day, etc). • Can take time depending on sample size of randomized inputs • Purpose: Hunt Bugs
  • 18. Summary and Links • Testing using Example Outputs • Parameterized Tests • Property-based Tests with Predetermined Inputs • Property-based Tests with Randomized Inputs • Bug Hunting CI Pipeline Python Version of the Talk: https://tinyurl.com/bughunt-python-pbt C# Demo Source Code: https://github.com/whatevergeek/csharp_netcore_pbt_demo Paul Amazona @whatevergeek