Upgrade to Pro — share decks privately, control downloads, hide ads and more …

テスト実行時に 不要な初期化コードを実行しないようにする

kNagadou
February 22, 2018

テスト実行時に 不要な初期化コードを実行しないようにする

potatotips#48での発表スライドです

kNagadou

February 22, 2018
Tweet

More Decks by kNagadou

Other Decks in Programming

Transcript

  1. ςετ࣮ߦ࣌ʹ
    ෆཁͳॳظԽίʔυΛ࣮ߦ͠
    ͳ͍Α͏ʹ͢Δ
    ,":"$ٕज़෦
    ,B[VNBTB/"("%0
    QPUBUPUJQT

    View full-size slide

  2. ࣗݾ঺հ
    w ,B[VNBTB/"("%0
    w ,":"$ೖࣾϲ݄
    w ʢ౔ʣ/1$+ϑΟδʔΫ
    ϊʔϏεΫϥεग़৔༧ఆ
    w 5XJUUFS!L[NTOHE

    View full-size slide

  3. ϓϥίϨ

    ϓϥίϨ8FEEJOH
    IUUQTQMBDPMFXFEEJOH
    %SFTTZCZϓϥίϨ
    IUUQTESFTTZQMBDPMFXFEEJOH

    View full-size slide

  4. ΞϓϦىಈ࣌ʹॳظσʔλΛߏங͢Δ͜ͱ͕͋Δ
    ྫʣςετ༻ͷϢʔβάϧʔϓ
    ؀ڥݻ༗ͷม਺
    ֎෦4%,ͷηοτΞοϓ
    ͳͲ

    View full-size slide

  5. ᶃΞϓϦͷॳظԽॲཧͳͲͰ.PEFMͷৼΔ෣͍͕มΘΒͳ͍Α͏ʹ͍ͨ͠
    ॳظԽॲཧ͸ɺ"QQ%FMFHBUFʹॻ͘͜ͱ͕ଟ͍ɻ
    ςετ࣌͸"QQ%FMFHBUFͷॲཧΛ࣮ߦ͠ͳ͍Α͏ʹ͍ͨ͠

    View full-size slide

  6. ᶄ7JFX΍7JFX$POUSPMMFSͷΠϯελϯε͸ෆཁ

    σϑΥϧτͩͱɺ6OJUςετ࣌΋6*͕ϩʔυ͞ΕΔ
    .BJO*OUFSGBDFͷϩʔυ͸ίʔυͰ΋Ͱ͖Δ

    View full-size slide

  7. ᶅςετ༻ͷίʔυ͕"QQ%FMFHBUF಺ʹଘࡏͯ͠͠·͏
    let isRunningTest =
    ProcessInfo.processInfo.environment["XCInjectBundleInto"] != nil
    if isRunningTest {
    // ςετ࣌ͷॳظԽॲཧ
    } else {
    // ϓϩμΫγϣϯ༻ͷॳظԽॲཧ
    }
    ςετ༻ͷॳظԽॲཧ͸ผͷ৔ॴʹ෼͚͍ͨ

    View full-size slide

  8. ςετ͕࣮ߦ͞Ε͍ͯΔ͔൑ఆ
    ςετ࣮ߦ࣌͸5FTU"QQ%FMFHBUFΛ࢖༻ɺͦ͏͡Όͳ͍ͳΒ
    "QQ%FMFHBUFΛ࢖༻
    .BJO*OUFSGBDFͷϩʔυ͸ɺϓϩμΫγϣϯ༻ͷ"QQ%FMFHBUFͰ
    ॲཧ
    ɹʢςετ༻ͷ5FTU"QQ%FMFHBUF͸ɺ.BJO*OUFSGBDFΛϩʔυ͠ͳ͍ʣ
    ͱ͢Ε͹ྑͦ͞͏ɻ

    View full-size slide


  9. 5FTU"QQ%FMFHBUFͱ"QQ%FMFHBUFΛ࢖͍෼͚ΒΕΔΑ͏ʹɺςετ࣮ߦ൑ఆΛ
    NBJOؔ਺ʹॻ͘ɻ
    4XJGUͷNBJOؔ਺͸Ͳ͜ʹ͋Δͷ͔ʁ
    ࣮͸ɺ҉໧తʹએݴ͞Ε͓ͯΓɺNBJOTXJGUΛϓϩδΣΫτʹ௥Ճ͢Δ͜ͱͰ
    NBJOؔ਺తͳॲཧΛॻ͚Δɻ

    View full-size slide

  10. import UIKit
    let isRunningTest =
    ProcessInfo.processInfo.environment["XCInjectBundleInto"] != nil
    let appDelegateClass = isRunningTest ?
    NSStringFromClass(TestAppDelegate.self) :
    NSStringFromClass(AppDelegate.self)
    let args = UnsafeMutableRawPointer(CommandLine.unsafeArgv).bindMemory(to:
    UnsafeMutablePointer.self, capacity: Int(CommandLine.argc))
    UIApplicationMain(CommandLine.argc, args, nil, appDelegateClass)

    NBJOTXJGUΛϓϩδΣΫτʹ௥Ճɻ
    ςετ࣮ߦதͳΒɺ5FTU"QQ%FMFHBUFΛUIApplicationMain(_,_,_,_)ͷҾ਺
    ʹ౉͢ॲཧΛॻ͘ɻ

    View full-size slide

  11. <5FTU"QQ%FMFHBUFTXJGU>
    5FTU"QQ%FMFHBUF͸ɺͱΓ͋͑ͣॲཧͳ͠Ͱ΋0,
    import UIKit
    class TestAppDelegate: UIResponder, UIApplicationDelegate {
    }

    View full-size slide

  12. <"QQ%FMFHBUFTXJGU>
    .BJO*OUFSGBDFΛϩʔυ͢ΔίʔυΛॻ͘

    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    window = UIWindow()
    window?.rootViewController = storyboard.instantiateInitialViewController()
    window?.makeKeyAndVisible()

    View full-size slide

  13. 1SPKFDU(FOFSBM%FQMPZNFOU*OGPʹ͋Δ.BJO
    *OUFSGBDFΛϒϥϯΫʢۭനʣʹ͢Δɻ

    ·ͨ͸ɺJOGPQMJTUͷ6*.BJO4USPZCPBSE'JMFΛ࡟আɻ

    View full-size slide

  14. <"QQ%FMFHBUFTXJGU>
    ๨Εͣʹɺ"QQ%FMFHBUFTXJGU͔Β@UIApplicationMainΛ࡟আɻ
    ʢUIApplicationMain(_,_,_,_)ΛݺΜͰɺσϦήʔτΫϥεΛ౉͢
    "UUSJCVUFʣ

    // @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate {

    View full-size slide

  15. ·ͱΊ
    NBJOTXJGUͰςετ࣮ߦதͷ൑ఆΛͯ͠ɺ5FTU"QQ%FMFHBUFΛ࢖͏
    ςετ࣮ߦ࣌͸.BJO*OUFSGBDF΋ϩʔυͨ͘͠ͳ͍ͷͰɺJOGPQMJTU͔Β࡟

    ˠϓϩμΫγϣϯ༻ͷ"QQ%FMFHBUFͰϩʔυ͢ΔΑ͏ʹ͢Δ

    View full-size slide

  16. ͋Γ͕ͱ͏͍͟͝·ͨ͠

    View full-size slide

  17. ςετ࣮ߦதͷ൑ఆํ๏ʹ͍ͭͯ

    let isRunningTest = ProcessInfo.processInfo.environment["XCInjectBundleInto"] !
    = nil
    // or
    let isRunningTest = NSClassFromString("XCTestCase") != nil

    View full-size slide

  18. ͲͪΒͰ൑ఆͨ͠ํ͕Α͍͔ʁ
    • ProcessInfo.processInfo.environment[“XCInjectBundleInto"]
    xcodebuild test࣮ߦ࣌ʹඞͣଘࡏ͢Δ؀ڥม਺
    • NSClassFromString(“XCTestCase")
    XCTestCase͕ଘࡏ͠ͳ͍؀ڥͩͱɺਖ਼͘͠൑ఆͰ͖ͳ͍

    View full-size slide

  19. ςετ͕ಋೖ͞Ε͍ͯͳ͍ΞϓϦΛϦϑΝΫλ
    ˣ
    .PEFMͷ6OJUςετΛॻ͘
    ˣ
    ෆཁͳॳظԽίʔυΛ࣮ߦ͠ͳ͍Α͏ʹͨ͠

    View full-size slide

  20. ࢀߟ
    <>'BLF"QQ%FMFHBUF'PS6OJU5FTUJOH*O4XJGU
    IUUQTNBSDPTBOUBEFWDPNGBLFBQQEFMFHBUFVOJUUFTUJOHTXJGU
    <>)PXUP&BTJMZ4XJUDI:PVS"QQ%FMFHBUFGPS5FTUJOH
    IUUQTRVBMJUZDPEJOHPSHBQQEFMFHBUFGPSUFTUT
    <>9DPEFͰςετ࣮ߦ࣌ʹ"QQ%FMFHBUFΛ෼͚Δ
    IUUQTRJJUBDPNLFZJUFNTEBCCFBFDE

    View full-size slide