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
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