program tip

iOS-Swift 용 단위 테스트를 실행할 때 'MyProject-Swift.h'파일을 찾을 수 없음

radiobox 2020. 9. 3. 08:09
반응형

iOS-Swift 용 단위 테스트를 실행할 때 'MyProject-Swift.h'파일을 찾을 수 없음


내 프로젝트에 대해 단위 테스트를 설정하려고합니다. 최근에 하나의 Swift 클래스를 추가 한 기존 Objective-C 앱입니다. 'MyProject-Swift.h'및 Swift Bridging 파일 ( 'MyProject'및 'MyProjectTest'모두)을 설정했으며 Objective-C 및 Swift 코드를 모두 사용하여 앱을 제대로 빌드하고 실행할 수 있습니다.

그러나 이제 새로운 Swift 클래스에서 일부 단위 테스트를 실행하고 싶습니다. 내 테스트 파일을 설정했는데 다음과 같이 보입니다.

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.
        }
    }

}

앱을 테스트로 실행할 때이 오류가 발생합니다.

'MyProject-Swift.h' file not found

왜 이것이 테스트를 실행하려고 할 때만 발생하는지 잘 모르겠습니다. 어떤 제안?


"MyProject-Swift.h" 파일은 다음 경로에 생성됩니다.

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

내 단위 테스트 대상에 대한 헤더 검색 경로에 이것을 추가합니다.

또한 @hyouuu가 알려진 문제에 대해 지적했듯이 Apple이 좋은 솔루션을 제공하기를 바랍니다. 위의 솔루션을 사용해야한다고 생각할 때까지.

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


이것을 알아 낸 @gagarwal에게 감사드립니다. 우리의 경우 제품 이름에 공백이있어서으로 접혀서 $PROJECT_NAME하드 코딩해야했습니다. 또한 $CONFIGURATION_TEMP_DIR대신을 사용 하여 경로에서 $TARGET_TEMP_DIR상위 디렉터리 ( ../)를 제거 할 수 있습니다 . 따라서 해결책은 테스트 대상의 헤더 검색 경로에 다음을 추가하는 것입니다.

"$(CONFIGURATION_TEMP_DIR)/Product Name With Spaces.build/DerivedSources"

또는 제품에 공백이없는 경우 :

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

Xcode 6.1 릴리스 노트에서 이것이 알려진 문제라는 것을 확인했습니다 ... sign ... 릴리스 노트 https://developer.apple.com/library/content/documentation/Xcode 에서 "-swift.h"를 검색합니다. /Conceptual/RN-Xcode-Archive/Chapters/xc6_release_notes.html

Objective-C로 작성된 테스트는 애플리케이션 대상에 대한 Swift 생성 인터페이스 헤더 ($ (PRODUCT_MODULE_NAME) -Swift.h)를 가져올 수 없으므로이 헤더가 필요한 코드를 테스트하는 데 사용할 수 없습니다.

Swift 코드 테스트는 Swift로 작성해야합니다. Objective-C로 작성된 프레임 워크 대상 용 테스트는 @import FrameworkName;을 사용하여 프레임 워크 모듈을 가져 와서 Swift 생성 인터페이스에 액세스 할 수 있습니다. (16931027)

작동하는 @gagarwal의 해결 방법을 참조하십시오!


나는 당신과 비슷한 문제가 있다고 생각합니다. 여기 내 설정이 있습니다.

Swift에 정의 된 객체가 있습니다.

// file Foo.swift
@objc public class Foo {
    // ...
}

This class was then used in the initializer of an Objective-C object:

// file Bar.h
#import "MyProject-Swift.h"

@interface Bar: NSObject

- (instancetype)initWithFoo:(Foo *)foo;

@end

This made my unit tests for Bar not compile, since the MyProject-Swift.h header isn't real and the unit test target can't see it. The release note shared by @hyouuu is on point - but I'm not testing a Swift class, I'm testing an Objective-C class!

I was able to fix this by changing the header file for Bar to use a forward class reference instead:

// file Bar.h
@class Foo;

@interface Bar: NSObject

- (instancetype)initWithFoo:(Foo *)foo;

@end

I then included MyProject-Swift.h in Bar.m, and everything worked - my tests of Objective-C objects written in Objective-C compiled properly and continued running, and I could write new tests for Swift objects in Swift.

Hope this helps!


After I tried out everything I could find on the topic, the thing that worked for me was actually running the app although it was still showing the 'ModuleName-Swift.h file not found' error.

It went away and my app works perfectly fine. I guess I should have considered that earlier... The error keeps coming back, but after running the app it always just goes away again. So the issue is not really solved for me, but I can continue working on other topics for now...


Strangely, I was seeing this same error, but only when targeting a device (not the simulator). Before running the test I would see the red exclamation point next to the import statement for "MyProjectNameTests-Swift.h".

However, funny thing is, if I just go ahead and run the test anyway (despite this apparent build error), then during the build phase that happens thereafter, XCode actually does generate the "MyProjectNameTests-Swift.h" file, and the test runs just fine!

So, at least in my case, there was no need for the other solutions here, evidently, although I believe they do work also.

I should also note that I deleted my DerivedData directory prior to this, so maybe that is a step also worth trying.


A simple

@testable import MyProject

has done the job for me.


mySwiftClassTests (and any other swift classes you want to use in objective-c) needs to be marked @objc:

@objc class MySwiftClassTests: XCTestCase

I couldn't get it to work by adding that filepath mentioned by other answers, but I realized the file where it was complaining wasn't even being tested. I just had to remove it from the test target using the Right Utilities Side Bar.


Adding a .swift file to that target fixes issue on it.

참고URL : https://stackoverflow.com/questions/26473058/ios-myproject-swift-h-file-not-found-when-running-unit-tests-for-swift

반응형