SlideShare une entreprise Scribd logo
1  sur  15
Tests unitaires
Martin Yung
18 septembre 2019
 Quels sont les tests unitaires ?
 Concept
 Identifier les cas de test
 Connaître les outils (.NET Core)
 TDD
Les tests unitaires
ne sont pas….
les tests d’intégration
Les tests unitaires
nous permettent
d’isoler un composant et
de le tester
sans ses dépendances
Stub ou mock ?
 Stub
 Mock
Etes-vous un bon dév ?
public class FourWheelDrive
{
private IEnumerable<IWheel> _wheels;
public FourWheelDrive(IEnumerable<IWheel> wheels)
{
_wheels = wheels;
}
public bool Turn(IWheel wheel, float angleInDegree)
{
wheel.Turn(angleInDegree);
return true;
}
}
Etes-vous un bon dév ?
public bool Turn(IWheel wheel, float angleInDegree)
{
if (wheel == null)
{
throw new ArgumentNullException(nameof(wheel));
}
if (angleInDegree < 0 || angleInDegree >= 360)
{
throw new ArgumentOutOfRangeException(nameof(angleInDegree));
}
wheel.Turn(angleInDegree);
return true;
}
Arrange, act, assert…
 AAA (Arrange – Act – Assert)
 GWT (Given – When – Then)
Cas de test
 Cas nominaux
 Cas d’erreur
Cas nominal
[Fact]
public void FourWheelDrive_Turn()
{
// Arrange
var wheelStubs = Enumerable.Repeat(new WheelStub(), 4);
var fourWheelDrive = new FourWheelDrive(wheelStubs);
// Act
var wheelStub = new WheelStub();
var isTurned = fourWheelDrive.Turn(wheelStub, 10);
// Assert
Assert.True(isTurned);
}
Cas d’erreur
[Fact]
public void FourWheelDrive_Turn_ExpectsArgumentOutOfRangeException()
{
// Arrange
var wheelStubs = Enumerable.Repeat(new WheelStub(), 4);
var fourWheelDrive = new FourWheelDrive(wheelStubs);
// Act
var wheelStub = new WheelStub();
Action func = () => fourWheelDrive.Turn(wheelStub, 1000);
// Assert
var ex = Assert.Throws<ArgumentOutOfRangeException>(func);
Assert.IsType<ArgumentOutOfRangeException>(ex);
}
Outils (.NET Core)
 xUnit.net
 NUnit
TDD
 = Test Driven Development
 Principle
 Ecrire d’abord les tests unitaires, ils sont en rouge
 Ecrire les lignes de code pour passer les tests au vert
 Réorganiser vos codes
 Un autre nom : « Red-Green Testing »
Ce que nous avons vu
 Les tests unitaires
 Cas de test
 Outils (.NET Core)
 TDD
Merci 

Contenu connexe

En vedette

Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 

En vedette (20)

PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy Presentation
 

Tests unitaires

  • 2.  Quels sont les tests unitaires ?  Concept  Identifier les cas de test  Connaître les outils (.NET Core)  TDD
  • 3. Les tests unitaires ne sont pas…. les tests d’intégration
  • 4. Les tests unitaires nous permettent d’isoler un composant et de le tester sans ses dépendances
  • 5. Stub ou mock ?  Stub  Mock
  • 6. Etes-vous un bon dév ? public class FourWheelDrive { private IEnumerable<IWheel> _wheels; public FourWheelDrive(IEnumerable<IWheel> wheels) { _wheels = wheels; } public bool Turn(IWheel wheel, float angleInDegree) { wheel.Turn(angleInDegree); return true; } }
  • 7. Etes-vous un bon dév ? public bool Turn(IWheel wheel, float angleInDegree) { if (wheel == null) { throw new ArgumentNullException(nameof(wheel)); } if (angleInDegree < 0 || angleInDegree >= 360) { throw new ArgumentOutOfRangeException(nameof(angleInDegree)); } wheel.Turn(angleInDegree); return true; }
  • 8. Arrange, act, assert…  AAA (Arrange – Act – Assert)  GWT (Given – When – Then)
  • 9. Cas de test  Cas nominaux  Cas d’erreur
  • 10. Cas nominal [Fact] public void FourWheelDrive_Turn() { // Arrange var wheelStubs = Enumerable.Repeat(new WheelStub(), 4); var fourWheelDrive = new FourWheelDrive(wheelStubs); // Act var wheelStub = new WheelStub(); var isTurned = fourWheelDrive.Turn(wheelStub, 10); // Assert Assert.True(isTurned); }
  • 11. Cas d’erreur [Fact] public void FourWheelDrive_Turn_ExpectsArgumentOutOfRangeException() { // Arrange var wheelStubs = Enumerable.Repeat(new WheelStub(), 4); var fourWheelDrive = new FourWheelDrive(wheelStubs); // Act var wheelStub = new WheelStub(); Action func = () => fourWheelDrive.Turn(wheelStub, 1000); // Assert var ex = Assert.Throws<ArgumentOutOfRangeException>(func); Assert.IsType<ArgumentOutOfRangeException>(ex); }
  • 12. Outils (.NET Core)  xUnit.net  NUnit
  • 13. TDD  = Test Driven Development  Principle  Ecrire d’abord les tests unitaires, ils sont en rouge  Ecrire les lignes de code pour passer les tests au vert  Réorganiser vos codes  Un autre nom : « Red-Green Testing »
  • 14. Ce que nous avons vu  Les tests unitaires  Cas de test  Outils (.NET Core)  TDD

Notes de l'éditeur

  1. Dans l’exemple que l’on vu : 2 cas d’erreur : ArgumentNullException et ArgumentOutOfRangeException
  2. Framework de mock : https://github.com/moq/moq