SlideShare uma empresa Scribd logo
1 de 56
Baixar para ler offline
Mutants to the rescue:
How effective are your
unit tests?
@DevPaco
Paco van Beckhoven
- Software engineer
- Automated warehouse team @ Picnic
About me
@DevPaco
“Quality”
@DevPaco
“The degree to which a system, component, or process meets
specified requirements” – IEEE
Software quality
@DevPaco
ISO/IEC 25010
@DevPaco
ISO/IEC 25010
@DevPaco
@DevPaco
Unit test example
public class Calculator {
public int multiply(int a, int b) {
return a * b;
}
}
@DevPaco
Unit test example
public class Calculator {
public int multiply(int a, int b) {
return a * b;
}
}
class CalculatorTest {
@Test
void testMultiply() {
assertEquals(20, new Calculator().multiply(4, 5));
}
@Test
void testMultiplyWithZero() {
assertEquals(0, new Calculator().multiply(0, 5));
}
}
@DevPaco
Maintenance
@DevPaco
Documentation
@DevPaco
Quick feedback
https://www.rainerhahnekamp.com/en/tag/agile/
@DevPaco
“ Always code as if the guy who ends up maintaining
your code will be a violent psychopath who knows
where you live ”
John F. Woods
@DevPaco
@DevPaco
@DevPaco
• Projects evolve, grow
• Tests evolve, sometimes missed when refactoring
• Test code often not monitored
The problem
@DevPaco
Code coverage
“The degree to which the source code of a program is
executed when a particular test suite runs”
Monitoring Tests
@DevPaco
Example
public Result submit(Proposal proposal, int openProposals, Instant deadline) {
if (openProposals >= 3 || Instant.now().isAfter(deadline)) {
notificationService.notifySubmitFailed(proposal.getUser());
return new Error("Not allowed to submit");
}
// Doing the actual submit
return new Success();
}
@DevPaco
Method coverage
public Result submit(Proposal proposal, int openProposals, Instant deadline) {
if (openProposals >= 3 || Instant.now().isAfter(deadline)) {
notificationService.notifySubmitFailed(proposal.getUser());
return new Error("Not allowed to submit");
}
// Doing the actual submit
return new Success();
}
@DevPaco
Method coverage
public Result submit(Proposal proposal, int openProposals, Instant deadline) {
if (openProposals >= 3 || Instant.now().isAfter(deadline)) {
notificationService.notifySubmitFailed(proposal.getUser());
return new Error("Not allowed to submit");
}
// Doing the actual submit
return new Success();
}
TESTS:
testX: submit(proposal, 0, Instant.now().plusSeconds(999));
@DevPaco
Statement coverage
public Result submit(Proposal proposal, int openProposals, Instant deadline) {
if (openProposals >= 3 || Instant.now().isAfter(deadline)) {
notificationService.notifySubmitFailed(proposal.getUser());
return new Error("Not allowed to submit");
}
// Doing the actual submit
return new Success();
}
TESTS:
testX: submit(proposal, 0, Instant.now().plusSeconds(999));
@DevPaco
Statement coverage
public Result submit(Proposal proposal, int openProposals, Instant deadline) {
if (openProposals >= 3 || Instant.now().isAfter(deadline)) {
notificationService.notifySubmitFailed(proposal.getUser());
return new Error("Not allowed to submit");
}
// Doing the actual submit
return new Success();
}
TESTS:
testX: submit(proposal, 0, Instant.now().plusSeconds(999));
testY: submit(proposal, 5, Instant.now().plusSeconds(999));
@DevPaco
Condition coverage
TESTS:
testX: submit(proposal, 0, Instant.now().plusSeconds(999));
testY: submit(proposal, 5, Instant.now().plusSeconds(999));
public Result submit(Proposal proposal, int openProposals, Instant deadline) {
if (openProposals >= 3 || Instant.now().isAfter(deadline)) {
notificationService.notifySubmitFailed(proposal.getUser());
return new Error("Not allowed to submit");
}
// Doing the actual submit
return new Success();
}
@DevPaco
Condition coverage
TESTS:
testX: submit(proposal, 0, Instant.now().plusSeconds(999));
testY: submit(proposal, 5, Instant.now().plusSeconds(999));
testZ: submit(proposal, 0, Instant.now().minusSeconds(999));
public Result submit(Proposal proposal, int openProposals, Instant deadline) {
if (openProposals >= 3 || Instant.now().isAfter(deadline)) {
notificationService.notifySubmitFailed(proposal.getUser());
return new Error("Not allowed to submit");
}
// Doing the actual submit
return new Success();
}
@DevPaco
Pros:
• Helps you write more/better tests
• Easy/cheap to measure
• Shows what you didn’t test
• Shows that what you did test, didn’t crash
Code coverage
@DevPaco
But:
• Can be misleading
• Doesn’t give any guarantees
Code coverage
@DevPaco
But:
• Can be misleading
• Doesn’t give any guarantees
Code coverage
@Test
public void shouldReturnSuccessOnValidSubmit() {
proposalService.submit(randomProposal(), 0, tomorrow());
}
@DevPaco
• Testing the tests
• “Bug detection ability”
Test effectiveness
https://shirt.woot.com/offers/bug-hunt
@DevPaco
• Simulating bugs
• Typical programming errors
Mutation testing
@DevPaco
Step 1. Generating mutants
MUTATOR
Production code The mutant
Altered version of the
production code
@DevPaco
Example
boolean isAllowedToApplyForDriversLicense(int age) {
return age < 17;
}
boolean isAllowedToApplyForDriversLicense(int age) {
return age >= 17;
}
@DevPaco
Example
return age < 17; // negate conditional
return age > 17; // change conditional boundaries
return true; // true returns
return false; // false returns
boolean isAllowedToApplyForDriversLicense(int age) {
return age >= 17;
}
@DevPaco
Math mutators
int getTotalNumberOfLayers() {
return layers.size() + numberOfAdditionalLayers;
}
int getTotalNumberOfLayers() {
return layers.size() - numberOfAdditionalLayers;
}
@DevPaco
Removing void statements
MetaData createMetaData(Instant eventTimestamp) {
MetaData meta = new MetaData();
meta.setPublished(eventTimestamp);
return meta;
}
MetaData createMetaData(Instant eventTimestamp) {
MetaData meta = new MetaData();
return meta;
}
@DevPaco
Step 2. Mutation analysis
❌ Mutant survived
✅ Mutant killed
+ Tests
@DevPaco
The first generation mutation tools (for Java)
- Jester
- Jumble
- MuJava
Mutation tools
@DevPaco
• Created by Henry Coles in 2011
• Byte code mutation
• Optimized for coverage
• Easy to use
• Actively maintained/supported
PIT / Pitest
@DevPaco
1. Add the plugin
<plugin>
<groupId>org.pitest</groupId>
<artifactId>pitest-maven</artifactId>
<version>1.7.5</version>
</plugin>
2. Run pitest
> mvn clean verify pitest:mutationCoverage
Getting started
@DevPaco
Demo
@DevPaco
Reality
@DevPaco
• Limit number of mutants
<targetClasses>
<param>org.jfree.chart.util.*</param>
</targetClasses>
• Limit number of executed tests
<targetTests>
<param>org.jfree.chart.util.*</param>
</targetTests>
• Increase Threads
<threads>8</threads>
Start small!
@DevPaco
Tweaking performance
@DevPaco
• Reduce number of tests to run
<configuration>
<excludedTestClasses>foo.bar.acceptance.*</excludedTestClasses>
<excludedTestClasses>foo.bar.database.*</excludedTestClasses>
<excludedTestClasses>foo.bar.slooow.*</excludedTestClasses>
</configuration>
Tweaking performance
@DevPaco
• Exclude code from mutation
<configuration>
<excludedClasses>foo.bar.generated.*</excludedClasses>
<excludedClasses>*pdfgenerator.*</excludedClasses>
<excludedClasses>foo.bar.database.*</excludedClasses>
<excludedMethods>*ThreadManager.run*</excludedMethods>
</configuration>
Tweaking performance
@DevPaco
• Incremental analysis
• Experimental feature
• Reuse results from last run
Tweaking performance
@DevPaco
Keeping your reports clean
<configuration>
<avoidCallsTo>
<avoidCallsTo>java.util.logging</avoidCallsTo>
<avoidCallsTo>org.apache.log4j</avoidCallsTo>
<avoidCallsTo>org.slf4j</avoidCallsTo>
<avoidCallsTo>org.apache.commons.logging</avoidCallsTo>
</avoidCallsTo>
</configuration>
log.info(”Execution took {} seconds", end – start);
Tweaking performance
@DevPaco
Tweaking Mutators
@DevPaco
Tweaking Mutators
@DevPaco
Tweaking Mutators
@DevPaco
Equivalent mutants
MUTATOR
Replace == with >=
int i = 0;
while (true) {
i++;
if (i == MAX) {
break;
}
}
int i = 0;
while (true) {
i++;
if (i >= MAX) {
break;
}
}
@DevPaco
• Add the maven goal to your build step
mvn org.pitest:pitest-maven:mutationCoverage
• Plugins available for Jenkins/Sonar
• Pitest can be configured to break the pipeline
Adding mutation testing to CI
@DevPaco
When to consider mutation testing?
@DevPaco
Are there lives at stake?
Are you building software for a rocket?
Is it a self driving car?
When to consider mutation testing?
@DevPaco
Are you using code coverage?
What is the cost of fixing a bug?
Does the team think it’s a good idea?
When to consider mutation testing?
@DevPaco
• Code coverage: “How much of the code is tested”
• Mutation testing: “How proper is the code tested”
• Start small
• Tweak for performance!
Summary
@DevPaco
Thank you!
https://pitest.org/
Mutation testing for Java, Kotlin(paid)
https://stryker-mutator.io/
Supports JavaScript (typescript), C#, Scala
https://github.com/boxed/mutmut
Mutation testing tool for Python
@DevPaco
For questions, feedback, suggestions

Mais conteúdo relacionado

Semelhante a Mutation testing Bucharest Tech Week

Advanced Java Testing @ POSS 2019
Advanced Java Testing @ POSS 2019Advanced Java Testing @ POSS 2019
Advanced Java Testing @ POSS 2019Vincent Massol
 
Testing basics for developers
Testing basics for developersTesting basics for developers
Testing basics for developersAnton Udovychenko
 
JCD 2013 OCM Java Developer
JCD 2013 OCM Java DeveloperJCD 2013 OCM Java Developer
JCD 2013 OCM Java Developer益裕 張
 
OCM Java 開發人員認證與設計模式
OCM Java 開發人員認證與設計模式OCM Java 開發人員認證與設計模式
OCM Java 開發人員認證與設計模式CodeData
 
Php Unit With Zend Framework Zendcon09
Php Unit With Zend Framework   Zendcon09Php Unit With Zend Framework   Zendcon09
Php Unit With Zend Framework Zendcon09Michelangelo van Dam
 
We Are All Testers Now: The Testing Pyramid and Front-End Development
We Are All Testers Now: The Testing Pyramid and Front-End DevelopmentWe Are All Testers Now: The Testing Pyramid and Front-End Development
We Are All Testers Now: The Testing Pyramid and Front-End DevelopmentAll Things Open
 
比XML更好用的Java Annotation
比XML更好用的Java Annotation比XML更好用的Java Annotation
比XML更好用的Java Annotationjavatwo2011
 
Quality of life through Unit Testing
Quality of life through Unit TestingQuality of life through Unit Testing
Quality of life through Unit TestingSian Lerk Lau
 
Testing with VS2010 - A Bugs Life
Testing with VS2010 - A Bugs LifeTesting with VS2010 - A Bugs Life
Testing with VS2010 - A Bugs LifePeter Gfader
 
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
 
Junit mockito and PowerMock in Java
Junit mockito and  PowerMock in JavaJunit mockito and  PowerMock in Java
Junit mockito and PowerMock in JavaAnkur Maheshwari
 
Eclipse MicroProfile: Accelerating the adoption of Java Microservices
Eclipse MicroProfile: Accelerating the adoption of Java MicroservicesEclipse MicroProfile: Accelerating the adoption of Java Microservices
Eclipse MicroProfile: Accelerating the adoption of Java MicroservicesDev_Events
 
Automated Developer Testing: Achievements and Challenges
Automated Developer Testing: Achievements and ChallengesAutomated Developer Testing: Achievements and Challenges
Automated Developer Testing: Achievements and ChallengesTao Xie
 
Desenvolva plugins para o compilador do Java 8
Desenvolva plugins para o compilador do Java 8Desenvolva plugins para o compilador do Java 8
Desenvolva plugins para o compilador do Java 8Marcelo de Castro
 
Mutation Testing: Start Hunting The Bugs
Mutation Testing: Start Hunting The BugsMutation Testing: Start Hunting The Bugs
Mutation Testing: Start Hunting The BugsAri Waller
 

Semelhante a Mutation testing Bucharest Tech Week (20)

Advanced Java Testing @ POSS 2019
Advanced Java Testing @ POSS 2019Advanced Java Testing @ POSS 2019
Advanced Java Testing @ POSS 2019
 
Design for Testability
Design for TestabilityDesign for Testability
Design for Testability
 
Testing basics for developers
Testing basics for developersTesting basics for developers
Testing basics for developers
 
JCD 2013 OCM Java Developer
JCD 2013 OCM Java DeveloperJCD 2013 OCM Java Developer
JCD 2013 OCM Java Developer
 
OCM Java 開發人員認證與設計模式
OCM Java 開發人員認證與設計模式OCM Java 開發人員認證與設計模式
OCM Java 開發人員認證與設計模式
 
Php Unit With Zend Framework Zendcon09
Php Unit With Zend Framework   Zendcon09Php Unit With Zend Framework   Zendcon09
Php Unit With Zend Framework Zendcon09
 
We Are All Testers Now: The Testing Pyramid and Front-End Development
We Are All Testers Now: The Testing Pyramid and Front-End DevelopmentWe Are All Testers Now: The Testing Pyramid and Front-End Development
We Are All Testers Now: The Testing Pyramid and Front-End Development
 
比XML更好用的Java Annotation
比XML更好用的Java Annotation比XML更好用的Java Annotation
比XML更好用的Java Annotation
 
Unit testing with JUnit
Unit testing with JUnitUnit testing with JUnit
Unit testing with JUnit
 
Agile mobile
Agile mobileAgile mobile
Agile mobile
 
Quality of life through Unit Testing
Quality of life through Unit TestingQuality of life through Unit Testing
Quality of life through Unit Testing
 
Testing with VS2010 - A Bugs Life
Testing with VS2010 - A Bugs LifeTesting with VS2010 - A Bugs Life
Testing with VS2010 - A Bugs Life
 
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
 
Test driven development
Test driven developmentTest driven development
Test driven development
 
Junit mockito and PowerMock in Java
Junit mockito and  PowerMock in JavaJunit mockito and  PowerMock in Java
Junit mockito and PowerMock in Java
 
Eclipse MicroProfile: Accelerating the adoption of Java Microservices
Eclipse MicroProfile: Accelerating the adoption of Java MicroservicesEclipse MicroProfile: Accelerating the adoption of Java Microservices
Eclipse MicroProfile: Accelerating the adoption of Java Microservices
 
Automated Developer Testing: Achievements and Challenges
Automated Developer Testing: Achievements and ChallengesAutomated Developer Testing: Achievements and Challenges
Automated Developer Testing: Achievements and Challenges
 
Desenvolva plugins para o compilador do Java 8
Desenvolva plugins para o compilador do Java 8Desenvolva plugins para o compilador do Java 8
Desenvolva plugins para o compilador do Java 8
 
Python testing
Python  testingPython  testing
Python testing
 
Mutation Testing: Start Hunting The Bugs
Mutation Testing: Start Hunting The BugsMutation Testing: Start Hunting The Bugs
Mutation Testing: Start Hunting The Bugs
 

Último

Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
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
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...masabamasaba
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfonteinmasabamasaba
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionOnePlan Solutions
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Hararemasabamasaba
 
%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
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech studentsHimanshiGarg82
 
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
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareJim McKeeth
 
Generic or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisionsGeneric or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisionsBert Jan Schrijver
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...Shane Coughlan
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfonteinmasabamasaba
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
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
 
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburgmasabamasaba
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...masabamasaba
 
%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
 
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
 
%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durbanmasabamasaba
 

Último (20)

Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
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
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare
 
%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
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
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
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
Generic or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisionsGeneric or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisions
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
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
 
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
%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
 
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
 
%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban
 

Mutation testing Bucharest Tech Week

  • 1. Mutants to the rescue: How effective are your unit tests?
  • 2. @DevPaco Paco van Beckhoven - Software engineer - Automated warehouse team @ Picnic About me
  • 4. @DevPaco “The degree to which a system, component, or process meets specified requirements” – IEEE Software quality
  • 8. @DevPaco Unit test example public class Calculator { public int multiply(int a, int b) { return a * b; } }
  • 9. @DevPaco Unit test example public class Calculator { public int multiply(int a, int b) { return a * b; } } class CalculatorTest { @Test void testMultiply() { assertEquals(20, new Calculator().multiply(4, 5)); } @Test void testMultiplyWithZero() { assertEquals(0, new Calculator().multiply(0, 5)); } }
  • 13. @DevPaco “ Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live ” John F. Woods
  • 16. @DevPaco • Projects evolve, grow • Tests evolve, sometimes missed when refactoring • Test code often not monitored The problem
  • 17. @DevPaco Code coverage “The degree to which the source code of a program is executed when a particular test suite runs” Monitoring Tests
  • 18. @DevPaco Example public Result submit(Proposal proposal, int openProposals, Instant deadline) { if (openProposals >= 3 || Instant.now().isAfter(deadline)) { notificationService.notifySubmitFailed(proposal.getUser()); return new Error("Not allowed to submit"); } // Doing the actual submit return new Success(); }
  • 19. @DevPaco Method coverage public Result submit(Proposal proposal, int openProposals, Instant deadline) { if (openProposals >= 3 || Instant.now().isAfter(deadline)) { notificationService.notifySubmitFailed(proposal.getUser()); return new Error("Not allowed to submit"); } // Doing the actual submit return new Success(); }
  • 20. @DevPaco Method coverage public Result submit(Proposal proposal, int openProposals, Instant deadline) { if (openProposals >= 3 || Instant.now().isAfter(deadline)) { notificationService.notifySubmitFailed(proposal.getUser()); return new Error("Not allowed to submit"); } // Doing the actual submit return new Success(); } TESTS: testX: submit(proposal, 0, Instant.now().plusSeconds(999));
  • 21. @DevPaco Statement coverage public Result submit(Proposal proposal, int openProposals, Instant deadline) { if (openProposals >= 3 || Instant.now().isAfter(deadline)) { notificationService.notifySubmitFailed(proposal.getUser()); return new Error("Not allowed to submit"); } // Doing the actual submit return new Success(); } TESTS: testX: submit(proposal, 0, Instant.now().plusSeconds(999));
  • 22. @DevPaco Statement coverage public Result submit(Proposal proposal, int openProposals, Instant deadline) { if (openProposals >= 3 || Instant.now().isAfter(deadline)) { notificationService.notifySubmitFailed(proposal.getUser()); return new Error("Not allowed to submit"); } // Doing the actual submit return new Success(); } TESTS: testX: submit(proposal, 0, Instant.now().plusSeconds(999)); testY: submit(proposal, 5, Instant.now().plusSeconds(999));
  • 23. @DevPaco Condition coverage TESTS: testX: submit(proposal, 0, Instant.now().plusSeconds(999)); testY: submit(proposal, 5, Instant.now().plusSeconds(999)); public Result submit(Proposal proposal, int openProposals, Instant deadline) { if (openProposals >= 3 || Instant.now().isAfter(deadline)) { notificationService.notifySubmitFailed(proposal.getUser()); return new Error("Not allowed to submit"); } // Doing the actual submit return new Success(); }
  • 24. @DevPaco Condition coverage TESTS: testX: submit(proposal, 0, Instant.now().plusSeconds(999)); testY: submit(proposal, 5, Instant.now().plusSeconds(999)); testZ: submit(proposal, 0, Instant.now().minusSeconds(999)); public Result submit(Proposal proposal, int openProposals, Instant deadline) { if (openProposals >= 3 || Instant.now().isAfter(deadline)) { notificationService.notifySubmitFailed(proposal.getUser()); return new Error("Not allowed to submit"); } // Doing the actual submit return new Success(); }
  • 25. @DevPaco Pros: • Helps you write more/better tests • Easy/cheap to measure • Shows what you didn’t test • Shows that what you did test, didn’t crash Code coverage
  • 26. @DevPaco But: • Can be misleading • Doesn’t give any guarantees Code coverage
  • 27. @DevPaco But: • Can be misleading • Doesn’t give any guarantees Code coverage @Test public void shouldReturnSuccessOnValidSubmit() { proposalService.submit(randomProposal(), 0, tomorrow()); }
  • 28. @DevPaco • Testing the tests • “Bug detection ability” Test effectiveness https://shirt.woot.com/offers/bug-hunt
  • 29. @DevPaco • Simulating bugs • Typical programming errors Mutation testing
  • 30. @DevPaco Step 1. Generating mutants MUTATOR Production code The mutant Altered version of the production code
  • 31. @DevPaco Example boolean isAllowedToApplyForDriversLicense(int age) { return age < 17; } boolean isAllowedToApplyForDriversLicense(int age) { return age >= 17; }
  • 32. @DevPaco Example return age < 17; // negate conditional return age > 17; // change conditional boundaries return true; // true returns return false; // false returns boolean isAllowedToApplyForDriversLicense(int age) { return age >= 17; }
  • 33. @DevPaco Math mutators int getTotalNumberOfLayers() { return layers.size() + numberOfAdditionalLayers; } int getTotalNumberOfLayers() { return layers.size() - numberOfAdditionalLayers; }
  • 34. @DevPaco Removing void statements MetaData createMetaData(Instant eventTimestamp) { MetaData meta = new MetaData(); meta.setPublished(eventTimestamp); return meta; } MetaData createMetaData(Instant eventTimestamp) { MetaData meta = new MetaData(); return meta; }
  • 35. @DevPaco Step 2. Mutation analysis ❌ Mutant survived ✅ Mutant killed + Tests
  • 36. @DevPaco The first generation mutation tools (for Java) - Jester - Jumble - MuJava Mutation tools
  • 37. @DevPaco • Created by Henry Coles in 2011 • Byte code mutation • Optimized for coverage • Easy to use • Actively maintained/supported PIT / Pitest
  • 38. @DevPaco 1. Add the plugin <plugin> <groupId>org.pitest</groupId> <artifactId>pitest-maven</artifactId> <version>1.7.5</version> </plugin> 2. Run pitest > mvn clean verify pitest:mutationCoverage Getting started
  • 41. @DevPaco • Limit number of mutants <targetClasses> <param>org.jfree.chart.util.*</param> </targetClasses> • Limit number of executed tests <targetTests> <param>org.jfree.chart.util.*</param> </targetTests> • Increase Threads <threads>8</threads> Start small!
  • 43. @DevPaco • Reduce number of tests to run <configuration> <excludedTestClasses>foo.bar.acceptance.*</excludedTestClasses> <excludedTestClasses>foo.bar.database.*</excludedTestClasses> <excludedTestClasses>foo.bar.slooow.*</excludedTestClasses> </configuration> Tweaking performance
  • 44. @DevPaco • Exclude code from mutation <configuration> <excludedClasses>foo.bar.generated.*</excludedClasses> <excludedClasses>*pdfgenerator.*</excludedClasses> <excludedClasses>foo.bar.database.*</excludedClasses> <excludedMethods>*ThreadManager.run*</excludedMethods> </configuration> Tweaking performance
  • 45. @DevPaco • Incremental analysis • Experimental feature • Reuse results from last run Tweaking performance
  • 46. @DevPaco Keeping your reports clean <configuration> <avoidCallsTo> <avoidCallsTo>java.util.logging</avoidCallsTo> <avoidCallsTo>org.apache.log4j</avoidCallsTo> <avoidCallsTo>org.slf4j</avoidCallsTo> <avoidCallsTo>org.apache.commons.logging</avoidCallsTo> </avoidCallsTo> </configuration> log.info(”Execution took {} seconds", end – start); Tweaking performance
  • 50. @DevPaco Equivalent mutants MUTATOR Replace == with >= int i = 0; while (true) { i++; if (i == MAX) { break; } } int i = 0; while (true) { i++; if (i >= MAX) { break; } }
  • 51. @DevPaco • Add the maven goal to your build step mvn org.pitest:pitest-maven:mutationCoverage • Plugins available for Jenkins/Sonar • Pitest can be configured to break the pipeline Adding mutation testing to CI
  • 52. @DevPaco When to consider mutation testing?
  • 53. @DevPaco Are there lives at stake? Are you building software for a rocket? Is it a self driving car? When to consider mutation testing?
  • 54. @DevPaco Are you using code coverage? What is the cost of fixing a bug? Does the team think it’s a good idea? When to consider mutation testing?
  • 55. @DevPaco • Code coverage: “How much of the code is tested” • Mutation testing: “How proper is the code tested” • Start small • Tweak for performance! Summary
  • 56. @DevPaco Thank you! https://pitest.org/ Mutation testing for Java, Kotlin(paid) https://stryker-mutator.io/ Supports JavaScript (typescript), C#, Scala https://github.com/boxed/mutmut Mutation testing tool for Python @DevPaco For questions, feedback, suggestions