This document discusses unit testing in iOS. It defines unit testing as small pieces of code that test individual parts of a program to show they are correct. Key advantages are finding problems early, demonstrating code works, and ensuring regressions don't reoccur. The document covers the XCTest framework, writing testable code, mocks and stubs. It provides examples of writing unit tests in Xcode including for asynchronous operations and performance. Continuous integration with Xcode Server is also discussed.
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
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
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
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
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.
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