unit test

43
Unit Test Đức Trần

Upload: tran-duc

Post on 16-Feb-2017

236 views

Category:

Mobile


2 download

TRANSCRIPT

Unit TestĐức Trần

Unit TestWhy Testing?

Beginning Unit Test

Asynchronous Testing

Performance Testing

Why Testing?Find bug

Clear requirement

Maintenance

How?1.Code

2.Add Tests

3.Modify code for passing Tests

Vs

4.Write Tests

5.Write code that pass Tests (Test-Driven Development)

Beginning Unit Test

Beginning Unit TestXCTest

Test Target

Running Test

Viewing Result

XCTestSubclass XCTestCase

Implement Test method

(Beginning with “test”)

- (void)testMyMethodWorks

Use Assertion (Macro) APIs to report failures

- XCTAssertEqual(value, expectValue);

Test TargetTest Target:

- Test code

- Resources

Automatically included in new project

Existing projects can add test targets

Running TestCommand+U

Buttons in source editor sidebar

Buttons in Test Navigator

Viewing ResultTest Navigator

Viewing ResultTest Navigator

Issue Navigator

Viewing ResultTest Navigator

Issue Navigator

Source Editor

Viewing ResultTest Navigator

Issue Navigator

Source Editor

Test Report

DemoAdding Test Case

Prepare “fake” data

Create Model

Create Service

Write Test Case

Implement MethodWrite code to implement methods.

Check if test cases are passed

Asynchronous Testing

Asynchronous TestingCreate Expectation Object:

- XCTestExpectation *expectation = [self expectationWithDescription:@"Load Data"];

XCTestCase waits for Expectations to “Fullfill”- (void)waitForExpectationsWithTimeout:(NSTimeInterval)timeout handler:(nullable

XCWaitCompletionHandler)handler;

Handle when finishing asynchronous tasks:- [expectation fulfill];

DemoAsynchronous Testing

Add Asynchronous method

Write Test case

Implement methodWrite code to implement methods.

Check if test cases are passed

Performance Testing

Performance TestingHow to measure app performance?

How to find which part causes performance problem?

=> Performance Testing.

Measuring PerformanceTest code block:

- (void)measureBlock:(void (^)(void))block;

Runs code block 10 time

Measures time

Results show in Xcode

Performance Testing Setting Baselines

Setting Standard Deviation (STDDEV)

Setting BaselinesBaselines is Average run time

Detect Regressions:

- Fail if (Baselines Average)>0.1 seconds and >10%

Baselines are stored in sources

Baselines are per-device configuration:

- Device model, CPU, OS

Setting BaselineNo Baseline Set Baseline

Setting BaselinesNormal case (Baseline 1s) Error Case (Baseline 1s)

Setting BaselineBaselines are not enough

Setting Standard Deviation (STDDEV)Fail if (STDDEV)>0.1 seconds and >10%

Standard DeviationWhy STDDEV too big?

- File I/O, Network I/O?

- Initialization?

- Does it do the same work each time it's called?

- System busy?

Profiling TestInvestigate functions’ performance

DemoPerformance Testing

Performance Test

SummaryWhy Testing?

Beginning Unit Test

Asynchronous Testing

Performance Testing

QA?1.How to implement UITest?

- Checkout apple’s Sample app: https://developer.apple.com/library/mac/samplecode/UnitTests/History/History.html#//apple_ref/doc/uid/DTS40011742-RevisionHistory-DontLinkElementID_1

- Checkout this WWDC session: https://developer.apple.com/videos/play/wwdc2015/406/

AssignmentWrite a sample app using UnitTest

- Which test cases should we have to cover?

Thanks for watching!