SlideShare une entreprise Scribd logo
1  sur  47
Télécharger pour lire hors ligne
Unit Testing & TDD Training
For iOS & Android Native Apps
Agenda
- Part 1: Unit Testing Concepts
- Part 2: Test-Driven Development
- Part 3: Object Oriented Design for Testing
Unit Testing Concepts
Unit Testing Definition
- A unit test is code made to test a portion of
our applications code in an automated
manner, verifying methods functionality
(result or behavior) of a particular class (SUT
or System Under Testing).
Unit Tests Main Properties
- Independent
- Running a UT should not affect another UT run. Execution order is not
important.
- Fast
- A test which takes several seconds to run is not a Unit Test and
probably needs a refactor.
- Isolated
- UT should not depend on external systems. They need to know their
collaborators answers.
Test Class Structure
- Class Setup/TearDown
- Executed once when the UT class is initialized, not for every test in the
class. Useful to avoid test code duplication at class level.
- Test Setup/TearDown
- Executed once before and after each executed UT. Useful to avoid test
code duplication at test level.
- Test Methods
- The Unit Test implementation itself. Here is where the test run and
validations happens.
Android Test Structure Example (Java)
iOS Test Structure Example (Swift)
Tests Naming
- Use descriptive test names. It will provide quick
feedback when test fails.
- Avoid prefix “test” when no required by framework.
Android JUnit does not need it, XCTest require it.
- Choose names which in few words says:
- Method under testing
- Condition under testing
- Expected result/behavior
Tests Naming Examples
- Good examples:
- Android:
getPayments_oneRegisteredPayment_returnsListWithRegisteredPayment
- iOS:
testListPayments_oneRegisteredPayment_returnsListWithRegisteredPayment
- Bad examples:
- testCalculateArea2
- testCalculateArea3
Test Method Organization
- Arrange / Setup
- Prepare all needed objects/dependencies for test execution.
- Act / Test
- Test itself it performed, calling one specific method in the system
under testing.
- Assert / Verify
- Method result/behavior verification is performed at the end, using test
asserts.
Tests Verification Types
- State Verification
- Verify the result returned for the method under test, no matter which
interactions with other objects has been made. Useful and preferred
verification in most cases.
- Behavior Verification
- Verify that all tested method interactions among other collaboration
objects are the expected one (generally done using mocks). Useful for
some cases where interactions are important (for example a cache
implementation).
Android State Verification Example (Java)
iOS State Verification Example (Swift)
Test Doubles
- Dummy
- Fake
- Stubs
- Mocks
Test Doubles
- Dummy
- These are objects used to satisfy class dependencies, but they are not
used in the test execution itself. For example Java constructor or
Swift init dependencies for class instantiation.
- Fake
- Objects with functional implementation, but with useful shortcuts for
testing. For example an in-memory database, which works but after a
test run lost all stored data.
Test Doubles
- Stubs
- Objects with pre-programmed replies. They are not prepared to
respond to another thing more than what they were programmed for.
Useful in tests for state verifications.
- Mocks
- Pre-programmed objects with expectations, which are requirements
about it use, like: way to be called, invocations amount, invocations
parameters. Useful in tests for behavior verifications.
Android Behavior Verification Example (Java - Mockito)
Android Behavior Verification Example (Java - EasyMock)
iOS Behavior Verification Example (Swift)
Test-driven Development
(TDD)
What TDD is?
● It is a software development process driven by tests.
● It consist in first write one or more unit tests of a class yet to be
implemented, then write the minimum necessary code to make that test
pass.
● Each Unit Test represents a requirement for that class.
● Each Unit Test keeps and evaluates the developer's knowledge about that
class over time.
● It avoids unnecessary code complexity not specified by a particular
requirement.
What TDD is not?
● It is not a testing technique.
● It is not write a class first and then write tests to have a good code
coverage.
● It is not functional tests, it must be quickly executed and must not have
dependencies with external systems.
The TDD process - Step 1
Add a new unit test, that represents a
requirement for a method of a class.
The TDD process - Step 2
Execute the test and expect it to fail. In cases
when the test don't fail, this means that the
requirement has been fulfilled before. Go back
to step 1 and add a new unit test for a new
requirement.
The TDD process - Step 3
Implement the method in the class, to make
the new unit test successfully pass.
The TDD process - Step 4
Execute all the unit test suite, making sure that
all the previous test successfully pass. If some
tests fail, fix the implementation that make
those tests fail. Remember to re execute the
test suite every time the code has been
changed.
The TDD process - Step 5
Improvements and cleaning code, this can be
made without worries about breaking
something because the test suite gives us
immediate feedback if some test fails
TDD Process in a nutshell
1. Add a new test
2. Execute the test
3. Add the least amount of code necessary to make that test pass
4. Execute the whole test suite
5. Refactor and clean code
Tips
● Any development can be faced using TDD starting with an interface with
the signature of the methods to be implemented and tested.
● One single person can be in charge to implement the tests and other
person can be in charge to implement the class methods.
● An alternative to avoid the interface is to define the class with its method
by simply throwing an exception indicating that the method has not yet
been implemented.
Object Oriented Design for Testing
Dependency Injection
- Allows that dependent objects needed in our
class implementation can be specified
(injected) from outside of that class.
- This can be done using constructors/initializers
or properties.
Dependency Injection - Using Constructor (Java)
Dependency Injection - Using Initializer (Swift)
Dependency Injection - Using Setter (Java)
Dependency Injection - Using Property (Swift)
Single Responsibility
- Each class should have a single responsibility.
- Reduces coupling of different domain
functionalities.
- Facilitates classes reusage.
- The opposite of having Utils/Helper classes.
These classes hardly have a unique responsibility.
Single Responsibility - Examples
- Bad Example:
- Good Examples:
StringUtil
+isValidDate(String)
+parseDate(String)
+translateString(String)
DateParser
+isValidDate(String)
+parseDate(String)
TranslationService
+translateString(String)
Don’t talk to strangers (1)
- An object only should invoke methods of direct
known dependencies.
- Avoid calling dependencies of our dependencies
(strangers). This is avoid chain of methods in
code.
Don’t talk to strangers (2)
- It’s a guide, not a rule. For example this could be
avoid in model/entities objects.
- Reduces object coupling, improving code
maintainability and allows refactor when needed.
Don’t talk to strangers - Examples
- Bad Examples:
Java: this.paymentService.getStorageService().getStoredObject(paymentId)
Swift: self.paymentService.storageService.storedObjectWithId(paymentId)
- Good Examples:
Java: this.paymentService.getPayment(payement)
Swift: self.paymentService.paymentWithId(paymentId)
Tell don’t ask
- Objects should tell another objects to perform
actions instead of asking for data and processing
the data itself.
- Objects should be as lazy as possible and
delegate the processing to its dependencies.
- Improves class testability and class cohesion.
Tell don’t ask - Examples
- Bad Example:
- Good Example:
ShapeService
-shapes : List<Shape>
+calculateTotalArea() : float
Rectangle <Shape>
+getHeight() : float
+getWidth() : float
ShapeService
-shapes : List<Shape>
+calculateTotalArea() : float
Rectangle <Shape>
+calculateArea() : float
Shape
+calculateArea() : float
Circle <Shape>
+getRadius() : float
Circle <Shape>
+calculateArea() : float
Shape
Avoid Singletons
- Singleton is a pattern that share a global state in
the whole application.
- In unit testing, does not allow dependencies
control, because it hides class dependencies to
the consumer.
- Test execution order can affect the result of the
test run when using singletons.
Avoid Singletons - Examples
- Bad Example:
- Good Example:
PaymentService
-storageService
+PaymentService()
StorageService <<Singleton>>
-instance
+getInstance() : Storage
PaymentService
-storageService
+PaymentService(storageService)
StorageService
StorageServiceFactory
+getStorageService() : StorageService
MyApp
-paymentService
+execute()
MyApp
-paymentService
- storageService
+execute()
Questions?
Thank you so much!

Contenu connexe

Tendances

Benefit From Unit Testing In The Real World
Benefit From Unit Testing In The Real WorldBenefit From Unit Testing In The Real World
Benefit From Unit Testing In The Real WorldDror Helper
 
Unit Testing And Mocking
Unit Testing And MockingUnit Testing And Mocking
Unit Testing And MockingJoe Wilson
 
How and what to unit test
How and what to unit testHow and what to unit test
How and what to unit testEugenio Lentini
 
Unit Testing Fundamentals
Unit Testing FundamentalsUnit Testing Fundamentals
Unit Testing FundamentalsRichard Paul
 
Java Unit Test and Coverage Introduction
Java Unit Test and Coverage IntroductionJava Unit Test and Coverage Introduction
Java Unit Test and Coverage IntroductionAlex Su
 
Unit Testing Guidelines
Unit Testing GuidelinesUnit Testing Guidelines
Unit Testing GuidelinesJoel Hooks
 
Unit Test Presentation
Unit Test PresentationUnit Test Presentation
Unit Test PresentationSayedur Rahman
 
Assessing Unit Test Quality
Assessing Unit Test QualityAssessing Unit Test Quality
Assessing Unit Test Qualityguest268ee8
 
Understanding Unit Testing
Understanding Unit TestingUnderstanding Unit Testing
Understanding Unit Testingikhwanhayat
 
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 2012Jacinto Limjap
 
Xp Day 080506 Unit Tests And Mocks
Xp Day 080506 Unit Tests And MocksXp Day 080506 Unit Tests And Mocks
Xp Day 080506 Unit Tests And Mocksguillaumecarre
 
Unit and integration Testing
Unit and integration TestingUnit and integration Testing
Unit and integration TestingDavid Berliner
 
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 .NETBaskar K
 
Roy Osherove on Unit Testing Good Practices and Horrible Mistakes
Roy Osherove on Unit Testing Good Practices and Horrible MistakesRoy Osherove on Unit Testing Good Practices and Horrible Mistakes
Roy Osherove on Unit Testing Good Practices and Horrible MistakesRoy Osherove
 
Unit Tests And Automated Testing
Unit Tests And Automated TestingUnit Tests And Automated Testing
Unit Tests And Automated TestingLee Englestone
 
SE2_Lec 21_ TDD and Junit
SE2_Lec 21_ TDD and JunitSE2_Lec 21_ TDD and Junit
SE2_Lec 21_ TDD and JunitAmr E. Mohamed
 
Quality Software With Unit Test
Quality Software With Unit TestQuality Software With Unit Test
Quality Software With Unit Testalice yang
 

Tendances (20)

Benefit From Unit Testing In The Real World
Benefit From Unit Testing In The Real WorldBenefit From Unit Testing In The Real World
Benefit From Unit Testing In The Real World
 
Unit Testing And Mocking
Unit Testing And MockingUnit Testing And Mocking
Unit Testing And Mocking
 
Unit testing
Unit testingUnit testing
Unit testing
 
How and what to unit test
How and what to unit testHow and what to unit test
How and what to unit test
 
Unit Testing Fundamentals
Unit Testing FundamentalsUnit Testing Fundamentals
Unit Testing Fundamentals
 
Java Unit Test and Coverage Introduction
Java Unit Test and Coverage IntroductionJava Unit Test and Coverage Introduction
Java Unit Test and Coverage Introduction
 
Unit Testing Guidelines
Unit Testing GuidelinesUnit Testing Guidelines
Unit Testing Guidelines
 
Unit Test Presentation
Unit Test PresentationUnit Test Presentation
Unit Test Presentation
 
Assessing Unit Test Quality
Assessing Unit Test QualityAssessing Unit Test Quality
Assessing Unit Test Quality
 
Understanding Unit Testing
Understanding Unit TestingUnderstanding Unit Testing
Understanding Unit 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
 
N Unit Presentation
N Unit PresentationN Unit Presentation
N Unit Presentation
 
Xp Day 080506 Unit Tests And Mocks
Xp Day 080506 Unit Tests And MocksXp Day 080506 Unit Tests And Mocks
Xp Day 080506 Unit Tests And Mocks
 
Unit and integration Testing
Unit and integration TestingUnit and integration Testing
Unit and integration Testing
 
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
 
Roy Osherove on Unit Testing Good Practices and Horrible Mistakes
Roy Osherove on Unit Testing Good Practices and Horrible MistakesRoy Osherove on Unit Testing Good Practices and Horrible Mistakes
Roy Osherove on Unit Testing Good Practices and Horrible Mistakes
 
Unit Tests And Automated Testing
Unit Tests And Automated TestingUnit Tests And Automated Testing
Unit Tests And Automated Testing
 
SE2_Lec 21_ TDD and Junit
SE2_Lec 21_ TDD and JunitSE2_Lec 21_ TDD and Junit
SE2_Lec 21_ TDD and Junit
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
 
Quality Software With Unit Test
Quality Software With Unit TestQuality Software With Unit Test
Quality Software With Unit Test
 

En vedette

Unit Testing in Java
Unit Testing in JavaUnit Testing in Java
Unit Testing in Javaguy_davis
 
TDD Flow: The Mantra in Action
TDD Flow: The Mantra in ActionTDD Flow: The Mantra in Action
TDD Flow: The Mantra in ActionDionatan default
 
TDD and the Legacy Code Black Hole
TDD and the Legacy Code Black HoleTDD and the Legacy Code Black Hole
TDD and the Legacy Code Black HoleNoam Kfir
 
Making Swift even safer
Making Swift even saferMaking Swift even safer
Making Swift even saferDenis Fileev
 
Unit testing in iOS featuring OCUnit, GHUnit & OCMock
Unit testing in iOS featuring OCUnit, GHUnit & OCMockUnit testing in iOS featuring OCUnit, GHUnit & OCMock
Unit testing in iOS featuring OCUnit, GHUnit & OCMockRobot Media
 
Testing in swift
Testing in swiftTesting in swift
Testing in swifthugo lu
 
Test Driven Development (TDD) with FlexUnit 4 - 360|Flex San Jose preso
Test Driven Development (TDD) with FlexUnit 4 - 360|Flex San Jose presoTest Driven Development (TDD) with FlexUnit 4 - 360|Flex San Jose preso
Test Driven Development (TDD) with FlexUnit 4 - 360|Flex San Jose presoElad Elrom
 
TDD and more than 9000 tries to sell it to a customer
TDD and more than 9000 tries to sell it to a customerTDD and more than 9000 tries to sell it to a customer
TDD and more than 9000 tries to sell it to a customerAnuar Nurmakanov
 
UI Testing with Earl Grey
UI Testing with Earl GreyUI Testing with Earl Grey
UI Testing with Earl GreyShyam Bhat
 
Writing Swift code with great testability
Writing Swift code with great testabilityWriting Swift code with great testability
Writing Swift code with great testabilityJohn Sundell
 
Test-Driven Development (TDD)
Test-Driven Development (TDD)Test-Driven Development (TDD)
Test-Driven Development (TDD)Brian Rasmussen
 
TDD is for Dreamers, not for Real Developers, Isn't It? - Entwicklertag Frank...
TDD is for Dreamers, not for Real Developers, Isn't It? - Entwicklertag Frank...TDD is for Dreamers, not for Real Developers, Isn't It? - Entwicklertag Frank...
TDD is for Dreamers, not for Real Developers, Isn't It? - Entwicklertag Frank...Sven Amann
 
Ornamental Iron Catalog-Hebei Founder
Ornamental Iron Catalog-Hebei FounderOrnamental Iron Catalog-Hebei Founder
Ornamental Iron Catalog-Hebei FounderEvan Fan
 
The Aleanda Group Q3 2009
The Aleanda Group Q3  2009The Aleanda Group Q3  2009
The Aleanda Group Q3 2009Aleanda
 

En vedette (20)

Unit Testing in Java
Unit Testing in JavaUnit Testing in Java
Unit Testing in Java
 
TDD Flow: The Mantra in Action
TDD Flow: The Mantra in ActionTDD Flow: The Mantra in Action
TDD Flow: The Mantra in Action
 
TDD and the Legacy Code Black Hole
TDD and the Legacy Code Black HoleTDD and the Legacy Code Black Hole
TDD and the Legacy Code Black Hole
 
Making Swift even safer
Making Swift even saferMaking Swift even safer
Making Swift even safer
 
Unit testing in iOS featuring OCUnit, GHUnit & OCMock
Unit testing in iOS featuring OCUnit, GHUnit & OCMockUnit testing in iOS featuring OCUnit, GHUnit & OCMock
Unit testing in iOS featuring OCUnit, GHUnit & OCMock
 
Testing in swift
Testing in swiftTesting in swift
Testing in swift
 
Unit Testing in Swift
Unit Testing in SwiftUnit Testing in Swift
Unit Testing in Swift
 
Test Driven Development (TDD) with FlexUnit 4 - 360|Flex San Jose preso
Test Driven Development (TDD) with FlexUnit 4 - 360|Flex San Jose presoTest Driven Development (TDD) with FlexUnit 4 - 360|Flex San Jose preso
Test Driven Development (TDD) with FlexUnit 4 - 360|Flex San Jose preso
 
TDD and more than 9000 tries to sell it to a customer
TDD and more than 9000 tries to sell it to a customerTDD and more than 9000 tries to sell it to a customer
TDD and more than 9000 tries to sell it to a customer
 
UI Testing with Earl Grey
UI Testing with Earl GreyUI Testing with Earl Grey
UI Testing with Earl Grey
 
TDD Overview
TDD OverviewTDD Overview
TDD Overview
 
Writing Swift code with great testability
Writing Swift code with great testabilityWriting Swift code with great testability
Writing Swift code with great testability
 
Test-Driven Development (TDD)
Test-Driven Development (TDD)Test-Driven Development (TDD)
Test-Driven Development (TDD)
 
TDD is for Dreamers, not for Real Developers, Isn't It? - Entwicklertag Frank...
TDD is for Dreamers, not for Real Developers, Isn't It? - Entwicklertag Frank...TDD is for Dreamers, not for Real Developers, Isn't It? - Entwicklertag Frank...
TDD is for Dreamers, not for Real Developers, Isn't It? - Entwicklertag Frank...
 
TDD - Agile
TDD - Agile TDD - Agile
TDD - Agile
 
TDD or TFD
TDD or TFDTDD or TFD
TDD or TFD
 
Ornamental Iron Catalog-Hebei Founder
Ornamental Iron Catalog-Hebei FounderOrnamental Iron Catalog-Hebei Founder
Ornamental Iron Catalog-Hebei Founder
 
KYE Keynote
KYE KeynoteKYE Keynote
KYE Keynote
 
The Aleanda Group Q3 2009
The Aleanda Group Q3  2009The Aleanda Group Q3  2009
The Aleanda Group Q3 2009
 
Him Reak
Him ReakHim Reak
Him Reak
 

Similaire à Unit Testing & TDD Training for Mobile Apps

Unit & integration testing
Unit & integration testingUnit & integration testing
Unit & integration testingPavlo Hodysh
 
Unit testing & TDD concepts with best practice guidelines.
Unit testing & TDD concepts with best practice guidelines.Unit testing & TDD concepts with best practice guidelines.
Unit testing & TDD concepts with best practice guidelines.Mohamed Taman
 
Implementing TDD in for .net Core applications
Implementing TDD in for .net Core applicationsImplementing TDD in for .net Core applications
Implementing TDD in for .net Core applicationsAhmad Kazemi
 
Unit Testing Full@
Unit Testing Full@Unit Testing Full@
Unit Testing Full@Alex Borsuk
 
Unit testing basics with NUnit and Visual Studio
Unit testing basics with NUnit and Visual StudioUnit testing basics with NUnit and Visual Studio
Unit testing basics with NUnit and Visual StudioAmit Choudhary
 
Unit test documentation
Unit test documentationUnit test documentation
Unit test documentationAnjan Debnath
 
Testing and TDD - KoJUG
Testing and TDD - KoJUGTesting and TDD - KoJUG
Testing and TDD - KoJUGlburdz
 
An introduction to unit testing
An introduction to unit testingAn introduction to unit testing
An introduction to unit testingAdam Stephensen
 
types of testing with descriptions and examples
types of testing with descriptions and examplestypes of testing with descriptions and examples
types of testing with descriptions and examplesMani Deepak Choudhry
 
Unit testing (Exploring the other side as a tester)
Unit testing (Exploring the other side as a tester)Unit testing (Exploring the other side as a tester)
Unit testing (Exploring the other side as a tester)Abhijeet Vaikar
 
UNIT TESTING PPT
UNIT TESTING PPTUNIT TESTING PPT
UNIT TESTING PPTsuhasreddy1
 
Software testing for biginners
Software testing for biginnersSoftware testing for biginners
Software testing for biginnersSriman Eshwar
 
Test driven development
Test driven developmentTest driven development
Test driven developmentnamkha87
 
An insight to test driven development and unit testing
An insight to test driven development and unit testingAn insight to test driven development and unit testing
An insight to test driven development and unit testingDharmendra Prasad
 

Similaire à Unit Testing & TDD Training for Mobile Apps (20)

Unit & integration testing
Unit & integration testingUnit & integration testing
Unit & integration testing
 
Unit testing & TDD concepts with best practice guidelines.
Unit testing & TDD concepts with best practice guidelines.Unit testing & TDD concepts with best practice guidelines.
Unit testing & TDD concepts with best practice guidelines.
 
Unit testing, principles
Unit testing, principlesUnit testing, principles
Unit testing, principles
 
Implementing TDD in for .net Core applications
Implementing TDD in for .net Core applicationsImplementing TDD in for .net Core applications
Implementing TDD in for .net Core applications
 
Unit Testing Full@
Unit Testing Full@Unit Testing Full@
Unit Testing Full@
 
Unit testing basics with NUnit and Visual Studio
Unit testing basics with NUnit and Visual StudioUnit testing basics with NUnit and Visual Studio
Unit testing basics with NUnit and Visual Studio
 
Unit test documentation
Unit test documentationUnit test documentation
Unit test documentation
 
Testing and TDD - KoJUG
Testing and TDD - KoJUGTesting and TDD - KoJUG
Testing and TDD - KoJUG
 
Unit testing basic
Unit testing basicUnit testing basic
Unit testing basic
 
TDD Workshop UTN 2012
TDD Workshop UTN 2012TDD Workshop UTN 2012
TDD Workshop UTN 2012
 
An introduction to unit testing
An introduction to unit testingAn introduction to unit testing
An introduction to unit testing
 
types of testing with descriptions and examples
types of testing with descriptions and examplestypes of testing with descriptions and examples
types of testing with descriptions and examples
 
Test driven development(tdd)
Test driven development(tdd)Test driven development(tdd)
Test driven development(tdd)
 
Unit testing (Exploring the other side as a tester)
Unit testing (Exploring the other side as a tester)Unit testing (Exploring the other side as a tester)
Unit testing (Exploring the other side as a tester)
 
UNIT TESTING PPT
UNIT TESTING PPTUNIT TESTING PPT
UNIT TESTING PPT
 
Unit testing
Unit testingUnit testing
Unit testing
 
Software testing for biginners
Software testing for biginnersSoftware testing for biginners
Software testing for biginners
 
Effective unit testing
Effective unit testingEffective unit testing
Effective unit testing
 
Test driven development
Test driven developmentTest driven development
Test driven development
 
An insight to test driven development and unit testing
An insight to test driven development and unit testingAn insight to test driven development and unit testing
An insight to test driven development and unit testing
 

Dernier

%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrainmasabamasaba
 
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
 
%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
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...SelfMade bd
 
%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
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfkalichargn70th171
 
%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
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdfPearlKirahMaeRagusta1
 
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
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastPapp Krisztián
 
%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
 
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
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesVictorSzoltysek
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplatePresentation.STUDIO
 
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
 
%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
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrandmasabamasaba
 

Dernier (20)

%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
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
 
%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
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
%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
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
%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
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
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
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
%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
 
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
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
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
 
%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
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 

Unit Testing & TDD Training for Mobile Apps

  • 1. Unit Testing & TDD Training For iOS & Android Native Apps
  • 2. Agenda - Part 1: Unit Testing Concepts - Part 2: Test-Driven Development - Part 3: Object Oriented Design for Testing
  • 4. Unit Testing Definition - A unit test is code made to test a portion of our applications code in an automated manner, verifying methods functionality (result or behavior) of a particular class (SUT or System Under Testing).
  • 5. Unit Tests Main Properties - Independent - Running a UT should not affect another UT run. Execution order is not important. - Fast - A test which takes several seconds to run is not a Unit Test and probably needs a refactor. - Isolated - UT should not depend on external systems. They need to know their collaborators answers.
  • 6. Test Class Structure - Class Setup/TearDown - Executed once when the UT class is initialized, not for every test in the class. Useful to avoid test code duplication at class level. - Test Setup/TearDown - Executed once before and after each executed UT. Useful to avoid test code duplication at test level. - Test Methods - The Unit Test implementation itself. Here is where the test run and validations happens.
  • 7. Android Test Structure Example (Java)
  • 8. iOS Test Structure Example (Swift)
  • 9. Tests Naming - Use descriptive test names. It will provide quick feedback when test fails. - Avoid prefix “test” when no required by framework. Android JUnit does not need it, XCTest require it. - Choose names which in few words says: - Method under testing - Condition under testing - Expected result/behavior
  • 10. Tests Naming Examples - Good examples: - Android: getPayments_oneRegisteredPayment_returnsListWithRegisteredPayment - iOS: testListPayments_oneRegisteredPayment_returnsListWithRegisteredPayment - Bad examples: - testCalculateArea2 - testCalculateArea3
  • 11. Test Method Organization - Arrange / Setup - Prepare all needed objects/dependencies for test execution. - Act / Test - Test itself it performed, calling one specific method in the system under testing. - Assert / Verify - Method result/behavior verification is performed at the end, using test asserts.
  • 12. Tests Verification Types - State Verification - Verify the result returned for the method under test, no matter which interactions with other objects has been made. Useful and preferred verification in most cases. - Behavior Verification - Verify that all tested method interactions among other collaboration objects are the expected one (generally done using mocks). Useful for some cases where interactions are important (for example a cache implementation).
  • 13. Android State Verification Example (Java)
  • 14. iOS State Verification Example (Swift)
  • 15. Test Doubles - Dummy - Fake - Stubs - Mocks
  • 16. Test Doubles - Dummy - These are objects used to satisfy class dependencies, but they are not used in the test execution itself. For example Java constructor or Swift init dependencies for class instantiation. - Fake - Objects with functional implementation, but with useful shortcuts for testing. For example an in-memory database, which works but after a test run lost all stored data.
  • 17. Test Doubles - Stubs - Objects with pre-programmed replies. They are not prepared to respond to another thing more than what they were programmed for. Useful in tests for state verifications. - Mocks - Pre-programmed objects with expectations, which are requirements about it use, like: way to be called, invocations amount, invocations parameters. Useful in tests for behavior verifications.
  • 18. Android Behavior Verification Example (Java - Mockito)
  • 19. Android Behavior Verification Example (Java - EasyMock)
  • 20. iOS Behavior Verification Example (Swift)
  • 22. What TDD is? ● It is a software development process driven by tests. ● It consist in first write one or more unit tests of a class yet to be implemented, then write the minimum necessary code to make that test pass. ● Each Unit Test represents a requirement for that class. ● Each Unit Test keeps and evaluates the developer's knowledge about that class over time. ● It avoids unnecessary code complexity not specified by a particular requirement.
  • 23. What TDD is not? ● It is not a testing technique. ● It is not write a class first and then write tests to have a good code coverage. ● It is not functional tests, it must be quickly executed and must not have dependencies with external systems.
  • 24. The TDD process - Step 1 Add a new unit test, that represents a requirement for a method of a class.
  • 25. The TDD process - Step 2 Execute the test and expect it to fail. In cases when the test don't fail, this means that the requirement has been fulfilled before. Go back to step 1 and add a new unit test for a new requirement.
  • 26. The TDD process - Step 3 Implement the method in the class, to make the new unit test successfully pass.
  • 27. The TDD process - Step 4 Execute all the unit test suite, making sure that all the previous test successfully pass. If some tests fail, fix the implementation that make those tests fail. Remember to re execute the test suite every time the code has been changed.
  • 28. The TDD process - Step 5 Improvements and cleaning code, this can be made without worries about breaking something because the test suite gives us immediate feedback if some test fails
  • 29. TDD Process in a nutshell 1. Add a new test 2. Execute the test 3. Add the least amount of code necessary to make that test pass 4. Execute the whole test suite 5. Refactor and clean code
  • 30. Tips ● Any development can be faced using TDD starting with an interface with the signature of the methods to be implemented and tested. ● One single person can be in charge to implement the tests and other person can be in charge to implement the class methods. ● An alternative to avoid the interface is to define the class with its method by simply throwing an exception indicating that the method has not yet been implemented.
  • 31. Object Oriented Design for Testing
  • 32. Dependency Injection - Allows that dependent objects needed in our class implementation can be specified (injected) from outside of that class. - This can be done using constructors/initializers or properties.
  • 33. Dependency Injection - Using Constructor (Java)
  • 34. Dependency Injection - Using Initializer (Swift)
  • 35. Dependency Injection - Using Setter (Java)
  • 36. Dependency Injection - Using Property (Swift)
  • 37. Single Responsibility - Each class should have a single responsibility. - Reduces coupling of different domain functionalities. - Facilitates classes reusage. - The opposite of having Utils/Helper classes. These classes hardly have a unique responsibility.
  • 38. Single Responsibility - Examples - Bad Example: - Good Examples: StringUtil +isValidDate(String) +parseDate(String) +translateString(String) DateParser +isValidDate(String) +parseDate(String) TranslationService +translateString(String)
  • 39. Don’t talk to strangers (1) - An object only should invoke methods of direct known dependencies. - Avoid calling dependencies of our dependencies (strangers). This is avoid chain of methods in code.
  • 40. Don’t talk to strangers (2) - It’s a guide, not a rule. For example this could be avoid in model/entities objects. - Reduces object coupling, improving code maintainability and allows refactor when needed.
  • 41. Don’t talk to strangers - Examples - Bad Examples: Java: this.paymentService.getStorageService().getStoredObject(paymentId) Swift: self.paymentService.storageService.storedObjectWithId(paymentId) - Good Examples: Java: this.paymentService.getPayment(payement) Swift: self.paymentService.paymentWithId(paymentId)
  • 42. Tell don’t ask - Objects should tell another objects to perform actions instead of asking for data and processing the data itself. - Objects should be as lazy as possible and delegate the processing to its dependencies. - Improves class testability and class cohesion.
  • 43. Tell don’t ask - Examples - Bad Example: - Good Example: ShapeService -shapes : List<Shape> +calculateTotalArea() : float Rectangle <Shape> +getHeight() : float +getWidth() : float ShapeService -shapes : List<Shape> +calculateTotalArea() : float Rectangle <Shape> +calculateArea() : float Shape +calculateArea() : float Circle <Shape> +getRadius() : float Circle <Shape> +calculateArea() : float Shape
  • 44. Avoid Singletons - Singleton is a pattern that share a global state in the whole application. - In unit testing, does not allow dependencies control, because it hides class dependencies to the consumer. - Test execution order can affect the result of the test run when using singletons.
  • 45. Avoid Singletons - Examples - Bad Example: - Good Example: PaymentService -storageService +PaymentService() StorageService <<Singleton>> -instance +getInstance() : Storage PaymentService -storageService +PaymentService(storageService) StorageService StorageServiceFactory +getStorageService() : StorageService MyApp -paymentService +execute() MyApp -paymentService - storageService +execute()
  • 47. Thank you so much!