LoginSignup
65

More than 5 years have passed since last update.

SwiftでのUnit Testのはじめかた

Last updated at Posted at 2015-08-16

Swift で書いた Class の Unit Test を書こうとした時にハマったのでメモ

Objective-C での Unit Test

img0.png
テスト対象のクラスがあり、
img2.png
Allow testing Host Application APIsに✔して(デフォルトで✔されている)
img1.png
普通にテストを書いて実行すればよかった。

Swift での Unit Test

img3.png
何も考えずにテストを実行すると、テスト対象クラスが見つからず、Use of unresolve identifier 'LogicA' というコンパイルエラーが出る。
Allow testing Host Application APIsに✔していても関係ない。

Swift1.2 以前

img4.png
テスト対象ファイルの File Inspector でちまちま ${PROJECT_NAME}Tests に✔していくか、

img6.png
「Build Setting > Compile Sources 」から複数ファイルを一気に ${PROJECT_NAME}Tests に含めるか

しかなかった。
でもこの方法だと、特に既存機能のテストを書いていこうとした場合に、
あるファイルの Target に ${PROJECT_NAME}Tests を追加するとその依存する別のファイルが芋づる式に必用になり非常にだるかった。

Swift2 で追加された @testable

img8.png
「Build Settings > Enable Testability」をYESにして

img9.png
@testable import ${PROJECT_NAME}すると、${PROJECT_NAME}Tests をターゲットに追加しなくてもテストが可能になる。


参考URL
- http://natashatherobot.com/swift-2-xcode-7-unit-testing-access/

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
65