SlideShare ist ein Scribd-Unternehmen logo
1 von 19
Downloaden Sie, um offline zu lesen
testing with
propertiesChristoph Neuroth ( )@c089
[horribly predictable quote]
“Testing shows the presence, not the
absence of bugs.” -- Edsger W. Dijkstra
Hoare: “ The role of testing, in theory, is to
establish the base propositions of an
inductive proof.[...] At present, this is mainly
theory [but] seems to show a possibility of
practical results, though proving correctness
is a laborious and expensive process.”
Perlis: “ Much of program complexity is
spurious and a number of test cases
properly studied will exhaust the testing
problem. The problem is to isolate the right
test cases, not to prove the algorithm, for
that follows after the choice of the proper
test cases.”
Diijkstra: “Testing shows the presence, not
the absence of bugs”
Testing with examples
specify correct output for chosen inputs
relatively easy to come up with
good communication tool
straightforward to implement
straightforward to overlook important ones
usually only cover a very small portion of your domain
yay, all tests passing!
//obviously
expect(Math.abs(0)).to.equal(0);
//negativevaluesbecomepositive
expect(Math.abs(-1)).to.equal(1);
expect(Math.abs(-5)).to.equal(5);
//whilepositivevaluesstayspositive
expect(Math.abs(1)).to.equal(1);
//shouldalsoworkwithfloatingpointnumbers
expect(Math.abs(-1.23)).to.equal(1.23);
expect(Math.abs(1.23)).to.equal(1.23);
//oh,andjavascriptknowsaboutnegative0
expect(Math.abs(-0)).to.equal(0);
Problem
-- ECMAScript® Language Specification
“The Number type has exactly
18437736874454810627
(that is, 264−253+3) values”
-- me
“The box above has 90000 pixels”
remember when you started learning TDD?
Math.abs=function(x){
if(x===0)return0;
if(x===-1||x===1)return1;
if((x>1&&x<2)||(x>-2&&x<-1))return1.23;
return5;
}
Testing with properties
not to be confused with properties on JavaScript objects
specify a property that holds for all / specified inputs
instead of finding a proof, test random inputs
similar to contracts
examples use jsverify, other implementations available
Describing a property
Humans: “whatever number we pass to abs, it should return
a number greater or equal than 0.”
Mathematicians: ∀ x ∈ ℝ: abs(x) ≥ 0
Programmers:
varp=require('jsverify');
p.check(p.forall(p.number(),function(x){
varactual=Math.abs(x);
returntypeofactual==='number'&&actual>=0;
}));
OK,passed100tests
Examples / Property / Proof
∀ x ∈ ℝ: abs(x) ≥ x
p.check(p.forall(p.number(),function(x){
returnMath.abs(x)>=x;
}));
Error:Failedafter3testsand0shrinks.rngState:8c0a43ef85c761db92;Coun
terexample:1.5644732909277081;
More, um, well, examples
Defining the domain using filters
varslowSum=function(n){
varsum=0,i;
for(i=1;i<=n;i++){sum+=i;}
returnsum;
},
gaussSum=function(n){return(n/2)*(n+1);},
positiveNumber=p.suchthat(p.integer(),function(i){returni>0;});
p.assert(p.forall(positiveNumber,function(i){
returnslowSum(i)===gaussSum(i);
}));
Defining the domain using custom
generators
oddNumber=function(){
return{
arbitrary:function(r){
varn=p.integer().arbitrary(r);
return2*n+1;
},
shrink:shrink.noop,
show:show.def
};
};
describe('odd',function(){
//varodd=function(n){returnn%2===1;};
varodd=function(n){returnMath.abs(n)%2===1;};
it('returnstrueforalloddnumbers',function(){
p.assert(p.forall(oddNumber(),odd));
});
idempotence: ∀ x: f(x) === f(f(x))
Reusable properties
varis_idempotent=function(generator,fn){
returnp.forall(generator,function(x){
return_.isEqual(fn(fn(s)),fn(s));
});
};
p.assert(is_idempotent(p.array(),_.compact));
p.assert(is_idempotent(p.array(),_.uniq));
Shrinking Counterexamples
p.assert(p.forall(p.array(p.integer()),function(l){
return_.all(l,function(n){returnn!=3;});
}));
Error:Failedafter27testsand4shrinks.rngState:88361ed782a9c0b45f;Cou
nterexample:[3];
Common use cases
f(x)>=y //assertingthefunction'srange
f(x)===f(f(x));//idempotence
a(x)===b(x);//regressiontestforreimplementation
newC1().f(x)===newC2().f(x);//closelyrelated
newC(c1).f(x)===newC(c2).f(x);//closelyrelated
max(a,b)===max(b,a));//commutativity
zoom(zoom(img,n),-n)===img);//invertibility
Bonus crazieness
What about higher-order functions?
“If we are to check properties involving
function valued variables, then we must be
able to generate arbitrary functions. Rather
surprisingly, we are able to do so.”
p.assert(p.forall(p.array(),p.fn(),p.value(),function(a,f,c){
return_.map(a,f,c).length===a.length;
}));
Conclusion
finding good properties makes you think harder
very functional style, but should work on testing OO code
not a replacement for TDD using examples
but can be used to help finding missed edge cases
best for unit testing (because of high number of test cases)
also good for verifying assumptions on 3dparty code

Weitere ähnliche Inhalte

Andere mochten auch

#SMEcom Presentation
#SMEcom Presentation#SMEcom Presentation
#SMEcom PresentationAlyson Nagle
 
Ethnography presentation
Ethnography presentationEthnography presentation
Ethnography presentationrobbyrohan44
 
2: How Does Your Media Product Represent Particular Social Group?
2: How Does Your Media Product Represent Particular Social Group?2: How Does Your Media Product Represent Particular Social Group?
2: How Does Your Media Product Represent Particular Social Group?06wl1
 
Neoaug 2013 critical success factors for data quality management-chain-sys-co...
Neoaug 2013 critical success factors for data quality management-chain-sys-co...Neoaug 2013 critical success factors for data quality management-chain-sys-co...
Neoaug 2013 critical success factors for data quality management-chain-sys-co...Chain Sys Corporation
 
Impact of dividend on the stock price –
Impact of dividend on the stock price –Impact of dividend on the stock price –
Impact of dividend on the stock price –Asit Gope
 
Target audience analysis
Target audience analysisTarget audience analysis
Target audience analysis06wl1
 

Andere mochten auch (13)

Welcome to pamplona
Welcome to pamplonaWelcome to pamplona
Welcome to pamplona
 
#SMEcom Presentation
#SMEcom Presentation#SMEcom Presentation
#SMEcom Presentation
 
Presentation
PresentationPresentation
Presentation
 
Amor real power point
Amor real power pointAmor real power point
Amor real power point
 
Ethnography presentation
Ethnography presentationEthnography presentation
Ethnography presentation
 
LQT3 Final Presentation
LQT3 Final PresentationLQT3 Final Presentation
LQT3 Final Presentation
 
5D-impact
5D-impact5D-impact
5D-impact
 
2: How Does Your Media Product Represent Particular Social Group?
2: How Does Your Media Product Represent Particular Social Group?2: How Does Your Media Product Represent Particular Social Group?
2: How Does Your Media Product Represent Particular Social Group?
 
Neoaug 2013 critical success factors for data quality management-chain-sys-co...
Neoaug 2013 critical success factors for data quality management-chain-sys-co...Neoaug 2013 critical success factors for data quality management-chain-sys-co...
Neoaug 2013 critical success factors for data quality management-chain-sys-co...
 
LQT3 Midterm Slides
LQT3 Midterm SlidesLQT3 Midterm Slides
LQT3 Midterm Slides
 
Impact of dividend on the stock price –
Impact of dividend on the stock price –Impact of dividend on the stock price –
Impact of dividend on the stock price –
 
Hackers
HackersHackers
Hackers
 
Target audience analysis
Target audience analysisTarget audience analysis
Target audience analysis
 

Ähnlich wie property-based testing (FrOsCon 9, 2014, August 23)

JSregularExpressions.pptx
JSregularExpressions.pptxJSregularExpressions.pptx
JSregularExpressions.pptxMattMarino13
 
Developer Test - Things to Know
Developer Test - Things to KnowDeveloper Test - Things to Know
Developer Test - Things to Knowvilniusjug
 
Beyond xUnit example-based testing: property-based testing with ScalaCheck
Beyond xUnit example-based testing: property-based testing with ScalaCheckBeyond xUnit example-based testing: property-based testing with ScalaCheck
Beyond xUnit example-based testing: property-based testing with ScalaCheckFranklin Chen
 
Aaron Bedra - Effective Software Security Teams
Aaron Bedra - Effective Software Security TeamsAaron Bedra - Effective Software Security Teams
Aaron Bedra - Effective Software Security Teamscentralohioissa
 
White Box Testing (Introduction to)
White Box Testing (Introduction to)White Box Testing (Introduction to)
White Box Testing (Introduction to)Henry Muccini
 
2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good Tests2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good TestsTomek Kaczanowski
 
Rotten Green Tests
Rotten Green TestsRotten Green Tests
Rotten Green TestsESUG
 
An Introduction to Property Based Testing
An Introduction to Property Based TestingAn Introduction to Property Based Testing
An Introduction to Property Based TestingC4Media
 
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
 
Mutation Testing: Testing your tests
Mutation Testing: Testing your testsMutation Testing: Testing your tests
Mutation Testing: Testing your testsStephen Leigh
 
Unit testing patterns for concurrent code
Unit testing patterns for concurrent codeUnit testing patterns for concurrent code
Unit testing patterns for concurrent codeDror Helper
 
Advances in Unit Testing: Theory and Practice
Advances in Unit Testing: Theory and PracticeAdvances in Unit Testing: Theory and Practice
Advances in Unit Testing: Theory and PracticeTao Xie
 
PVS-Studio vs Chromium
PVS-Studio vs ChromiumPVS-Studio vs Chromium
PVS-Studio vs ChromiumPVS-Studio
 
Debug - MITX60012016-V005100
Debug - MITX60012016-V005100Debug - MITX60012016-V005100
Debug - MITX60012016-V005100Ha Nguyen
 
What Do the Asserts in a Unit Test Tell Us About Code Quality? (CSMR2013)
What Do the Asserts in a Unit Test Tell Us About Code Quality? (CSMR2013)What Do the Asserts in a Unit Test Tell Us About Code Quality? (CSMR2013)
What Do the Asserts in a Unit Test Tell Us About Code Quality? (CSMR2013)Maurício Aniche
 
The Art of Identifying Vulnerabilities - CascadiaFest 2015
The Art of Identifying Vulnerabilities  - CascadiaFest 2015The Art of Identifying Vulnerabilities  - CascadiaFest 2015
The Art of Identifying Vulnerabilities - CascadiaFest 2015Adam Baldwin
 
Ruslan Shevchenko - Property based testing
Ruslan Shevchenko - Property based testingRuslan Shevchenko - Property based testing
Ruslan Shevchenko - Property based testingIevgenii Katsan
 

Ähnlich wie property-based testing (FrOsCon 9, 2014, August 23) (20)

JSregularExpressions.pptx
JSregularExpressions.pptxJSregularExpressions.pptx
JSregularExpressions.pptx
 
Developer Test - Things to Know
Developer Test - Things to KnowDeveloper Test - Things to Know
Developer Test - Things to Know
 
Beyond xUnit example-based testing: property-based testing with ScalaCheck
Beyond xUnit example-based testing: property-based testing with ScalaCheckBeyond xUnit example-based testing: property-based testing with ScalaCheck
Beyond xUnit example-based testing: property-based testing with ScalaCheck
 
Aaron Bedra - Effective Software Security Teams
Aaron Bedra - Effective Software Security TeamsAaron Bedra - Effective Software Security Teams
Aaron Bedra - Effective Software Security Teams
 
White Box Testing (Introduction to)
White Box Testing (Introduction to)White Box Testing (Introduction to)
White Box Testing (Introduction to)
 
2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good Tests2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good Tests
 
Rotten Green Tests
Rotten Green TestsRotten Green Tests
Rotten Green Tests
 
ppopoff
ppopoffppopoff
ppopoff
 
An Introduction to Property Based Testing
An Introduction to Property Based TestingAn Introduction to Property Based Testing
An Introduction to Property Based Testing
 
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
 
Mutation Testing: Testing your tests
Mutation Testing: Testing your testsMutation Testing: Testing your tests
Mutation Testing: Testing your tests
 
Unit testing patterns for concurrent code
Unit testing patterns for concurrent codeUnit testing patterns for concurrent code
Unit testing patterns for concurrent code
 
Advances in Unit Testing: Theory and Practice
Advances in Unit Testing: Theory and PracticeAdvances in Unit Testing: Theory and Practice
Advances in Unit Testing: Theory and Practice
 
PVS-Studio vs Chromium
PVS-Studio vs ChromiumPVS-Studio vs Chromium
PVS-Studio vs Chromium
 
Debug - MITX60012016-V005100
Debug - MITX60012016-V005100Debug - MITX60012016-V005100
Debug - MITX60012016-V005100
 
What Do the Asserts in a Unit Test Tell Us About Code Quality? (CSMR2013)
What Do the Asserts in a Unit Test Tell Us About Code Quality? (CSMR2013)What Do the Asserts in a Unit Test Tell Us About Code Quality? (CSMR2013)
What Do the Asserts in a Unit Test Tell Us About Code Quality? (CSMR2013)
 
The Art of Identifying Vulnerabilities - CascadiaFest 2015
The Art of Identifying Vulnerabilities  - CascadiaFest 2015The Art of Identifying Vulnerabilities  - CascadiaFest 2015
The Art of Identifying Vulnerabilities - CascadiaFest 2015
 
Ruslan Shevchenko - Property based testing
Ruslan Shevchenko - Property based testingRuslan Shevchenko - Property based testing
Ruslan Shevchenko - Property based testing
 
130706266060138191
130706266060138191130706266060138191
130706266060138191
 
R Basics
R BasicsR Basics
R Basics
 

Kürzlich hochgeladen

Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdfPearlKirahMaeRagusta1
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfproinshot.com
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park masabamasaba
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is insideshinachiaurasa2
 
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
 
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verifiedSector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verifiedDelhi Call girls
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnAmarnathKambale
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesVictorSzoltysek
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
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
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 
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
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfVishalKumarJha10
 
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptxBUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptxalwaysnagaraju26
 
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...kalichargn70th171
 
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
 
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
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park masabamasaba
 

Kürzlich hochgeladen (20)

Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdf
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
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 🔝✔️✔️
 
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verifiedSector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
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...
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
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
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
 
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptxBUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
 
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
 
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 🔝✔️✔️
 
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...
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 

property-based testing (FrOsCon 9, 2014, August 23)

  • 2. [horribly predictable quote] “Testing shows the presence, not the absence of bugs.” -- Edsger W. Dijkstra
  • 3. Hoare: “ The role of testing, in theory, is to establish the base propositions of an inductive proof.[...] At present, this is mainly theory [but] seems to show a possibility of practical results, though proving correctness is a laborious and expensive process.” Perlis: “ Much of program complexity is spurious and a number of test cases properly studied will exhaust the testing problem. The problem is to isolate the right test cases, not to prove the algorithm, for that follows after the choice of the proper test cases.” Diijkstra: “Testing shows the presence, not the absence of bugs”
  • 4. Testing with examples specify correct output for chosen inputs relatively easy to come up with good communication tool straightforward to implement straightforward to overlook important ones usually only cover a very small portion of your domain
  • 5. yay, all tests passing! //obviously expect(Math.abs(0)).to.equal(0); //negativevaluesbecomepositive expect(Math.abs(-1)).to.equal(1); expect(Math.abs(-5)).to.equal(5); //whilepositivevaluesstayspositive expect(Math.abs(1)).to.equal(1); //shouldalsoworkwithfloatingpointnumbers expect(Math.abs(-1.23)).to.equal(1.23); expect(Math.abs(1.23)).to.equal(1.23); //oh,andjavascriptknowsaboutnegative0 expect(Math.abs(-0)).to.equal(0);
  • 6. Problem -- ECMAScript® Language Specification “The Number type has exactly 18437736874454810627 (that is, 264−253+3) values” -- me “The box above has 90000 pixels”
  • 7. remember when you started learning TDD? Math.abs=function(x){ if(x===0)return0; if(x===-1||x===1)return1; if((x>1&&x<2)||(x>-2&&x<-1))return1.23; return5; }
  • 8. Testing with properties not to be confused with properties on JavaScript objects specify a property that holds for all / specified inputs instead of finding a proof, test random inputs similar to contracts examples use jsverify, other implementations available
  • 9. Describing a property Humans: “whatever number we pass to abs, it should return a number greater or equal than 0.” Mathematicians: ∀ x ∈ ℝ: abs(x) ≥ 0 Programmers: varp=require('jsverify'); p.check(p.forall(p.number(),function(x){ varactual=Math.abs(x); returntypeofactual==='number'&&actual>=0; })); OK,passed100tests
  • 11. ∀ x ∈ ℝ: abs(x) ≥ x p.check(p.forall(p.number(),function(x){ returnMath.abs(x)>=x; })); Error:Failedafter3testsand0shrinks.rngState:8c0a43ef85c761db92;Coun terexample:1.5644732909277081;
  • 12. More, um, well, examples
  • 13. Defining the domain using filters varslowSum=function(n){ varsum=0,i; for(i=1;i<=n;i++){sum+=i;} returnsum; }, gaussSum=function(n){return(n/2)*(n+1);}, positiveNumber=p.suchthat(p.integer(),function(i){returni>0;}); p.assert(p.forall(positiveNumber,function(i){ returnslowSum(i)===gaussSum(i); }));
  • 14. Defining the domain using custom generators oddNumber=function(){ return{ arbitrary:function(r){ varn=p.integer().arbitrary(r); return2*n+1; }, shrink:shrink.noop, show:show.def }; }; describe('odd',function(){ //varodd=function(n){returnn%2===1;}; varodd=function(n){returnMath.abs(n)%2===1;}; it('returnstrueforalloddnumbers',function(){ p.assert(p.forall(oddNumber(),odd)); });
  • 15. idempotence: ∀ x: f(x) === f(f(x)) Reusable properties varis_idempotent=function(generator,fn){ returnp.forall(generator,function(x){ return_.isEqual(fn(fn(s)),fn(s)); }); }; p.assert(is_idempotent(p.array(),_.compact)); p.assert(is_idempotent(p.array(),_.uniq));
  • 17. Common use cases f(x)>=y //assertingthefunction'srange f(x)===f(f(x));//idempotence a(x)===b(x);//regressiontestforreimplementation newC1().f(x)===newC2().f(x);//closelyrelated newC(c1).f(x)===newC(c2).f(x);//closelyrelated max(a,b)===max(b,a));//commutativity zoom(zoom(img,n),-n)===img);//invertibility
  • 18. Bonus crazieness What about higher-order functions? “If we are to check properties involving function valued variables, then we must be able to generate arbitrary functions. Rather surprisingly, we are able to do so.” p.assert(p.forall(p.array(),p.fn(),p.value(),function(a,f,c){ return_.map(a,f,c).length===a.length; }));
  • 19. Conclusion finding good properties makes you think harder very functional style, but should work on testing OO code not a replacement for TDD using examples but can be used to help finding missed edge cases best for unit testing (because of high number of test cases) also good for verifying assumptions on 3dparty code