【发布时间】:2014-12-15 20:56:44
【问题描述】:
我正在尝试为我的项目设置单元测试。 这是一个现有的 Objective-C 应用程序,我最近添加了一个 Swift 类。我已经设置了“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
我不确定为什么只有在尝试运行测试时才会发生这种情况。 有什么建议吗?
【问题讨论】:
-
添加更新?我也面临同样的问题...
-
@Coveloper - 您如何设置“-Swift.h”文件的目标?它不是项目中的真实文件,而是由 Xcode 在构建时编译的。
-
这是对我的“我也得到 'MyProject-Swift.h' 文件未找到错误..”的更新:我通过将 MyProjectTests 目标的产品模块名称设置为 MyProject 找到了一种解决方法反对 MyProjectTests。因此,现在两个目标(MyProject 和 MyProjectTests)具有相同的产品模块名称。这很奇怪,但它是有效的,而且风险很低,因为它是测试目标。我应该提到我的项目名称实际上就像 My-Project 因此 My_Project 是实际的模块名称。)
-
“MyProject-Swift.h”文件在“$(TARGET_TEMP_DIR)/../$(PROJECT_NAME).build/DerivedSources”中生成。我最终将此添加到我的单元测试目标的标题搜索路径中。
-
@gagarwal 将“$(TARGET_TEMP_DIR)/../$(PROJECT_NAME).build/DerivedSources”添加到我的单元测试目标的标题搜索路径中有效。 :) 从该评论中做出一个 SO 答案,我可以奖励您赏金,并让其他人清楚这是一个多么棒的答案。
标签: ios unit-testing swift header