LoginSignup
15
13

More than 3 years have passed since last update.

XCTestテストにおけるsetup, teardownの便利なクラス

Last updated at Posted at 2016-06-15

2017/8/1追記

class func initialize()がdepricatedになりそうなのでちゃんとしたやつ

テストBundleのInfo.plistに

<key>NSPrincipalClass</key>
<string>Bundle名.クラス名(ここでいうTestSetup)</string>

TestSetup.swift

class TestSetup:NSObject,XCTestObservation {

    override init() {
        super.init()
        XCTestObservationCenter.shared().addTestObserver(self)
    }

    /// TestBundle開始時
    func testBundleWillStart(_ testBundle: Bundle) {
    }

    /**
     毎テストケース開始前
     */
    func testCaseWillStart(_ testCase: XCTestCase) {
    }
}

もっとすごい


あるとすごい便利
DBのセットアップとか最初にしておきたいとか

import XCTest

// ゴニョゴニョクラス
class TestSetup: XCTestCase,XCTestObservation  {
    override class func  initialize() {
        let me = TestSetup()
        let center = XCTestObservationCenter.sharedTestObservationCenter()
        center.addTestObserver(me)
    }

    func testBundleWillStart(testBundle: NSBundle) {
        //テスト開始前
    }
    func testBundleDidFinish(testBundle: NSBundle) {
        // テスト終了後
    }
    func testSuiteWillStart(testSuite: XCTestSuite) {
        // 各スイート開始前
    }

    func testSuiteDidFinish(testSuite: XCTestSuite) {
        // 各スイート終了後
    }
    func testCaseWillStart(testCase: XCTestCase) {
        // 各ケース開始前
    }
    func testCaseDidFinish(testCase: XCTestCase) {
        // 各ケース終了後
    }
}


すごい

15
13
2

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
15
13