iOS - 'MyProject-Swift.h' file not found when running Unit Tests for Swift

JimmyJammed picture JimmyJammed · Oct 20, 2014 · Viewed 22.2k times · Source

I am trying to setup Unit Testing for my project. It is an existing Objective-C app, that I have recently added one Swift class to. I have setup the 'MyProject-Swift.h' and Swift Bridging files (both 'MyProject' and 'MyProjectTest') and I am able to build and run the app just fine using both Objective-C and Swift code.

However, now I want to run some Unit Tests on the new Swift class. I setup my test file and it looks like the following:

MySwiftClassTests.swift:

import UIKit
import XCTest
import MyProject

class MySwiftClassTests: XCTestCase {

    override func setUp() {
        super.setUp()
        // Put setup code here. This method is called before the invocation of each test method in the class.
    }

    override func tearDown() {
        // Put teardown code here. This method is called after the invocation of each test method in the class.
        super.tearDown()
    }

    func testExample() {
        // This is an example of a functional test case.
        XCTAssert(true, "Pass")
    }

    func testPerformanceExample() {
        // This is an example of a performance test case.
        self.measureBlock() {
            // Put the code you want to measure the time of here.
        }
    }

}

I get this error when running the app as Test:

'MyProject-Swift.h' file not found

I am not sure why this happens only when trying to run the Tests. Any suggestions?

Answer

gagarwal picture gagarwal · Jan 25, 2015

"MyProject-Swift.h" file is generated at following path:

"$(TARGET_TEMP_DIR)/../$(PROJECT_NAME).build/DerivedSources"

I end up adding this to Header Search Paths for my Unit Test target.

Also as @hyouuu pointed out about being the known issue, hopefully Apple will provide some good solution at their end. Until I believe we need to use this above solution.

https://developer.apple.com/library/content/documentation/Xcode/Conceptual/RN-Xcode-Archive/Chapters/xc6_release_notes.html