IOS-“ MyProject-Swift.h”文件在运行 Swift 单元测试时找不到

我正在尝试为我的项目设置单元测试。 这是一个现有的 Objective-C 应用程序,我最近添加了一个 Swift 类。我已经设置了“ MyProject-Swift.h”和 Swift 桥接文件(包括“ MyProject”和“ MyProjectTest”) ,我能够使用 Objective-C 和 Swift 代码构建和运行这个应用程序。

但是,现在我想在新的 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

我不确定为什么只有在尝试运行测试时才会发生这种情况。 有什么建议吗?

25599 次浏览

Saw in the Xcode 6.1 release note, that this is a known issue... sign... Search for "-swift.h" in the release note https://developer.apple.com/library/content/documentation/Xcode/Conceptual/RN-Xcode-Archive/Chapters/xc6_release_notes.html

Tests written in Objective-C cannot import the Swift generated interfaces header ($(PRODUCT_MODULE_NAME)-Swift.h) for application targets, and therefore cannot be used to test code that requires this header.

Tests for Swift code should be written in Swift. Tests written in Objective-C for framework targets can access the Swift generated interfaces by importing the framework module using @import FrameworkName;. (16931027)

Please see @gagarwal's workaround below which WORKS!

I had a similar issue to yours, I think; here was my setup.

I had an object defined in 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!

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

@objc class MySwiftClassTests: XCTestCase

"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

Thanks to @gagarwal for figuring this out. In our case the product name has a space, which is collapsed in $PROJECT_NAME, so I had to hard code it. Additionally, by using $CONFIGURATION_TEMP_DIR instead of $TARGET_TEMP_DIR, you can remove the parent directory (../) from the path. So the solution is to add the following to the Header Search Paths in your test target:

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

Or, if your product does not contain spaces:

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

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.

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.

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

A simple

@testable import MyProject

has done the job for me.

On one of my clients project they added frameworks directly in the project. I solved the errors like this;

  • Command 1
  • option command J
  • now type the framework name (that gives the error) in the filter
  • select the framework
  • option command 1
  • in Target Membership select your *UITests target