Unit Test (iOS)
Bài đăng này đã không được cập nhật trong 8 năm
http://www.slideshare.net/dunglinh111/nguyenvandungb-seminar
- 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
All rights reserved