SlideShare a Scribd company logo
Property Based Testing
Shishir Dwivedi
What is property Based Testing?
Property-based tests make statements about the
output of your code based on the input, and these
statements are verified for many different possible
inputs.
Example
Let’s think of some properties for the rectangle area function:
1. Given any two width and height values, the result is always a multiple
of the two
2. Order doesn’t matter. A rectangle which is 50×100 has the same area
as one which is 100×50
3. If we divide the area by width, we should get the height
public int findArea(int width, int height){
return width*height;
}
public boolean verifyArea(int height, int width,int area){
return (area==height*width)?true:false;
}
public boolean verifyHeight(int area, int widht, int height){
return(height==area/widht)?true:false;
}
public static void main(String[] args) {
for(int i=0;i<100;i++){
int height=(int) (Math.random()*100000);
int width=(int)(Math.random()*2000000);
int result=obj.findArea(height, width);
Assert.assertTrue(verifyArea(height, width, result));
Assert.assertTrue(verifyHeight(height, width, result));
}
}
//Generate List
for (List<Integer> any : someLists(integers())) {
System.out.println(any);
}
//Generate array
for (Integer[] arr : someArrays(integers(), Integer.class)) {
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
//Generate Key Value where value is lis of value
for (Pair<Integer, List<Integer>> any : somePairs(integers(),
lists(integers()))) {
System.out.println("Checking for Value" + any);
}
Different Types of Values Which we can generate
//Generate Key Value where value is lis of value
for (Pair<Integer, Integer> any : somePairs(integers(),
integers())) {
System.out.println("Checking for Value" + any);
}
//Generate list of strings
for(List<String> str: someLists(strings())){
System.out.println(str);
}
//Generate unique strings
for(String str: someUniqueValues(strings())){
System.out.println(str);
}
////Generate Sorted list
for(List<Integer> sortedList:someSortedLists(integers())){
System.out.println(sortedList);
}
Different Types of Values Which we can generate
//Generate unique string
for(String uniqueStr:someUniqueValues(strings())){
System.out.println(uniqueStr);
}
//Generate Non empty string
for(String uniqueStr:someUniqueValues(nonEmptyStrings())){
System.out.println(uniqueStr);
}
for(Set<Set<String>> map:someSets(sets(strings()))){
System.out.println(map);
}
Different Types of Values Which we can generate
package quickcheck;
public class OfferCalculation {
public int calculateAmount(int amount, int offeramount){
return amount-offeramount;
}
public int calculateConvFee(int amount, int conFee){
return amount+conFee;
}
public int calculatefinalAmount(int amount, int confee, int
discount){
return amount-discount+confee;
}
}
Offer Calculation Example
@Test
public void testOffer(){
OfferCalculation offer=new OfferCalculation();
SoftAssert soft=new SoftAssert();
boolean flag=false;
for (Pair<Integer, List<Integer>> any :
somePairs(integers(), lists(integers()))) {
System.out.println("Checking for Value"+ any);
for(int i=0;i<any.getSecond().size();i++){
if(offer.calculateAmount(any.getFirst(),
any.getSecond().get(i))>0) flag=true;
soft.assertTrue(flag);
}
}
}
@Test
public void testOfferConv(){
OfferCalculation offer=new OfferCalculation();
SoftAssert soft=new SoftAssert();
boolean flag=false;
for (Pair<Integer, List<Integer>> any :
somePairs(integers(), lists(integers()))) {
System.out.println("Checking for Value"+ any);
for(int i=0;i<any.getSecond().size();i++){
int amount= offer.calculateConvFee(any.getFirst(),
any.getSecond().get(i));
if(amount>any.getFirst()) flag=true;
soft.assertTrue(flag);
}
}
}
@Test
public void testOfferConvAndDiscount(){
OfferCalculation offer=new OfferCalculation();
SoftAssert soft=new SoftAssert();
boolean flag=false;
for (Pair<Integer, List<Integer>> any :
somePairs(integers(), lists(integers()))) {
System.out.println("Checking for Value"+ any);
for(int i=0;i<any.getSecond().size();i++){
int amount= offer.calculatefinalAmount(any.getFirst(),
any.getSecond().get(i),any.getSecond().get(i+1));
if(amount>0) flag=true;
soft.assertTrue(flag);
}
}
}
List<Integer> list;
double meanvalue=0;
double variance=0.0;
double standardDeviation=0.0;
/**
* calculate Mean value.
* @param list
* @return
*/
public double calculateMean(List<Integer> list){
int sum=0;
this.list=list;
for(int i=0;i<list.size();i++){
sum=sum+list.get(i);
}
meanvalue=sum/list.size();
return meanvalue;
}
Mean, Variance and Standard Deviation Example
/**
* Calcualte Variance
* @return
*/
public double calcualteVariance(){
for(int i=0;i<list.size();i++){
variance=variance+Math.pow(list.get(i)-meanvalue, 2);
}
variance=variance/list.size();
return variance;
}
/**
* Calcualteb Standard deviation
* @return
*/
public double calculateStandardDeviation(){
return Math.sqrt(variance);
}
Mean, Variance and Standard Deviation Example
@Test
public void testMeanCalculation() {
boolean flag;
for (List<Integer> any : someLists(integers(0, 10))) {
System.out.println("Checking for Value" + any);
VarianceCalcualtion var = new VarianceCalcualtion();
double meanValue = var.calculateMean(any);
Collections.sort(any);
int least = any.get(0);
int max = any.get(any.size() - 1);
if (meanValue < max && meanValue > least)
flag = true;
else
flag = false;
Assert.assertTrue(flag);
}
}
Mean, Variance and Standard Deviation Example
@Test
public void testMeanCalculationForNegativeValues(){
boolean flag;
for (List<Integer> any : someLists(integers(Integer.MIN_VALUE, 0))) {
System.out.println("Checking for Value" + any);
VarianceCalcualtion var = new VarianceCalcualtion();
double meanValue = var.calculateMean(any);
Collections.sort(any);
int least = any.get(0);
int max = any.get(any.size() - 1);
if (meanValue > max && meanValue < least)
flag = true;
else
flag = false;
Assert.assertTrue(flag);
}
}
Mean, Variance and Standard Deviation Example
@Test
public void testVariance(){
SoftAssert soft=new SoftAssert();
boolean flag;
for (List<Integer> any : someLists(integers(0, 1000000))) {
System.out.println("Checking for Value" + any);
VarianceCalcualtion var = new VarianceCalcualtion();
double meanValue = var.calculateMean(any);
double varianceValue=var.calcualteVariance();
if (varianceValue>meanValue)
flag = true;
else
flag = false;
soft.assertTrue(flag);
}
}
Mean, Variance and Standard Deviation Example
@Test
public void testSD() {
SoftAssert soft = new SoftAssert();
boolean flag;
for (List<Integer> any : someLists(integers(0, 1000000))) {
System.out.println("Checking for Value" + any);
VarianceCalcualtion var = new VarianceCalcualtion();
double meanValue = var.calculateMean(any);
double varianceValue = var.calcualteVariance();
double sd = var.calculateStandardDeviation();
if (varianceValue > meanValue && sd < meanValue && sd < varianceValue)
flag = true;
else
flag = false;
soft.assertTrue(flag);
}
}
Mean, Variance and Standard Deviation Example
<?php
use ErisGenerator;
use ErisTestTrait;
class AlwaysFailsTest extends PHPUnit_Framework_TestCase
{
use TestTrait;
public function testFailsNoMatterWhatIsTheInput()
{
$this->forAll(
Generatorelements(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'])
)
->then(function ($someChar) {
$this->fail("This test fails by design. '$someChar' was passed in");
});
}
}
Quick check in PHP
package gopter
import "testing"
func TestRunnerSingleWorker(t *testing.T) {
parameters := DefaultTestParameters()
testRunner := &runner{
parameters: parameters,
worker: func(num int, shouldStop shouldStop) *TestResult {
return &TestResult{
Status: TestPassed,
Succeeded: 1,
Discarded: 0,
}
},
}
result := testRunner.runWorkers()
if result.Status != TestPassed ||
result.Succeeded != 1 ||
result.Discarded != 0 {
t.Errorf("Invalid result: %#v", result)
}
}
Quick check in GO
use std::cmp::Ord;
use rand;
use super::{QuickCheck, StdGen, TestResult, quickcheck};
#[test]
fn prop_oob() {
fn prop() -> bool {
let zero: Vec<bool> = vec![];
zero[0]
}
match QuickCheck::new().quicktest(prop as fn() -> bool) {
Ok(n) => panic!("prop_oob should fail with a runtime error 
but instead it passed {} tests.", n),
_ => return,
}
}
Quick check in Rust

More Related Content

PDF
Core c sharp and .net quick reference
DOCX
informatics practices practical file
PPTX
Functional DDD
PPTX
Writing Good Tests
DOCX
JAVA AND MYSQL QUERIES
DOCX
TRAFFIC CODE MATLAB Function varargouttraffic code
PPTX
Core c sharp and .net quick reference
informatics practices practical file
Functional DDD
Writing Good Tests
JAVA AND MYSQL QUERIES
TRAFFIC CODE MATLAB Function varargouttraffic code

What's hot (20)

PDF
An Introduction to Property Based Testing
PDF
8 arrays and pointers
PPT
An imperative study of c
PDF
9 character string &amp; string library
PPTX
Quality Python Homework Help
PPTX
4. chapter iii
PPTX
3. chapter ii
DOCX
Applications
PPTX
Quality Python Homework Help
PDF
Clean Coders Hate What Happens To Your Code When You Use These Enterprise Pro...
PDF
Programming with GUTs
PPTX
Exceptional exceptions
PDF
What We Talk About When We Talk About Unit Testing
PDF
The Ring programming language version 1.7 book - Part 91 of 196
PPT
FP 201 - Unit 3 Part 2
PDF
The Ring programming language version 1.10 book - Part 101 of 212
PDF
Promise: async programming hero
PDF
6 c control statements branching &amp; jumping
DOCX
Statement
PDF
Aplikasi menghitung matematika dengan c++
An Introduction to Property Based Testing
8 arrays and pointers
An imperative study of c
9 character string &amp; string library
Quality Python Homework Help
4. chapter iii
3. chapter ii
Applications
Quality Python Homework Help
Clean Coders Hate What Happens To Your Code When You Use These Enterprise Pro...
Programming with GUTs
Exceptional exceptions
What We Talk About When We Talk About Unit Testing
The Ring programming language version 1.7 book - Part 91 of 196
FP 201 - Unit 3 Part 2
The Ring programming language version 1.10 book - Part 101 of 212
Promise: async programming hero
6 c control statements branching &amp; jumping
Statement
Aplikasi menghitung matematika dengan c++
Ad

Viewers also liked (11)

PDF
Making property-based testing easier to read for humans
PDF
Property-based testing
PDF
Property-based testing
DOC
Pino coviello, rodriguez
PDF
ScalaCheckでProperty-based Testing
PDF
Property-Based Testing for Services
PDF
Property based testing - Less is more
PDF
Better Tests, Less Code: Property-based Testing
PDF
Property based Testing - generative data & executable domain rules
PDF
An introduction to property based testing
PPTX
UNIT TESTING PPT
Making property-based testing easier to read for humans
Property-based testing
Property-based testing
Pino coviello, rodriguez
ScalaCheckでProperty-based Testing
Property-Based Testing for Services
Property based testing - Less is more
Better Tests, Less Code: Property-based Testing
Property based Testing - generative data & executable domain rules
An introduction to property based testing
UNIT TESTING PPT
Ad

Similar to Property Based Testing (20)

PDF
Ruslan Shevchenko - Property based testing
PDF
Beyond xUnit example-based testing: property-based testing with ScalaCheck
PDF
Software engineering ⊇ Software testing
PPTX
Mutation Testing
PDF
Spock Framework - Slidecast
PDF
Spock Framework
PDF
Property based tests and where to find them - Andrzej Jóźwiak - TomTom Webina...
PDF
Developer Testing Tools Roundup
PDF
Property Based BDD Examples (ETSI UCAAT 2016, Budapest)
PPTX
Kill the mutants and test your tests - Roy van Rijn
PDF
Kill the mutants - A better way to test your tests
PDF
Unit Testing
PDF
Mutation Testing at BzhJUG
PDF
Mutation Testing: Start Hunting The Bugs
PPTX
ScalaCheck
PPTX
GeeCON - Improve your tests with Mutation Testing
PDF
ppopoff
PDF
Type Driven Development @ Confitura 2014
PDF
Hunting Bugs While Sleeping: Property-Based Testing with C#
PDF
JUnit Kung Fu: Getting More Out of Your Unit Tests
Ruslan Shevchenko - Property based testing
Beyond xUnit example-based testing: property-based testing with ScalaCheck
Software engineering ⊇ Software testing
Mutation Testing
Spock Framework - Slidecast
Spock Framework
Property based tests and where to find them - Andrzej Jóźwiak - TomTom Webina...
Developer Testing Tools Roundup
Property Based BDD Examples (ETSI UCAAT 2016, Budapest)
Kill the mutants and test your tests - Roy van Rijn
Kill the mutants - A better way to test your tests
Unit Testing
Mutation Testing at BzhJUG
Mutation Testing: Start Hunting The Bugs
ScalaCheck
GeeCON - Improve your tests with Mutation Testing
ppopoff
Type Driven Development @ Confitura 2014
Hunting Bugs While Sleeping: Property-Based Testing with C#
JUnit Kung Fu: Getting More Out of Your Unit Tests

Property Based Testing

  • 2. What is property Based Testing? Property-based tests make statements about the output of your code based on the input, and these statements are verified for many different possible inputs.
  • 3. Example Let’s think of some properties for the rectangle area function: 1. Given any two width and height values, the result is always a multiple of the two 2. Order doesn’t matter. A rectangle which is 50×100 has the same area as one which is 100×50 3. If we divide the area by width, we should get the height
  • 4. public int findArea(int width, int height){ return width*height; } public boolean verifyArea(int height, int width,int area){ return (area==height*width)?true:false; } public boolean verifyHeight(int area, int widht, int height){ return(height==area/widht)?true:false; } public static void main(String[] args) { for(int i=0;i<100;i++){ int height=(int) (Math.random()*100000); int width=(int)(Math.random()*2000000); int result=obj.findArea(height, width); Assert.assertTrue(verifyArea(height, width, result)); Assert.assertTrue(verifyHeight(height, width, result)); } }
  • 5. //Generate List for (List<Integer> any : someLists(integers())) { System.out.println(any); } //Generate array for (Integer[] arr : someArrays(integers(), Integer.class)) { for (int i = 0; i < arr.length; i++) { System.out.println(arr[i]); } } //Generate Key Value where value is lis of value for (Pair<Integer, List<Integer>> any : somePairs(integers(), lists(integers()))) { System.out.println("Checking for Value" + any); } Different Types of Values Which we can generate
  • 6. //Generate Key Value where value is lis of value for (Pair<Integer, Integer> any : somePairs(integers(), integers())) { System.out.println("Checking for Value" + any); } //Generate list of strings for(List<String> str: someLists(strings())){ System.out.println(str); } //Generate unique strings for(String str: someUniqueValues(strings())){ System.out.println(str); } ////Generate Sorted list for(List<Integer> sortedList:someSortedLists(integers())){ System.out.println(sortedList); } Different Types of Values Which we can generate
  • 7. //Generate unique string for(String uniqueStr:someUniqueValues(strings())){ System.out.println(uniqueStr); } //Generate Non empty string for(String uniqueStr:someUniqueValues(nonEmptyStrings())){ System.out.println(uniqueStr); } for(Set<Set<String>> map:someSets(sets(strings()))){ System.out.println(map); } Different Types of Values Which we can generate
  • 8. package quickcheck; public class OfferCalculation { public int calculateAmount(int amount, int offeramount){ return amount-offeramount; } public int calculateConvFee(int amount, int conFee){ return amount+conFee; } public int calculatefinalAmount(int amount, int confee, int discount){ return amount-discount+confee; } } Offer Calculation Example
  • 9. @Test public void testOffer(){ OfferCalculation offer=new OfferCalculation(); SoftAssert soft=new SoftAssert(); boolean flag=false; for (Pair<Integer, List<Integer>> any : somePairs(integers(), lists(integers()))) { System.out.println("Checking for Value"+ any); for(int i=0;i<any.getSecond().size();i++){ if(offer.calculateAmount(any.getFirst(), any.getSecond().get(i))>0) flag=true; soft.assertTrue(flag); } } }
  • 10. @Test public void testOfferConv(){ OfferCalculation offer=new OfferCalculation(); SoftAssert soft=new SoftAssert(); boolean flag=false; for (Pair<Integer, List<Integer>> any : somePairs(integers(), lists(integers()))) { System.out.println("Checking for Value"+ any); for(int i=0;i<any.getSecond().size();i++){ int amount= offer.calculateConvFee(any.getFirst(), any.getSecond().get(i)); if(amount>any.getFirst()) flag=true; soft.assertTrue(flag); } } }
  • 11. @Test public void testOfferConvAndDiscount(){ OfferCalculation offer=new OfferCalculation(); SoftAssert soft=new SoftAssert(); boolean flag=false; for (Pair<Integer, List<Integer>> any : somePairs(integers(), lists(integers()))) { System.out.println("Checking for Value"+ any); for(int i=0;i<any.getSecond().size();i++){ int amount= offer.calculatefinalAmount(any.getFirst(), any.getSecond().get(i),any.getSecond().get(i+1)); if(amount>0) flag=true; soft.assertTrue(flag); } } }
  • 12. List<Integer> list; double meanvalue=0; double variance=0.0; double standardDeviation=0.0; /** * calculate Mean value. * @param list * @return */ public double calculateMean(List<Integer> list){ int sum=0; this.list=list; for(int i=0;i<list.size();i++){ sum=sum+list.get(i); } meanvalue=sum/list.size(); return meanvalue; } Mean, Variance and Standard Deviation Example
  • 13. /** * Calcualte Variance * @return */ public double calcualteVariance(){ for(int i=0;i<list.size();i++){ variance=variance+Math.pow(list.get(i)-meanvalue, 2); } variance=variance/list.size(); return variance; } /** * Calcualteb Standard deviation * @return */ public double calculateStandardDeviation(){ return Math.sqrt(variance); } Mean, Variance and Standard Deviation Example
  • 14. @Test public void testMeanCalculation() { boolean flag; for (List<Integer> any : someLists(integers(0, 10))) { System.out.println("Checking for Value" + any); VarianceCalcualtion var = new VarianceCalcualtion(); double meanValue = var.calculateMean(any); Collections.sort(any); int least = any.get(0); int max = any.get(any.size() - 1); if (meanValue < max && meanValue > least) flag = true; else flag = false; Assert.assertTrue(flag); } } Mean, Variance and Standard Deviation Example
  • 15. @Test public void testMeanCalculationForNegativeValues(){ boolean flag; for (List<Integer> any : someLists(integers(Integer.MIN_VALUE, 0))) { System.out.println("Checking for Value" + any); VarianceCalcualtion var = new VarianceCalcualtion(); double meanValue = var.calculateMean(any); Collections.sort(any); int least = any.get(0); int max = any.get(any.size() - 1); if (meanValue > max && meanValue < least) flag = true; else flag = false; Assert.assertTrue(flag); } } Mean, Variance and Standard Deviation Example
  • 16. @Test public void testVariance(){ SoftAssert soft=new SoftAssert(); boolean flag; for (List<Integer> any : someLists(integers(0, 1000000))) { System.out.println("Checking for Value" + any); VarianceCalcualtion var = new VarianceCalcualtion(); double meanValue = var.calculateMean(any); double varianceValue=var.calcualteVariance(); if (varianceValue>meanValue) flag = true; else flag = false; soft.assertTrue(flag); } } Mean, Variance and Standard Deviation Example
  • 17. @Test public void testSD() { SoftAssert soft = new SoftAssert(); boolean flag; for (List<Integer> any : someLists(integers(0, 1000000))) { System.out.println("Checking for Value" + any); VarianceCalcualtion var = new VarianceCalcualtion(); double meanValue = var.calculateMean(any); double varianceValue = var.calcualteVariance(); double sd = var.calculateStandardDeviation(); if (varianceValue > meanValue && sd < meanValue && sd < varianceValue) flag = true; else flag = false; soft.assertTrue(flag); } } Mean, Variance and Standard Deviation Example
  • 18. <?php use ErisGenerator; use ErisTestTrait; class AlwaysFailsTest extends PHPUnit_Framework_TestCase { use TestTrait; public function testFailsNoMatterWhatIsTheInput() { $this->forAll( Generatorelements(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']) ) ->then(function ($someChar) { $this->fail("This test fails by design. '$someChar' was passed in"); }); } } Quick check in PHP
  • 19. package gopter import "testing" func TestRunnerSingleWorker(t *testing.T) { parameters := DefaultTestParameters() testRunner := &runner{ parameters: parameters, worker: func(num int, shouldStop shouldStop) *TestResult { return &TestResult{ Status: TestPassed, Succeeded: 1, Discarded: 0, } }, } result := testRunner.runWorkers() if result.Status != TestPassed || result.Succeeded != 1 || result.Discarded != 0 { t.Errorf("Invalid result: %#v", result) } } Quick check in GO
  • 20. use std::cmp::Ord; use rand; use super::{QuickCheck, StdGen, TestResult, quickcheck}; #[test] fn prop_oob() { fn prop() -> bool { let zero: Vec<bool> = vec![]; zero[0] } match QuickCheck::new().quicktest(prop as fn() -> bool) { Ok(n) => panic!("prop_oob should fail with a runtime error but instead it passed {} tests.", n), _ => return, } } Quick check in Rust