SlideShare a Scribd company logo
1 of 27
Understanding
Unit Testing
INTRODUCTION TO AUTOMATED TESTING AND UNIT TESTING
Introduction
 Ikhwan Hayat (a.k.a 1kHz)
 9 years experience developing
software
 Freelance Software Developer
 ikhwanhayat@gmail.com
2
manual
3
Testing?
VS
automated
white box
VS
black box
acceptance test
integration test
unit test
Unit? 4
Unit testing is a method by which individual units of
source code are tested to determine if they are fit for
use.
One can view a unit as the smallest testable part
of an application.
Unit tests are created by programmers or
occasionally by white box testers during the
development process.
TDD
 Test Driven Design
 It’s NOT testing, but using tests to DRIVE the
design
 As a side-effect, you got unit tests!
With good level of coverage!
5
6
RED
GREEN
REFACTOR
Write a failing test. With empty class/method.
Fill in the class/method implementation. Make the tests pass.
Make code better.
Demo: Bank Account
 Requirements
 Can create bank accounts
 Can deposit money into account
 Can withdraw money from account
 Throw exception if balance is insufficient
 Can transfer money from one account to another
 Can insert and update accounts into database
7
8
IDE / Platform
http://www.microsoft.com/visualstudio/eng/pro
ducts/visual-studio-express-for-web
https://code.google.com/p/moq/
Unit Testing Framework
Mocking Framework
http://nunit.org/
Tools
Design 9
10
ARRANGE
ACT
ASSERT
Unit
11
Test
12
Design 2 13
14
Unit Test
Design 3 15
16
repository
VS
active record
???
17Unit Test
A good unit test is…
 Isolated/independent
 Test one thing at a time.
 Unit under test doesn’t depend on the other to make test runs.
 Repeatable
 Running multiple times yields the same result.
 Doesn’t rely on environment.
 Fast
 You want to repeat it again and again.
 You want it to be a pleasure to work with.
 Self-Documenting
 Test code and code under test clear and concise.
 Can be a reference for usage of your class/method/etc.
18
19
Dependency Injection
Separation of Interface
and Implementation
Single Responsibility Principal
20
Web Controller DI/IoC Setup
21
“Mocking” allow us to
isolate dependent
units.
Types of Test Doubles
 Dummy objects are passed around but never actually
used. Usually they are just used to fill parameter lists.
 Fake objects actually have working
implementations, but usually take some shortcut which
makes them not suitable for production.
 Stub objects provide canned answers to calls made
during the test, usually not responding at all to anything
outside what's programmed in for the test.
 Mock objects are pre-programmed with expectations
which form a specification of the calls they are
expected to receive.
22
23
Unit Test
Benefits
 Instant feedback
 Write test, write code, see instant result.
 Promote modularity in your design
 DI, SRP, Interface vs Implementation.
 Safety net
 Change/add code and check if business rules are still
honored.
 Free documentation
 Can be a reference for usage of your
class/method/etc.
24
Done!
 What is unit testing.
 TDD.
 How to write good unit tests.
 How unit testing can benefit us.
25
We have learned…
Get the codes and slides at…
https://github.com/ikhwanhayat/jomweb_unittest
Next?
 Learning BDD (Behavior Driven Design).
 Research CI (Continuous Integration).
 Learn on how to design testable systems.
 Use unit testing for your project!
(I mean, seriously, USE IT!)
26
You can go on with…
MOAR!
 Google+ MyDev
 http://www.mydev.my/automated-testing-
dalam-pembangunan-perisian.html
 ikhwanhayat@gmail.com
27
THANK YOU FOR LISTENING!

More Related Content

What's hot

Unit testing best practices
Unit testing best practicesUnit testing best practices
Unit testing best practices
nickokiss
 
UNIT TESTING PPT
UNIT TESTING PPTUNIT TESTING PPT
UNIT TESTING PPT
suhasreddy1
 
Unit Test Presentation
Unit Test PresentationUnit Test Presentation
Unit Test Presentation
Sayedur Rahman
 
Software testing.ppt
Software testing.pptSoftware testing.ppt
Software testing.ppt
Komal Garg
 
Basic software-testing-concepts
Basic software-testing-conceptsBasic software-testing-concepts
Basic software-testing-concepts
medsherb
 

What's hot (20)

An introduction to unit testing
An introduction to unit testingAn introduction to unit testing
An introduction to unit testing
 
Unit testing best practices
Unit testing best practicesUnit testing best practices
Unit testing best practices
 
UNIT TESTING PPT
UNIT TESTING PPTUNIT TESTING PPT
UNIT TESTING PPT
 
Beginners - Get Started With Unit Testing in .NET
Beginners - Get Started With Unit Testing in .NETBeginners - Get Started With Unit Testing in .NET
Beginners - Get Started With Unit Testing in .NET
 
Unit Test Presentation
Unit Test PresentationUnit Test Presentation
Unit Test Presentation
 
Unit Testing (C#)
Unit Testing (C#)Unit Testing (C#)
Unit Testing (C#)
 
Test automation
Test automationTest automation
Test automation
 
Test Driven Development (TDD)
Test Driven Development (TDD)Test Driven Development (TDD)
Test Driven Development (TDD)
 
Software testing.ppt
Software testing.pptSoftware testing.ppt
Software testing.ppt
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
 
Unit testing with NUnit
Unit testing with NUnitUnit testing with NUnit
Unit testing with NUnit
 
Sanity testing and smoke testing
Sanity testing and smoke testingSanity testing and smoke testing
Sanity testing and smoke testing
 
Unit testing
Unit testingUnit testing
Unit testing
 
Unit Testing vs Integration Testing
Unit Testing vs Integration TestingUnit Testing vs Integration Testing
Unit Testing vs Integration Testing
 
Test Automation
Test AutomationTest Automation
Test Automation
 
An Introduction to Test Driven Development
An Introduction to Test Driven Development An Introduction to Test Driven Development
An Introduction to Test Driven Development
 
Basic software-testing-concepts
Basic software-testing-conceptsBasic software-testing-concepts
Basic software-testing-concepts
 
Types of Software Testing | Edureka
Types of Software Testing | EdurekaTypes of Software Testing | Edureka
Types of Software Testing | Edureka
 
Automated Testing with Agile
Automated Testing with AgileAutomated Testing with Agile
Automated Testing with Agile
 
What is Integration Testing? | Edureka
What is Integration Testing? | EdurekaWhat is Integration Testing? | Edureka
What is Integration Testing? | Edureka
 

Similar to Understanding Unit Testing

Agile Testing Pasadena JUG Aug2009
Agile Testing Pasadena JUG Aug2009Agile Testing Pasadena JUG Aug2009
Agile Testing Pasadena JUG Aug2009
Grig Gheorghiu
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
bhochhi
 
Test driven development
Test driven developmentTest driven development
Test driven development
Harry Potter
 
Test driven development
Test driven developmentTest driven development
Test driven development
Luis Goldster
 
Test driven development
Test driven developmentTest driven development
Test driven development
Tony Nguyen
 
Test driven development
Test driven developmentTest driven development
Test driven development
Young Alista
 
Test driven development
Test driven developmentTest driven development
Test driven development
James Wong
 
Test driven development
Test driven developmentTest driven development
Test driven development
Fraboni Ec
 

Similar to Understanding Unit Testing (20)

xUnit and TDD: Why and How in Enterprise Software, August 2012
xUnit and TDD: Why and How in Enterprise Software, August 2012xUnit and TDD: Why and How in Enterprise Software, August 2012
xUnit and TDD: Why and How in Enterprise Software, August 2012
 
Test driven development
Test driven developmentTest driven development
Test driven development
 
Ian Cooper webinar for DDD Iran: Kent beck style tdd seven years after
Ian Cooper webinar for DDD Iran: Kent beck style tdd   seven years afterIan Cooper webinar for DDD Iran: Kent beck style tdd   seven years after
Ian Cooper webinar for DDD Iran: Kent beck style tdd seven years after
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Test driven development(tdd)
Test driven development(tdd)Test driven development(tdd)
Test driven development(tdd)
 
Unit testing - An introduction
Unit testing - An introductionUnit testing - An introduction
Unit testing - An introduction
 
TDD - Agile
TDD - Agile TDD - Agile
TDD - Agile
 
Loopt unit test experiences
Loopt unit test experiencesLoopt unit test experiences
Loopt unit test experiences
 
TDD Workshop UTN 2012
TDD Workshop UTN 2012TDD Workshop UTN 2012
TDD Workshop UTN 2012
 
Agile Testing Pasadena JUG Aug2009
Agile Testing Pasadena JUG Aug2009Agile Testing Pasadena JUG Aug2009
Agile Testing Pasadena JUG Aug2009
 
Test Driven Development - Overview and Adoption
Test Driven Development - Overview and AdoptionTest Driven Development - Overview and Adoption
Test Driven Development - Overview and Adoption
 
Developers’ mDay u Banjoj Luci - Milan Popović, PHP Srbija – Testimony (about...
Developers’ mDay u Banjoj Luci - Milan Popović, PHP Srbija – Testimony (about...Developers’ mDay u Banjoj Luci - Milan Popović, PHP Srbija – Testimony (about...
Developers’ mDay u Banjoj Luci - Milan Popović, PHP Srbija – Testimony (about...
 
Software presentation
Software presentationSoftware presentation
Software presentation
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Test driven development
Test driven developmentTest driven development
Test driven development
 
Test driven development
Test driven developmentTest driven development
Test driven development
 
Test driven development
Test driven developmentTest driven development
Test driven development
 
Test driven development
Test driven developmentTest driven development
Test driven development
 
Test driven development
Test driven developmentTest driven development
Test driven development
 
Test driven development
Test driven developmentTest driven development
Test driven development
 

Recently uploaded

+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@
 
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
 

Recently uploaded (20)

Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
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
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
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
 
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
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
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
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
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...
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
+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...
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
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
 
"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 ...
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Cyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdfCyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdf
 

Understanding Unit Testing

  • 1. Understanding Unit Testing INTRODUCTION TO AUTOMATED TESTING AND UNIT TESTING
  • 2. Introduction  Ikhwan Hayat (a.k.a 1kHz)  9 years experience developing software  Freelance Software Developer  ikhwanhayat@gmail.com 2
  • 4. Unit? 4 Unit testing is a method by which individual units of source code are tested to determine if they are fit for use. One can view a unit as the smallest testable part of an application. Unit tests are created by programmers or occasionally by white box testers during the development process.
  • 5. TDD  Test Driven Design  It’s NOT testing, but using tests to DRIVE the design  As a side-effect, you got unit tests! With good level of coverage! 5
  • 6. 6 RED GREEN REFACTOR Write a failing test. With empty class/method. Fill in the class/method implementation. Make the tests pass. Make code better.
  • 7. Demo: Bank Account  Requirements  Can create bank accounts  Can deposit money into account  Can withdraw money from account  Throw exception if balance is insufficient  Can transfer money from one account to another  Can insert and update accounts into database 7
  • 12. 12
  • 18. A good unit test is…  Isolated/independent  Test one thing at a time.  Unit under test doesn’t depend on the other to make test runs.  Repeatable  Running multiple times yields the same result.  Doesn’t rely on environment.  Fast  You want to repeat it again and again.  You want it to be a pleasure to work with.  Self-Documenting  Test code and code under test clear and concise.  Can be a reference for usage of your class/method/etc. 18
  • 19. 19 Dependency Injection Separation of Interface and Implementation Single Responsibility Principal
  • 21. 21 “Mocking” allow us to isolate dependent units.
  • 22. Types of Test Doubles  Dummy objects are passed around but never actually used. Usually they are just used to fill parameter lists.  Fake objects actually have working implementations, but usually take some shortcut which makes them not suitable for production.  Stub objects provide canned answers to calls made during the test, usually not responding at all to anything outside what's programmed in for the test.  Mock objects are pre-programmed with expectations which form a specification of the calls they are expected to receive. 22
  • 24. Benefits  Instant feedback  Write test, write code, see instant result.  Promote modularity in your design  DI, SRP, Interface vs Implementation.  Safety net  Change/add code and check if business rules are still honored.  Free documentation  Can be a reference for usage of your class/method/etc. 24
  • 25. Done!  What is unit testing.  TDD.  How to write good unit tests.  How unit testing can benefit us. 25 We have learned… Get the codes and slides at… https://github.com/ikhwanhayat/jomweb_unittest
  • 26. Next?  Learning BDD (Behavior Driven Design).  Research CI (Continuous Integration).  Learn on how to design testable systems.  Use unit testing for your project! (I mean, seriously, USE IT!) 26 You can go on with…
  • 27. MOAR!  Google+ MyDev  http://www.mydev.my/automated-testing- dalam-pembangunan-perisian.html  ikhwanhayat@gmail.com 27 THANK YOU FOR LISTENING!

Editor's Notes

  1. In procedural -> module/function, In OOP -> class/method. Jenis test paling mendalam. What you actually test is behavior.
  2. Create projects
  3. Go to codes
  4. Go to codes
  5. Copy files