SlideShare uma empresa Scribd logo
1 de 20
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

Mais conteúdo relacionado

Mais procurados

An Introduction to Property Based Testing
An Introduction to Property Based TestingAn Introduction to Property Based Testing
An Introduction to Property Based TestingC4Media
 
8 arrays and pointers
8  arrays and pointers8  arrays and pointers
8 arrays and pointersMomenMostafa
 
An imperative study of c
An imperative study of cAn imperative study of c
An imperative study of cTushar B Kute
 
9 character string &amp; string library
9  character string &amp; string library9  character string &amp; string library
9 character string &amp; string libraryMomenMostafa
 
Clean Coders Hate What Happens To Your Code When You Use These Enterprise Pro...
Clean Coders Hate What Happens To Your Code When You Use These Enterprise Pro...Clean Coders Hate What Happens To Your Code When You Use These Enterprise Pro...
Clean Coders Hate What Happens To Your Code When You Use These Enterprise Pro...Kevlin Henney
 
Programming with GUTs
Programming with GUTsProgramming with GUTs
Programming with GUTsKevlin Henney
 
What We Talk About When We Talk About Unit Testing
What We Talk About When We Talk About Unit TestingWhat We Talk About When We Talk About Unit Testing
What We Talk About When We Talk About Unit TestingKevlin Henney
 
The Ring programming language version 1.7 book - Part 91 of 196
The Ring programming language version 1.7 book - Part 91 of 196The Ring programming language version 1.7 book - Part 91 of 196
The Ring programming language version 1.7 book - Part 91 of 196Mahmoud Samir Fayed
 
FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2rohassanie
 
The Ring programming language version 1.10 book - Part 101 of 212
The Ring programming language version 1.10 book - Part 101 of 212The Ring programming language version 1.10 book - Part 101 of 212
The Ring programming language version 1.10 book - Part 101 of 212Mahmoud Samir Fayed
 
Promise: async programming hero
Promise: async programming heroPromise: async programming hero
Promise: async programming heroThe Software House
 
6 c control statements branching &amp; jumping
6 c control statements branching &amp; jumping6 c control statements branching &amp; jumping
6 c control statements branching &amp; jumpingMomenMostafa
 
Aplikasi menghitung matematika dengan c++
Aplikasi menghitung matematika dengan c++Aplikasi menghitung matematika dengan c++
Aplikasi menghitung matematika dengan c++radar radius
 

Mais procurados (20)

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

Destaque

Making property-based testing easier to read for humans
Making property-based testing easier to read for humansMaking property-based testing easier to read for humans
Making property-based testing easier to read for humansLaura M. Castro
 
Property-based testing
Property-based testingProperty-based testing
Property-based testingPeter Gromov
 
ScalaCheckでProperty-based Testing
ScalaCheckでProperty-based TestingScalaCheckでProperty-based Testing
ScalaCheckでProperty-based TestingKoji Agawa
 
Property-Based Testing for Services
Property-Based Testing for ServicesProperty-Based Testing for Services
Property-Based Testing for Servicesjessitron
 
Property based testing - Less is more
Property based testing - Less is moreProperty based testing - Less is more
Property based testing - Less is moreHo Tien VU
 
Better Tests, Less Code: Property-based Testing
Better Tests, Less Code: Property-based TestingBetter Tests, Less Code: Property-based Testing
Better Tests, Less Code: Property-based TestingC4Media
 
Property based Testing - generative data & executable domain rules
Property based Testing - generative data & executable domain rulesProperty based Testing - generative data & executable domain rules
Property based Testing - generative data & executable domain rulesDebasish Ghosh
 
An introduction to property based testing
An introduction to property based testingAn introduction to property based testing
An introduction to property based testingScott Wlaschin
 
UNIT TESTING PPT
UNIT TESTING PPTUNIT TESTING PPT
UNIT TESTING PPTsuhasreddy1
 

Destaque (11)

Making property-based testing easier to read for humans
Making property-based testing easier to read for humansMaking property-based testing easier to read for humans
Making property-based testing easier to read for humans
 
Property-based testing
Property-based testingProperty-based testing
Property-based testing
 
Property-based testing
Property-based testingProperty-based testing
Property-based testing
 
Pino coviello, rodriguez
Pino coviello, rodriguezPino coviello, rodriguez
Pino coviello, rodriguez
 
ScalaCheckでProperty-based Testing
ScalaCheckでProperty-based TestingScalaCheckでProperty-based Testing
ScalaCheckでProperty-based Testing
 
Property-Based Testing for Services
Property-Based Testing for ServicesProperty-Based Testing for Services
Property-Based Testing for Services
 
Property based testing - Less is more
Property based testing - Less is moreProperty based testing - Less is more
Property based testing - Less is more
 
Better Tests, Less Code: Property-based Testing
Better Tests, Less Code: Property-based TestingBetter Tests, Less Code: Property-based Testing
Better Tests, Less Code: Property-based Testing
 
Property based Testing - generative data & executable domain rules
Property based Testing - generative data & executable domain rulesProperty based Testing - generative data & executable domain rules
Property based Testing - generative data & executable domain rules
 
An introduction to property based testing
An introduction to property based testingAn introduction to property based testing
An introduction to property based testing
 
UNIT TESTING PPT
UNIT TESTING PPTUNIT TESTING PPT
UNIT TESTING PPT
 

Semelhante a Property Based Testing

Mutation testing - the way to improve unit tests quality
Mutation testing - the way to improve unit tests qualityMutation testing - the way to improve unit tests quality
Mutation testing - the way to improve unit tests qualityVadim Mikhnevych
 
How to write clean tests
How to write clean testsHow to write clean tests
How to write clean testsDanylenko Max
 
JUnit Kung Fu: Getting More Out of Your Unit Tests
JUnit Kung Fu: Getting More Out of Your Unit TestsJUnit Kung Fu: Getting More Out of Your Unit Tests
JUnit Kung Fu: Getting More Out of Your Unit TestsJohn Ferguson Smart Limited
 
Transaction is a monad
Transaction is a  monadTransaction is a  monad
Transaction is a monadJarek Ratajski
 
help me Java projectI put problem and my own code in the linkmy .pdf
help me Java projectI put problem and my own code in the linkmy .pdfhelp me Java projectI put problem and my own code in the linkmy .pdf
help me Java projectI put problem and my own code in the linkmy .pdfarihantmum
 
ES6 patterns in the wild
ES6 patterns in the wildES6 patterns in the wild
ES6 patterns in the wildJoe Morgan
 
The secret unit testing tools no one ever told you about
The secret unit testing tools no one ever told you aboutThe secret unit testing tools no one ever told you about
The secret unit testing tools no one ever told you aboutDror Helper
 
Dealing with combinatorial explosions and boring tests
Dealing with combinatorial explosions and boring testsDealing with combinatorial explosions and boring tests
Dealing with combinatorial explosions and boring testsAlexander Tarlinder
 
Ruslan Shevchenko - Property based testing
Ruslan Shevchenko - Property based testingRuslan Shevchenko - Property based testing
Ruslan Shevchenko - Property based testingIevgenii Katsan
 
Introduction to nsubstitute
Introduction to nsubstituteIntroduction to nsubstitute
Introduction to nsubstituteSuresh Loganatha
 
Go vs C++ - CppRussia 2019 Piter BoF
Go vs C++ - CppRussia 2019 Piter BoFGo vs C++ - CppRussia 2019 Piter BoF
Go vs C++ - CppRussia 2019 Piter BoFTimur Safin
 
Ensure code quality with vs2012
Ensure code quality with vs2012Ensure code quality with vs2012
Ensure code quality with vs2012Sandeep Joshi
 
This is a Java Code- you are only creating unit test for Validate and.pdf
This is a Java Code- you are only creating unit test for Validate and.pdfThis is a Java Code- you are only creating unit test for Validate and.pdf
This is a Java Code- you are only creating unit test for Validate and.pdfaamousnowov
 

Semelhante a Property Based Testing (20)

Mutation testing - the way to improve unit tests quality
Mutation testing - the way to improve unit tests qualityMutation testing - the way to improve unit tests quality
Mutation testing - the way to improve unit tests quality
 
Developer Testing Tools Roundup
Developer Testing Tools RoundupDeveloper Testing Tools Roundup
Developer Testing Tools Roundup
 
How to write clean tests
How to write clean testsHow to write clean tests
How to write clean tests
 
Mutation Testing
Mutation TestingMutation Testing
Mutation Testing
 
Benefits of Kotlin
Benefits of KotlinBenefits of Kotlin
Benefits of Kotlin
 
130706266060138191
130706266060138191130706266060138191
130706266060138191
 
JUnit Kung Fu: Getting More Out of Your Unit Tests
JUnit Kung Fu: Getting More Out of Your Unit TestsJUnit Kung Fu: Getting More Out of Your Unit Tests
JUnit Kung Fu: Getting More Out of Your Unit Tests
 
ppopoff
ppopoffppopoff
ppopoff
 
Transaction is a monad
Transaction is a  monadTransaction is a  monad
Transaction is a monad
 
Vs c# lecture7 2
Vs c# lecture7  2Vs c# lecture7  2
Vs c# lecture7 2
 
help me Java projectI put problem and my own code in the linkmy .pdf
help me Java projectI put problem and my own code in the linkmy .pdfhelp me Java projectI put problem and my own code in the linkmy .pdf
help me Java projectI put problem and my own code in the linkmy .pdf
 
ES6 patterns in the wild
ES6 patterns in the wildES6 patterns in the wild
ES6 patterns in the wild
 
Vp lecture 9 ararat
Vp lecture 9 araratVp lecture 9 ararat
Vp lecture 9 ararat
 
The secret unit testing tools no one ever told you about
The secret unit testing tools no one ever told you aboutThe secret unit testing tools no one ever told you about
The secret unit testing tools no one ever told you about
 
Dealing with combinatorial explosions and boring tests
Dealing with combinatorial explosions and boring testsDealing with combinatorial explosions and boring tests
Dealing with combinatorial explosions and boring tests
 
Ruslan Shevchenko - Property based testing
Ruslan Shevchenko - Property based testingRuslan Shevchenko - Property based testing
Ruslan Shevchenko - Property based testing
 
Introduction to nsubstitute
Introduction to nsubstituteIntroduction to nsubstitute
Introduction to nsubstitute
 
Go vs C++ - CppRussia 2019 Piter BoF
Go vs C++ - CppRussia 2019 Piter BoFGo vs C++ - CppRussia 2019 Piter BoF
Go vs C++ - CppRussia 2019 Piter BoF
 
Ensure code quality with vs2012
Ensure code quality with vs2012Ensure code quality with vs2012
Ensure code quality with vs2012
 
This is a Java Code- you are only creating unit test for Validate and.pdf
This is a Java Code- you are only creating unit test for Validate and.pdfThis is a Java Code- you are only creating unit test for Validate and.pdf
This is a Java Code- you are only creating unit test for Validate and.pdf
 

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