SlideShare a Scribd company logo
Des mutants
pour sauver
votre code
SPOILER Beaucoup de références
ciné + BD
Professor X: For someone who hates mutants... you certainly keep some strange company.
William Stryker: Oh, they serve their purpose... as long as they can be controlled.
Guillaume
Saint Etienne
“the risk of bugs is proportional to the
number of possible implementations for
your program”
https://livebook.manning.com/book/the-joy-of-kotlin/b-property-based-testing-in-kotlin/v-8/14
https://medium.com/@Cyrdup/unit-testing-youre-doing-it-wrong-407a07692989
Who can you trust?
��
��
��
Special K
Special K
🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈
des mutants dans le code.pdf
Tester les tests
Changer pour vérifier
Tests: States vs Behaviour
Change Behaviour => Test should fail
Attention aux doublures de tests
des mutants dans le code.pdf
Qui peut muter ?
Tout le monde ?
Le code :
● Il doit être résistant
● Toute mutation doit être détectée
Les tests
● Ils doivent être fiables
● Les paramètres ?-> Tests paramètrés
La configuration.?
Comment ça marche?
• Start with all tests passing
Lâchez les fauves!
● Introduce changes in the code and observe the behavior of the unit tests
• Two possible outcomes:
• Some of the unit tests will start failing : good
• All the tests will keep on passing : bad
It means that the unit tests do not really validate outcomes of the code under test
des mutants dans le code.pdf
Exemple pour la CLR de .Net // IL ≅ ByteCode, DLL ≅ JAR
des mutants dans le code.pdf
3 types
1. Statement Mutation – developer cut and pastes a part of a code of which
the outcome may be a removal of some lines
2. Value Mutation– values of primary parameters/variables are modified
3. Decision Mutation– control statements are to be changed
Différents types de mutation
● Arithmetic operators
● Equality operators
Différents types de mutation
● Logical operators
● Negation operators
The mutation score is defined as the percentage of killed mutants with the total number of
mutants.
● Mutation Score = (Killed Mutants / Total number of Mutants) * 100
Test cases are mutation adequate if the score is 100%.
Experimental results have shown that mutation testing is an effective approach for measuring the
adequacy of the test cases.
But, the main drawback is that the high cost of generating the mutants and executing each test
case against that mutant program.
https://www.guru99.com/mutation-testing.html
des mutants dans le code.pdf
A vous de jouer!
https://pitest.org/ https://stryker-mutator.io/
A vous de jouer!
C# (Stryker.Net)
● 3 branches (complexité croissante)
https://github.com/guillaumeagile/CSharp_mutation_Kata
JAVA (PiTest)
● problème des Mocks
https://github.com/guillaumeagile/mutate-test-kata/tree/onlyMockPb
● comment trouver le bon nombre de tests (nécessaires et suffisants) ?
https://github.com/guillaumeagile/mutate_test_kata_java_2
Comment soigner une mutation (facilement) ?
1. Pas (assez) de test
2. Assertions faibles
3. Cas aux limites
Freinez vos Minions
Mind the false positives
• Due to aggressive mutations:
• DEFAULTS should be conservative enough
• ALL is probably too aggressive
Smells in your Unit Tests 💩
● No assertions
● Irrelevant assertions
● Use of Mocks
● Expected results are calculated rather than explicitly specified • Test code
reuse (that is test logic reuse, test utilities are good)
● Test data reuse
● "Flickering" tests (tests with nondeterministic behavior) -> Threads 🚑
● Interdependencies between tests (e.g., execution order)
● Long running tests
● @Ignore‘d or commented out tests
HandsOn Lab
Mocks: Friends or foes?
Mocks: les ennemis des mutants
Test qui utilise un mock pour “simplifier/abstraire” FileSystem par exemple
Changement de comportement de FileSystem... sans changer les Mocks
Les Mutants n’ont rien vu!
Rappel : doublures de test
Article genèse: https://martinfowler.com/articles/mocksArentStubs.html
Article intense sur le sujet :
https://enterprisecraftsmanship.com/posts/when-to-mock/
Stub, Mock
Très bon article FR
https://agilitateur.azeau.com/post/2011/04/12/Pour-en-finir-avec-les-Stubs-et-les-Moc
ks
Behavioral testing with fakes
TIP: Testing the test doubles
Conseil avisé: https://tyrrrz.me/blog/fakes-over-mocks
ICalculate::Variation(prix, delta) : prix
Calcultator :: Variation(prix, delta) => prix * delta
Mock<Calcultator> . setup(prix, delta) => prix * delta
ModulateurDePrix(ICalculate calculator){
val delta : Int = 42
prixPlafonné(prix)
if (calculator.Variation(prix, delta) < prix )
prix = prix + calculator.Variation(prix, delta)
return prix
}
Test:::
sut = ModulateurDePrix(mockedCalc, ()-> true)
mockedCalc = Mock<Calcultator>() . setup(prix, delta) => prix * 0,2
val actual = sut.prixSelonLeTemps(10)
actual shouldBe 12
Test::: CalculateurQuantique
sut = CalculateurQuantique
actual = sut.Variation(10, 2)
actual shouldBe 2
Code:::
Calcultator :: Variation(prix, delta) => prix * delta
Test::: CalculateurQuantique
sut = CalculateurQuantique
actual = sut.Variation(10, 2)
actual shouldBe 2
Code:::
Calcultator :: Variation(prix, delta) => - (prix * delta)
MUTATION
Cas plus épineux
https://github.com/tpierrain/NFluent/blob/afda06c1bf761b598a0eaa32f4646510a56f6729/src/NFluent/Helpers/StringDifferen
ce.cs#L114
This method has such cyclomatic complexity as well as overlapping conditions that many mutants are
able to survive.
// original line 131
var boolSingleLine = actualLines.Length == 1 && expectedLines.Length == 1;
...
// mutant
var boolSingleLine = actualLines.Length == 1 || expectedLines.Length == 1;
actualLines et expectedLines ont *dans les faits* la même valeur, aucun test
ne peut échouer 😣
https://many-cores.com/2019/05/17/mutation-testing/
Refactoring needed
Ne cédez pas à la tentation de marquer un faux positif
the flow was objectively so complex that I could no longer understand, lest anticipate the impact of
any change (link to original code).
So I refactored it toward a simpler and cleaner design (new version here).
Conseils et exemples de @Cyrdup
Plein de samples
https://github.com/sanity/pairAdjacentViolators
https://github.com/odair-pedro/mutation-testing-csharp
https://stryker-mutator.io/docs/General/example
https://github.com/vmzakharov/mutate-test-kata
Bénéfices de la mutation
à vous de me dire...
Des mutants pour surveiller votre style
1. Petits Pas
2. No Excuses
3. Commencer par un test Rouge!..
4. ... et pour la bonne raison !!
5. Chicago School of Thought TDD (Inside Out/Bottom Up/Classic)
Hardcore TDD
The process should be:
1. Write the interface
2. Write the tests
3. Write the implementation and check if the tests pass.
4. Refactoring
So, the first thing you have to do before even starting coding (the tests) is think
about the problem in terms of the properties that should verify
when considering the input and the output of the function.
Property Based Testing = anti mutant weapon ?
Conseils & REX
Comment démarrer?
Mieux vaut partir de 0
Cibler le code
Uniquement votre code
Chassez les dépendances
● en excluant de la mutation
● par une architecture en onion/ hexagonale
Du code sans effet de bords
● transparence référentielle
● immutabilité
Découper le code pour cibler
https://www.kennethlange.com/functional-core-imperative-shell/
Attention aux tests
● Exécution parallèle
● Mutation testing is a computationally expensive process and can take quite some time
depending on the size of your codebase and the quality and speed of your test suite.
● Use filters to target only those packages or classes that are currently of interest
● Différence dans les tests = différence dans la résolution des mutations
○ Ex: tests have a hidden order dependency that is not revealed during the normal test run
● Some teams have very slow exhaustive tests or performance tests => EXCLUDE
● Throwing away tests is a hygienic move
Merci pour vos retours
https://roti.express/r/ag21-025

More Related Content

PDF
The Joy of Testing - Deep Dive @ Devoxx Belgium 2024
PPTX
presentation des google mock dans un contexte de tdd
PPTX
An Introduction to unit testing
PPTX
Kill the mutants and test your tests - Roy van Rijn
PDF
Kill the mutants - A better way to test your tests
PPTX
Building unit tests correctly with visual studio 2013
PDF
Mutation testing for a safer Future
PDF
Unit testing - A&BP CC
The Joy of Testing - Deep Dive @ Devoxx Belgium 2024
presentation des google mock dans un contexte de tdd
An Introduction to unit testing
Kill the mutants and test your tests - Roy van Rijn
Kill the mutants - A better way to test your tests
Building unit tests correctly with visual studio 2013
Mutation testing for a safer Future
Unit testing - A&BP CC

Similar to des mutants dans le code.pdf (20)

PPT
Mockito with a hint of PowerMock
PDF
Mutation Testing.pdf
PDF
Introduzione allo Unit Testing
PPT
Google mock training
PPT
Test Driven
PDF
Mutation Testing
PPTX
C++ Unit testing - the good, the bad & the ugly
PPTX
Building unit tests correctly
PPTX
Mutation-Testing mit PIT
PDF
Unit testing basic
PDF
TDD and Getting Paid
KEY
Testing w-mocks
PDF
Developer Tests - Things to Know (Vilnius JUG)
PDF
TDD python ne-april-2014
PPTX
Mocking with Mockito
PDF
How to complement TDD with static analysis
PDF
Shift-Left Testing: QA in a DevOps World by David Laulusa
PDF
Evaluating Model Testing and Model Checking for Finding Requirements Violatio...
PDF
DSR Testing (Part 1)
PDF
Bdd and-testing
Mockito with a hint of PowerMock
Mutation Testing.pdf
Introduzione allo Unit Testing
Google mock training
Test Driven
Mutation Testing
C++ Unit testing - the good, the bad & the ugly
Building unit tests correctly
Mutation-Testing mit PIT
Unit testing basic
TDD and Getting Paid
Testing w-mocks
Developer Tests - Things to Know (Vilnius JUG)
TDD python ne-april-2014
Mocking with Mockito
How to complement TDD with static analysis
Shift-Left Testing: QA in a DevOps World by David Laulusa
Evaluating Model Testing and Model Checking for Finding Requirements Violatio...
DSR Testing (Part 1)
Bdd and-testing
Ad

More from Guillaume Saint Etienne (20)

PPTX
Domain Driven Design expliqué aux product owners
PDF
Atelier Mob Programming_ tester dans l'hexagone, Adapters et Containers.pdf
PDF
Ecologie du Logiciel (Craft Luxembourg 2022).pdf
PPTX
musique electronique au cinéma.pptx
PDF
DDD FOR POs.pdf
PDF
Tout ce que vous avez voulu savoir sur les Doublures sans jamais oser le dema...
PDF
des algoritmes et des hommes (ethique et code).pdf
PDF
La crise Agile chez les Developpeurs (AGrenoble2019) (1).pdf
PDF
How we can BUILD.pdf
PPTX
_(V3.0) Aux sources de la simplicité Bordeaux 2022.pptx
PPTX
Il n’y a pas de bons développeurs.pptx
PPTX
Living Documentation (TDD, BDD).pptx
PPTX
Agile pour l'echafaud ATT2020.pptx
PPTX
10 ans de Code (Agile Bordeaux 2019).pptx
PPTX
Vendredi Tech_ la programmation fonctionnelle.pptx
PPTX
Feedback on DDD Europe - short -event storming.pptx
PDF
Crise agile chez les développeurs (frug agile 2020)
PPTX
My feedback on ddd europe
PDF
Electronic Music and Software Craftsmanship: analogue patterns.
Domain Driven Design expliqué aux product owners
Atelier Mob Programming_ tester dans l'hexagone, Adapters et Containers.pdf
Ecologie du Logiciel (Craft Luxembourg 2022).pdf
musique electronique au cinéma.pptx
DDD FOR POs.pdf
Tout ce que vous avez voulu savoir sur les Doublures sans jamais oser le dema...
des algoritmes et des hommes (ethique et code).pdf
La crise Agile chez les Developpeurs (AGrenoble2019) (1).pdf
How we can BUILD.pdf
_(V3.0) Aux sources de la simplicité Bordeaux 2022.pptx
Il n’y a pas de bons développeurs.pptx
Living Documentation (TDD, BDD).pptx
Agile pour l'echafaud ATT2020.pptx
10 ans de Code (Agile Bordeaux 2019).pptx
Vendredi Tech_ la programmation fonctionnelle.pptx
Feedback on DDD Europe - short -event storming.pptx
Crise agile chez les développeurs (frug agile 2020)
My feedback on ddd europe
Electronic Music and Software Craftsmanship: analogue patterns.
Ad

Recently uploaded (20)

PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
top salesforce developer skills in 2025.pdf
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
System and Network Administraation Chapter 3
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
Nekopoi APK 2025 free lastest update
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PPTX
history of c programming in notes for students .pptx
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PPTX
Transform Your Business with a Software ERP System
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PPTX
Operating system designcfffgfgggggggvggggggggg
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
top salesforce developer skills in 2025.pdf
Upgrade and Innovation Strategies for SAP ERP Customers
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
System and Network Administraation Chapter 3
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Nekopoi APK 2025 free lastest update
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
2025 Textile ERP Trends: SAP, Odoo & Oracle
history of c programming in notes for students .pptx
wealthsignaloriginal-com-DS-text-... (1).pdf
Navsoft: AI-Powered Business Solutions & Custom Software Development
Design an Analysis of Algorithms II-SECS-1021-03
Transform Your Business with a Software ERP System
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Operating system designcfffgfgggggggvggggggggg

des mutants dans le code.pdf

  • 2. SPOILER Beaucoup de références ciné + BD
  • 3. Professor X: For someone who hates mutants... you certainly keep some strange company. William Stryker: Oh, they serve their purpose... as long as they can be controlled.
  • 5. “the risk of bugs is proportional to the number of possible implementations for your program” https://livebook.manning.com/book/the-joy-of-kotlin/b-property-based-testing-in-kotlin/v-8/14
  • 7. Who can you trust?
  • 16. Changer pour vérifier Tests: States vs Behaviour Change Behaviour => Test should fail Attention aux doublures de tests
  • 18. Qui peut muter ? Tout le monde ? Le code : ● Il doit être résistant ● Toute mutation doit être détectée Les tests ● Ils doivent être fiables ● Les paramètres ?-> Tests paramètrés La configuration.?
  • 19. Comment ça marche? • Start with all tests passing Lâchez les fauves! ● Introduce changes in the code and observe the behavior of the unit tests • Two possible outcomes: • Some of the unit tests will start failing : good • All the tests will keep on passing : bad It means that the unit tests do not really validate outcomes of the code under test
  • 21. Exemple pour la CLR de .Net // IL ≅ ByteCode, DLL ≅ JAR
  • 23. 3 types 1. Statement Mutation – developer cut and pastes a part of a code of which the outcome may be a removal of some lines 2. Value Mutation– values of primary parameters/variables are modified 3. Decision Mutation– control statements are to be changed
  • 24. Différents types de mutation ● Arithmetic operators ● Equality operators
  • 25. Différents types de mutation ● Logical operators ● Negation operators
  • 26. The mutation score is defined as the percentage of killed mutants with the total number of mutants. ● Mutation Score = (Killed Mutants / Total number of Mutants) * 100 Test cases are mutation adequate if the score is 100%. Experimental results have shown that mutation testing is an effective approach for measuring the adequacy of the test cases. But, the main drawback is that the high cost of generating the mutants and executing each test case against that mutant program. https://www.guru99.com/mutation-testing.html
  • 28. A vous de jouer! https://pitest.org/ https://stryker-mutator.io/
  • 29. A vous de jouer! C# (Stryker.Net) ● 3 branches (complexité croissante) https://github.com/guillaumeagile/CSharp_mutation_Kata JAVA (PiTest) ● problème des Mocks https://github.com/guillaumeagile/mutate-test-kata/tree/onlyMockPb ● comment trouver le bon nombre de tests (nécessaires et suffisants) ? https://github.com/guillaumeagile/mutate_test_kata_java_2
  • 30. Comment soigner une mutation (facilement) ? 1. Pas (assez) de test 2. Assertions faibles 3. Cas aux limites
  • 31. Freinez vos Minions Mind the false positives • Due to aggressive mutations: • DEFAULTS should be conservative enough • ALL is probably too aggressive
  • 32. Smells in your Unit Tests 💩 ● No assertions ● Irrelevant assertions ● Use of Mocks ● Expected results are calculated rather than explicitly specified • Test code reuse (that is test logic reuse, test utilities are good) ● Test data reuse ● "Flickering" tests (tests with nondeterministic behavior) -> Threads 🚑 ● Interdependencies between tests (e.g., execution order) ● Long running tests ● @Ignore‘d or commented out tests
  • 34. Mocks: les ennemis des mutants Test qui utilise un mock pour “simplifier/abstraire” FileSystem par exemple Changement de comportement de FileSystem... sans changer les Mocks Les Mutants n’ont rien vu!
  • 35. Rappel : doublures de test Article genèse: https://martinfowler.com/articles/mocksArentStubs.html Article intense sur le sujet : https://enterprisecraftsmanship.com/posts/when-to-mock/
  • 36. Stub, Mock Très bon article FR https://agilitateur.azeau.com/post/2011/04/12/Pour-en-finir-avec-les-Stubs-et-les-Moc ks
  • 37. Behavioral testing with fakes TIP: Testing the test doubles Conseil avisé: https://tyrrrz.me/blog/fakes-over-mocks
  • 38. ICalculate::Variation(prix, delta) : prix Calcultator :: Variation(prix, delta) => prix * delta Mock<Calcultator> . setup(prix, delta) => prix * delta
  • 39. ModulateurDePrix(ICalculate calculator){ val delta : Int = 42 prixPlafonné(prix) if (calculator.Variation(prix, delta) < prix ) prix = prix + calculator.Variation(prix, delta) return prix }
  • 40. Test::: sut = ModulateurDePrix(mockedCalc, ()-> true) mockedCalc = Mock<Calcultator>() . setup(prix, delta) => prix * 0,2 val actual = sut.prixSelonLeTemps(10) actual shouldBe 12
  • 41. Test::: CalculateurQuantique sut = CalculateurQuantique actual = sut.Variation(10, 2) actual shouldBe 2 Code::: Calcultator :: Variation(prix, delta) => prix * delta
  • 42. Test::: CalculateurQuantique sut = CalculateurQuantique actual = sut.Variation(10, 2) actual shouldBe 2 Code::: Calcultator :: Variation(prix, delta) => - (prix * delta) MUTATION
  • 43. Cas plus épineux https://github.com/tpierrain/NFluent/blob/afda06c1bf761b598a0eaa32f4646510a56f6729/src/NFluent/Helpers/StringDifferen ce.cs#L114 This method has such cyclomatic complexity as well as overlapping conditions that many mutants are able to survive. // original line 131 var boolSingleLine = actualLines.Length == 1 && expectedLines.Length == 1; ... // mutant var boolSingleLine = actualLines.Length == 1 || expectedLines.Length == 1; actualLines et expectedLines ont *dans les faits* la même valeur, aucun test ne peut échouer 😣 https://many-cores.com/2019/05/17/mutation-testing/
  • 44. Refactoring needed Ne cédez pas à la tentation de marquer un faux positif the flow was objectively so complex that I could no longer understand, lest anticipate the impact of any change (link to original code). So I refactored it toward a simpler and cleaner design (new version here). Conseils et exemples de @Cyrdup
  • 46. Bénéfices de la mutation à vous de me dire...
  • 47. Des mutants pour surveiller votre style 1. Petits Pas 2. No Excuses 3. Commencer par un test Rouge!.. 4. ... et pour la bonne raison !! 5. Chicago School of Thought TDD (Inside Out/Bottom Up/Classic)
  • 48. Hardcore TDD The process should be: 1. Write the interface 2. Write the tests 3. Write the implementation and check if the tests pass. 4. Refactoring
  • 49. So, the first thing you have to do before even starting coding (the tests) is think about the problem in terms of the properties that should verify when considering the input and the output of the function. Property Based Testing = anti mutant weapon ?
  • 52. Cibler le code Uniquement votre code Chassez les dépendances ● en excluant de la mutation ● par une architecture en onion/ hexagonale Du code sans effet de bords ● transparence référentielle ● immutabilité
  • 53. Découper le code pour cibler https://www.kennethlange.com/functional-core-imperative-shell/
  • 54. Attention aux tests ● Exécution parallèle ● Mutation testing is a computationally expensive process and can take quite some time depending on the size of your codebase and the quality and speed of your test suite. ● Use filters to target only those packages or classes that are currently of interest ● Différence dans les tests = différence dans la résolution des mutations ○ Ex: tests have a hidden order dependency that is not revealed during the normal test run ● Some teams have very slow exhaustive tests or performance tests => EXCLUDE ● Throwing away tests is a hygienic move
  • 55. Merci pour vos retours https://roti.express/r/ag21-025