SlideShare una empresa de Scribd logo
1 de 23
Unit Testing Practical Tips
In the beginning First testing experience
Why Unit Testing? Instead of starting by Testing Manually, you start by testing programmatically. You test the code once… … and then continuously afterwards. Guards against others breaking your code. Guards against regression errors. Refactoring Ruthlessly Peace of Mind
Types of Testing – All Important Functional Testing (manual) Integration Testing (automated) (Dosens) Slow Unit Testing (automated) (Hundreds) Localize Failures Fast Dynamic Language Debuggers UI Layer Person View Person Controller Person Model Logic Layer Security Manager Data Layer
Test-First of Test-After? The difference is only When you write your tests? Test-After is painful and boring. Test-First makes unit testing easier. Unit Testing == Good. Win win situation. Test-Driven-Design Test-First drives the Design – Low Coupling. Write only enough to make the test pass. YAGNI
Automatic Test Generation? Only possible after the code has been written. You won’t get tests like… Test_that_user_registration_fails_on_invalid_ID() Doesn’t test Intent, can only test Methods.
Why testing efforts fail When tests become frustrating Integration Tests != Unit Tests Slow Brittle – break unnecessarily Hard to Write due to highly coupled code.
Let’s test some code
What to test? Single Responsibility Principle Test the responsibility of the Controller Action. ApplicationSuccess view should be returned if application succeeds. ApplicationFailure view should be returned with a reason if application fails.
Our “Unit Test” Database Web Service Session State
Step 1: Extract Class Single Responsibility Principle ,[object Object]
UserCreditService – Contains user credit checking functions,[object Object]
Injecting Fakes  ControllerTest Tests… Creates… Fake DB Repo Controller Uses… Fake DB Repo
Step 2: Fake object and Dependency Injection
Step 2: Fake object and dependency injection
Step 3: Abstract and Mock Session A Mock Example… ,[object Object]
Specify expectations
Works for Interfaces, Abstracts and Virtuals,[object Object]
Step 3: Abstract and Mock Session
Tips
Tip: A handy private class
Tip Fine grained is better 100 tests that test 100 things is better … than 10 tests that test a 100 things. Execute the same action many times in different tests and verify different results in each call. Makes errors easier to isolate.
Tips Break Dependencies – strive for low coupling Use Dependency Injection Avoid public Statics Avoid Singletons Use Interfaces to abstract calls to other classes Use Mocks to mimic and test behaviour Single Responsibility Principle – break things into smaller classes and test independently.

Más contenido relacionado

La actualidad más candente

Unit Testing Fundamentals
Unit Testing FundamentalsUnit Testing Fundamentals
Unit Testing Fundamentals
Richard Paul
 
Quality Software With Unit Test
Quality Software With Unit TestQuality Software With Unit Test
Quality Software With Unit Test
alice yang
 
Automation test
Automation testAutomation test
Automation test
yuyijq
 
Unit testing, UI testing and Test Driven Development in Visual Studio 2012
Unit testing, UI testing and Test Driven Development in Visual Studio 2012Unit testing, UI testing and Test Driven Development in Visual Studio 2012
Unit testing, UI testing and Test Driven Development in Visual Studio 2012
Jacinto Limjap
 
Unit Testing And Mocking
Unit Testing And MockingUnit Testing And Mocking
Unit Testing And Mocking
Joe Wilson
 

La actualidad más candente (20)

Nguyenvandungb seminar
Nguyenvandungb seminarNguyenvandungb seminar
Nguyenvandungb seminar
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
 
Unit testing - the hard parts
Unit testing - the hard partsUnit testing - the hard parts
Unit testing - the hard parts
 
White box testing
White box testing White box testing
White box testing
 
Clean Unit Test Patterns
Clean Unit Test PatternsClean Unit Test Patterns
Clean Unit Test Patterns
 
Test driven development in .Net - 2010 + Eclipse
Test driven development in .Net - 2010 + EclipseTest driven development in .Net - 2010 + Eclipse
Test driven development in .Net - 2010 + Eclipse
 
Unit Testing Fundamentals
Unit Testing FundamentalsUnit Testing Fundamentals
Unit Testing Fundamentals
 
An Introduction to Unit Testing
An Introduction to Unit TestingAn Introduction to Unit Testing
An Introduction to Unit Testing
 
Quality Software With Unit Test
Quality Software With Unit TestQuality Software With Unit Test
Quality Software With Unit Test
 
Debugging
DebuggingDebugging
Debugging
 
SE2_Lec 21_ TDD and Junit
SE2_Lec 21_ TDD and JunitSE2_Lec 21_ TDD and Junit
SE2_Lec 21_ TDD and Junit
 
Automation test
Automation testAutomation test
Automation test
 
Unit Testing Done Right
Unit Testing Done RightUnit Testing Done Right
Unit Testing Done Right
 
Unit Testing 101
Unit Testing 101Unit Testing 101
Unit Testing 101
 
Understanding Unit Testing
Understanding Unit TestingUnderstanding Unit Testing
Understanding Unit Testing
 
5 black box and grey box testing
5   black box and grey box testing5   black box and grey box testing
5 black box and grey box testing
 
Unit testing, UI testing and Test Driven Development in Visual Studio 2012
Unit testing, UI testing and Test Driven Development in Visual Studio 2012Unit testing, UI testing and Test Driven Development in Visual Studio 2012
Unit testing, UI testing and Test Driven Development in Visual Studio 2012
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
 
Unit Tests And Automated Testing
Unit Tests And Automated TestingUnit Tests And Automated Testing
Unit Tests And Automated Testing
 
Unit Testing And Mocking
Unit Testing And MockingUnit Testing And Mocking
Unit Testing And Mocking
 

Similar a Practical unit testing tips

Assessing Unit Test Quality
Assessing Unit Test QualityAssessing Unit Test Quality
Assessing Unit Test Quality
guest268ee8
 
Automated Unit Testing for Mere Mortals
Automated Unit Testing for Mere MortalsAutomated Unit Testing for Mere Mortals
Automated Unit Testing for Mere Mortals
Jess Chadwick
 

Similar a Practical unit testing tips (20)

TDD Best Practices
TDD Best PracticesTDD Best Practices
TDD Best Practices
 
Why Unit Testingl
Why Unit TestinglWhy Unit Testingl
Why Unit Testingl
 
Why unit testingl
Why unit testinglWhy unit testingl
Why unit testingl
 
Why Unit Testingl
Why Unit TestinglWhy Unit Testingl
Why Unit Testingl
 
SELJE_Database_Unit_Testing_Slides.pdf
SELJE_Database_Unit_Testing_Slides.pdfSELJE_Database_Unit_Testing_Slides.pdf
SELJE_Database_Unit_Testing_Slides.pdf
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
 
Unit testing
Unit testingUnit testing
Unit testing
 
Test driven development
Test driven developmentTest driven development
Test driven development
 
Unit testing, principles
Unit testing, principlesUnit testing, principles
Unit testing, principles
 
Real Life Unit Testing
Real Life Unit TestingReal Life Unit Testing
Real Life Unit Testing
 
Getting started with Test Driven Development
Getting started with Test Driven DevelopmentGetting started with Test Driven Development
Getting started with Test Driven Development
 
Getting started with Test Driven Development - Ferdous Mahmud Shaon
Getting started with Test Driven Development - Ferdous Mahmud ShaonGetting started with Test Driven Development - Ferdous Mahmud Shaon
Getting started with Test Driven Development - Ferdous Mahmud Shaon
 
Unit Testing & TDD Training for Mobile Apps
Unit Testing & TDD Training for Mobile AppsUnit Testing & TDD Training for Mobile Apps
Unit Testing & TDD Training for Mobile Apps
 
Assessing Unit Test Quality
Assessing Unit Test QualityAssessing Unit Test Quality
Assessing Unit Test Quality
 
Automated Unit Testing for Mere Mortals
Automated Unit Testing for Mere MortalsAutomated Unit Testing for Mere Mortals
Automated Unit Testing for Mere Mortals
 
assertYourself - Breaking the Theories and Assumptions of Unit Testing in Flex
assertYourself - Breaking the Theories and Assumptions of Unit Testing in FlexassertYourself - Breaking the Theories and Assumptions of Unit Testing in Flex
assertYourself - Breaking the Theories and Assumptions of Unit Testing in Flex
 
Unit testing - An introduction
Unit testing - An introductionUnit testing - An introduction
Unit testing - An introduction
 
An introduction to unit testing
An introduction to unit testingAn introduction to unit testing
An introduction to unit testing
 
Mocking
MockingMocking
Mocking
 
Workshop unit test
Workshop   unit testWorkshop   unit test
Workshop unit test
 

Último

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Último (20)

ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 

Practical unit testing tips

  • 2. In the beginning First testing experience
  • 3. Why Unit Testing? Instead of starting by Testing Manually, you start by testing programmatically. You test the code once… … and then continuously afterwards. Guards against others breaking your code. Guards against regression errors. Refactoring Ruthlessly Peace of Mind
  • 4. Types of Testing – All Important Functional Testing (manual) Integration Testing (automated) (Dosens) Slow Unit Testing (automated) (Hundreds) Localize Failures Fast Dynamic Language Debuggers UI Layer Person View Person Controller Person Model Logic Layer Security Manager Data Layer
  • 5. Test-First of Test-After? The difference is only When you write your tests? Test-After is painful and boring. Test-First makes unit testing easier. Unit Testing == Good. Win win situation. Test-Driven-Design Test-First drives the Design – Low Coupling. Write only enough to make the test pass. YAGNI
  • 6. Automatic Test Generation? Only possible after the code has been written. You won’t get tests like… Test_that_user_registration_fails_on_invalid_ID() Doesn’t test Intent, can only test Methods.
  • 7. Why testing efforts fail When tests become frustrating Integration Tests != Unit Tests Slow Brittle – break unnecessarily Hard to Write due to highly coupled code.
  • 9. What to test? Single Responsibility Principle Test the responsibility of the Controller Action. ApplicationSuccess view should be returned if application succeeds. ApplicationFailure view should be returned with a reason if application fails.
  • 10. Our “Unit Test” Database Web Service Session State
  • 11.
  • 12.
  • 13. Injecting Fakes ControllerTest Tests… Creates… Fake DB Repo Controller Uses… Fake DB Repo
  • 14. Step 2: Fake object and Dependency Injection
  • 15. Step 2: Fake object and dependency injection
  • 16.
  • 18.
  • 19. Step 3: Abstract and Mock Session
  • 20. Tips
  • 21. Tip: A handy private class
  • 22. Tip Fine grained is better 100 tests that test 100 things is better … than 10 tests that test a 100 things. Execute the same action many times in different tests and verify different results in each call. Makes errors easier to isolate.
  • 23. Tips Break Dependencies – strive for low coupling Use Dependency Injection Avoid public Statics Avoid Singletons Use Interfaces to abstract calls to other classes Use Mocks to mimic and test behaviour Single Responsibility Principle – break things into smaller classes and test independently.