テストフィクスチャtips(主にcoredata) #potatotips

17
テストフィクスチャTips (主にCoreData2014.03.12 potatotips#5 at COOKPAD @nowsprinting/Koji Hasegawa 14313日木曜日

Upload: koji-hasegawa

Post on 24-Jun-2015

1.753 views

Category:

Documents


3 download

DESCRIPTION

potatotips#5 at COOKPAD LT

TRANSCRIPT

Page 1: テストフィクスチャTips(主にCoreData) #potatotips

テストフィクスチャTips

(主にCoreData)

2014.03.12 potatotips#5 at COOKPAD

@nowsprinting/Koji Hasegawa14年3月13日木曜日

Page 2: テストフィクスチャTips(主にCoreData) #potatotips

自己紹介• @nowsprinting/Koji Hasegawa

• フリーランス(iOS/Androidアプリ受託開発)

• テスト自動化研究会、Androidテスト部

• 著書『Androidアプリテスト技法』(共著)

『iOSアプリ テスト自動化入門』3.18発売

14年3月13日木曜日

Page 3: テストフィクスチャTips(主にCoreData) #potatotips

都内の大きな書店では今週末から並ぶかも?

3.18 発売!

14年3月13日木曜日

Page 4: テストフィクスチャTips(主にCoreData) #potatotips

Agenda

• テストフィクスチャとは• CoreDataをインメモリストアで使用する

• アプリケーションデータを再現してテスト実行する

14年3月13日木曜日

Page 5: テストフィクスチャTips(主にCoreData) #potatotips

テストフィクスチャとは

14年3月13日木曜日

Page 6: テストフィクスチャTips(主にCoreData) #potatotips

テストフィクスチャとは• テスト実行環境、前提条件の集合• コンテキスト• [setUp]やテストメソッドの中で整え、

[tearDown]でクリアする

• テストの再現性、独立性を確保

14年3月13日木曜日

Page 7: テストフィクスチャTips(主にCoreData) #potatotips

再現性

• そのテストケースを何度実行しても、いつ実行しても検証結果が変わらない

独立性

• 他のテストケースの実行結果に依存しない

14年3月13日木曜日

Page 8: テストフィクスチャTips(主にCoreData) #potatotips

CoreDataをインメモリストアで使用する

14年3月13日木曜日

Page 9: テストフィクスチャTips(主にCoreData) #potatotips

インメモリストアとは

• アプリインストール先のストレージではなく、メモリ上にDBを作る

• テスト実行ごとに、常に空のDBを得ることができる

• I/Oが発生しないので高速(のはず)

14年3月13日木曜日

Page 10: テストフィクスチャTips(主にCoreData) #potatotips

CoreDataの初期化 - (void)setUp { NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"YourModelName" withExtension:@"momd"]; _model = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL]; _coordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:_model];

NSString *docpath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; NSURL *storeURL = [NSURL fileURLWithPath:[docpath stringByAppendingPathComponent: @"YourModelName.sqlite"]];

_store = [_coordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:NULL];

_context = [[NSManagedObjectContext alloc] init]; _context.persistentStoreCoordinator = _coordinator; }

14年3月13日木曜日

Page 11: テストフィクスチャTips(主にCoreData) #potatotips

インメモリストアを使う _store = [_coordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:NULL];

_store = [_coordinator addPersistentStoreWithType:NSInMemoryStoreType configuration:nil URL:nil options:nil error:NULL];

14年3月13日木曜日

Page 12: テストフィクスチャTips(主にCoreData) #potatotips

アプリケーションデータを再現してテスト実行する

14年3月13日木曜日

Page 13: テストフィクスチャTips(主にCoreData) #potatotips

• 実際にアプリを操作して得られたアプリケーションデータを保存し、テスト実行ごとに再現する

• 大量データや、DBのマイグレーションのテストなどで有効

14年3月13日木曜日

Page 14: テストフィクスチャTips(主にCoreData) #potatotips

実機のデータを取り出す

※iOSシミュレータではFinderで取り出すことができます

拡張子”.xcappdata”ファイルとして保存

14年3月13日木曜日

Page 15: テストフィクスチャTips(主にCoreData) #potatotips

データを設定して実行

Scheme設定画面でxcappdataを設定14年3月13日木曜日

Page 16: テストフィクスチャTips(主にCoreData) #potatotips

その他、フィクスチャ系

• テストフィクスチャをファイルでバンドルする

• NSUserDefaultsをクリアする

方法が『iOSアプリ テスト自動化入門』には書いてあります

14年3月13日木曜日

Page 17: テストフィクスチャTips(主にCoreData) #potatotips

14年3月13日木曜日