SlideShare una empresa de Scribd logo
1 de 31
Unit Test (iOS)
Nguyen Van Dung B, D3
Table of Content
1. What is unit test?.
2. What is testing for?
3. XCTest
4. Mock Object
5. Write stubs
6. Demo
What is unit test ?
Unit tests are small pieces of code that test the
behavior of other code
The goal of unit testing is to isolate each part of the
program and show that the individual parts are
correct
Advantages Of Unit test
Find problems early during develop
You can demonstrate that your code works
Modularity: Unit tests help keep you focused on
writing more modular code.
Focus: Writing tests for micro features keep you
focused on the small details.
Regression: Be sure that the bugs you fixed stay
fixed.
you’ll need unit tests to validate your refactoring.
Unit tests are great way to write better code.
Disadvantages of Unit Tests
More code
More to maintain : When there is more code, there is more to
maintain.
No sliver bullet : In projects with high test coverage it’s possible to
have more test code than functional code.
Takes longer: Writing tests takes time
It still is code. So it can error
A good unit test
Able to be fully automated
Tests a single logical concept in the system
Consistently returns the same result (no random
numbers, save those for integration tests)
Is maintainable and order-independent
Runs fast
Is Readable
Is Trustworthy
A Test is not unit test
talk to the database
Communicates across the network
Touch file system
Can’t not run at the same time as any of your other
unit tests.
You have to do special things to your environment
(such as editing config files) to run it.
Writing testable code
Define api requirements:
Write test cases as you write code
Check boundary conditions
Use negative tests
Write comprehensive test cases
Cover your bug fixs with test cases
When should software be tested ?
When should software be tested ?
XCTest
Xcode’s testing framework
Test case subclasses
Test method
Assertions
Integrated with Xcode
CI via xcode server and xcodebuild
Swift and Objective-C
XCTest Asserts
Xcode Test Navigator
Introduce test navigator
Writing test class and method
Run the test and see the result
XCTest flow
- For each class, testing starts by running class setup method
- For each test method, new instance of the class is allocated
and have same sequence above
- After last test method tearDown in class xcode will execute
class teardown method and move on to next class.
Test class
Subclass of XCTestCase
No header file
Has some common template method
(setUp,TearDown…)
Writing test method
Is an instance method of a test class
Has prefix “test”
No parameters
Return void
Writing test of asysnchronous
operations
Writing Performance Tests
A performance test takes a block of code that you
want to evaluate and runs it ten times, collecting the
average execution time and the standard deviation for
the runs
Using new api from XCTest in xcode 6 and later
Analyzing result
Mocks Object
What is mock object?
Why use Stubs ?
Don’t mock what you don’t own
What is mock object?
a simulated object that mimic the behavior of real
objects in controlled ways.
Create mock object to test the behavior of some other
object.
Has same interface as the real object they mimic
Mocks
Target communicates with the mock object, and all communication
are recorded in the mock. The test uses mock to verify that the test
passes.
Stubs
Returns specified result for a message, stubs can’t fail the test
Why use stubs?
When real object is impractical or impossible to
incorporate into a unit test
If an actual object has any following characteristis, it may
be useful to use a mock object in its place.
Object supplies non-deterministic result (current time,
current temperature)
It has states that are difficult to create or reproduce (network
error …)
It is slow (eg complete datebase,…)
It does not yet exist or may change behavior
it would have to include information and methods
exclusively for testing purposes (and not for its actual task).
Example Mock Object
Writing Stubs
 Fake a response to method calls of an object
 Use to test your code against a web service that isn’t
yet finished
Demo in project
UI Testing
iOS 9/OSX 10.11 or greater
Record steps,
Add assertion check if interface elements are in the
expected state
DEMO in project
About Continuous Integration in Xcode
Automating and streamlining the building, analyzing,
testing, archiving of App.
The goal of continuous integration is to improve
software quality
Catching problems quickly, easily, and early
Enhancing collaboration
Broadening test coverage
Generating build and test statistics over time
Xcode continuous integration
workflow
Xcode Server source code repository
interaction
Xcode Server activity reporting
workflow

Más contenido relacionado

La actualidad más candente

Unit Testing And Mocking
Unit Testing And MockingUnit Testing And Mocking
Unit Testing And Mocking
Joe Wilson
 
Assessing Unit Test Quality
Assessing Unit Test QualityAssessing Unit Test Quality
Assessing Unit Test Quality
guest268ee8
 
Unit testing (workshop)
Unit testing (workshop)Unit testing (workshop)
Unit testing (workshop)
Foyzul Karim
 
Moq presentation
Moq presentationMoq presentation
Moq presentation
LynxStar
 

La actualidad más candente (20)

Unit Testing And Mocking
Unit Testing And MockingUnit Testing And Mocking
Unit Testing And Mocking
 
Assessing Unit Test Quality
Assessing Unit Test QualityAssessing Unit Test Quality
Assessing Unit Test Quality
 
Embrace Unit Testing
Embrace Unit TestingEmbrace Unit Testing
Embrace Unit Testing
 
An Introduction to Unit Testing
An Introduction to Unit TestingAn Introduction to Unit Testing
An Introduction to Unit Testing
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
 
Unit Testing 101
Unit Testing 101Unit Testing 101
Unit Testing 101
 
Unit testing with NUnit
Unit testing with NUnitUnit testing with NUnit
Unit testing with NUnit
 
Unit testing
Unit testing Unit testing
Unit testing
 
Unit testing - the hard parts
Unit testing - the hard partsUnit testing - the hard parts
Unit testing - the hard parts
 
Unit testing (workshop)
Unit testing (workshop)Unit testing (workshop)
Unit testing (workshop)
 
How and what to unit test
How and what to unit testHow and what to unit test
How and what to unit test
 
Moq presentation
Moq presentationMoq presentation
Moq 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 Testing Concepts and Best Practices
Unit Testing Concepts and Best PracticesUnit Testing Concepts and Best Practices
Unit Testing Concepts and Best Practices
 
N Unit Presentation
N Unit PresentationN Unit Presentation
N Unit Presentation
 
Introduction to testing with MSTest, Visual Studio, and Team Foundation Serve...
Introduction to testing with MSTest, Visual Studio, and Team Foundation Serve...Introduction to testing with MSTest, Visual Studio, and Team Foundation Serve...
Introduction to testing with MSTest, Visual Studio, and Team Foundation Serve...
 
Unit Testing (C#)
Unit Testing (C#)Unit Testing (C#)
Unit Testing (C#)
 
Working Effectively with Legacy Code
Working Effectively with Legacy CodeWorking Effectively with Legacy Code
Working Effectively with Legacy Code
 
Mock driven development using .NET
Mock driven development using .NETMock driven development using .NET
Mock driven development using .NET
 
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
 

Similar a Nguyenvandungb seminar

Unit Testing Full@
Unit Testing Full@Unit Testing Full@
Unit Testing Full@
Alex Borsuk
 

Similar a Nguyenvandungb seminar (20)

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
 
Unit Testing Full@
Unit Testing Full@Unit Testing Full@
Unit Testing Full@
 
TDD Workshop UTN 2012
TDD Workshop UTN 2012TDD Workshop UTN 2012
TDD Workshop UTN 2012
 
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, principles
Unit testing, principlesUnit testing, principles
Unit testing, principles
 
Integration and Unit Testing in Java using Test Doubles like mocks and stubs
Integration and Unit Testing in Java using Test Doubles like mocks and stubsIntegration and Unit Testing in Java using Test Doubles like mocks and stubs
Integration and Unit Testing in Java using Test Doubles like mocks and stubs
 
An introduction to unit testing
An introduction to unit testingAn introduction to unit testing
An introduction to unit testing
 
Presentation
PresentationPresentation
Presentation
 
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.
 
Testing in Craft CMS
Testing in Craft CMSTesting in Craft CMS
Testing in Craft CMS
 
Test driven development
Test driven developmentTest driven development
Test driven development
 
Testing 101
Testing 101Testing 101
Testing 101
 
Testing the untestable
Testing the untestableTesting the untestable
Testing the untestable
 
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
 
An Introduction to Test Driven Development
An Introduction to Test Driven Development An Introduction to Test Driven Development
An Introduction to Test Driven Development
 
TDD Best Practices
TDD Best PracticesTDD Best Practices
TDD Best Practices
 
Effective unit testing
Effective unit testingEffective unit testing
Effective unit testing
 
Win at life with unit testing
Win at life with unit testingWin at life with unit testing
Win at life with unit testing
 

Último

Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
chiefasafspells
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
masabamasaba
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
VictoriaMetrics
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 

Último (20)

Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
%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
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
 
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 ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
WSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaS
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 

Nguyenvandungb seminar

  • 1. Unit Test (iOS) Nguyen Van Dung B, D3
  • 2. Table of Content 1. What is unit test?. 2. What is testing for? 3. XCTest 4. Mock Object 5. Write stubs 6. Demo
  • 3. What is unit test ? Unit tests are small pieces of code that test the behavior of other code The goal of unit testing is to isolate each part of the program and show that the individual parts are correct
  • 4. Advantages Of Unit test Find problems early during develop You can demonstrate that your code works Modularity: Unit tests help keep you focused on writing more modular code. Focus: Writing tests for micro features keep you focused on the small details. Regression: Be sure that the bugs you fixed stay fixed. you’ll need unit tests to validate your refactoring. Unit tests are great way to write better code.
  • 5. Disadvantages of Unit Tests More code More to maintain : When there is more code, there is more to maintain. No sliver bullet : In projects with high test coverage it’s possible to have more test code than functional code. Takes longer: Writing tests takes time It still is code. So it can error
  • 6. A good unit test Able to be fully automated Tests a single logical concept in the system Consistently returns the same result (no random numbers, save those for integration tests) Is maintainable and order-independent Runs fast Is Readable Is Trustworthy
  • 7. A Test is not unit test talk to the database Communicates across the network Touch file system Can’t not run at the same time as any of your other unit tests. You have to do special things to your environment (such as editing config files) to run it.
  • 8. Writing testable code Define api requirements: Write test cases as you write code Check boundary conditions Use negative tests Write comprehensive test cases Cover your bug fixs with test cases
  • 9. When should software be tested ?
  • 10. When should software be tested ?
  • 11. XCTest Xcode’s testing framework Test case subclasses Test method Assertions Integrated with Xcode CI via xcode server and xcodebuild Swift and Objective-C
  • 13. Xcode Test Navigator Introduce test navigator Writing test class and method Run the test and see the result
  • 14. XCTest flow - For each class, testing starts by running class setup method - For each test method, new instance of the class is allocated and have same sequence above - After last test method tearDown in class xcode will execute class teardown method and move on to next class.
  • 15. Test class Subclass of XCTestCase No header file Has some common template method (setUp,TearDown…)
  • 16. Writing test method Is an instance method of a test class Has prefix “test” No parameters Return void
  • 17. Writing test of asysnchronous operations
  • 18. Writing Performance Tests A performance test takes a block of code that you want to evaluate and runs it ten times, collecting the average execution time and the standard deviation for the runs Using new api from XCTest in xcode 6 and later
  • 20. Mocks Object What is mock object? Why use Stubs ? Don’t mock what you don’t own
  • 21. What is mock object? a simulated object that mimic the behavior of real objects in controlled ways. Create mock object to test the behavior of some other object. Has same interface as the real object they mimic
  • 22. Mocks Target communicates with the mock object, and all communication are recorded in the mock. The test uses mock to verify that the test passes.
  • 23. Stubs Returns specified result for a message, stubs can’t fail the test
  • 24. Why use stubs? When real object is impractical or impossible to incorporate into a unit test If an actual object has any following characteristis, it may be useful to use a mock object in its place. Object supplies non-deterministic result (current time, current temperature) It has states that are difficult to create or reproduce (network error …) It is slow (eg complete datebase,…) It does not yet exist or may change behavior it would have to include information and methods exclusively for testing purposes (and not for its actual task).
  • 26. Writing Stubs  Fake a response to method calls of an object  Use to test your code against a web service that isn’t yet finished Demo in project
  • 27. UI Testing iOS 9/OSX 10.11 or greater Record steps, Add assertion check if interface elements are in the expected state DEMO in project
  • 28. About Continuous Integration in Xcode Automating and streamlining the building, analyzing, testing, archiving of App. The goal of continuous integration is to improve software quality Catching problems quickly, easily, and early Enhancing collaboration Broadening test coverage Generating build and test statistics over time
  • 30. Xcode Server source code repository interaction
  • 31. Xcode Server activity reporting workflow