Xcode - 测试类文件是模块的一部分,忽略导入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34889552/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 08:26:49  来源:igfitidea点击:

Xcode - Test class File is part of module, ignoring import

iosxcode

提问by Nuno Gon?alves

So I have an iOS project with tests and everything was working properly.

所以我有一个带有测试的 iOS 项目,并且一切正常。

I felt the need to change the project name. I did as indicated at apple's docs, selected the project and changed its name.

我觉得有必要更改项目名称。我按照苹果文档中的说明做了,选择了项目并更改了名称。

After a while of updating things for this to finally work, the app runs ok, but I'm still having a problem.

经过一段时间的更新以使其最终工作,该应用程序运行正常,但我仍然遇到问题。

I can't run the tests because of this annoying issue. I'm sure it's a basic thing, but still I can't quite figure it out.

由于这个烦人的问题,我无法运行测试。我确定这是一个基本的东西,但我仍然无法弄清楚。

Here's my test class (for the sake of the question I'm using a Fooexample)

这是我的测试课(为了我正在使用的问题的Foo例子)

@testable import MyApp // File FooTests is part of module MyApp. Ignoring import.

class FooTests: QuickSpec {

  override func spec() {

     describe("a foo test") {
         it("tests foo") {
             let return = Foo.barMethod()
             XCTAssertEqual(return, "expected", "Expected something, got \(return) instead")
         }
    }
 }

The Fooclass is not being recognized, and I get the error that the test class is part of MyAppwhich is not the case.

Foo班是不被认可,我也得到了测试类是部分的错误MyApp是不是这样的。

Where should I look for?

我应该去哪里寻找?

回答by Jean-Frederic PLANTE

I just stumbled on the same problem, and your post was helpful. What I found out is by change the Product Nameto AppNameTests, as per your example, then the product module name is reflected correctly and you don't have to add Testsat the end of $(PRODUCT_NAME:c99extidentifier). c99extidentifierseems torefers to Product Name.

我只是偶然发现了同样的问题,你的帖子很有帮助。我发现是通过将 更改Product NameAppNameTests,根据您的示例,然后正确反映产品模块名称并且您不必Tests$(PRODUCT_NAME:c99extidentifier). c99extidentifier似乎是指Product Name

Summary:

概括:

  • select your test target in Project
  • navigate to Build Settings -> Packaging
  • change the Product Nameto your previous test target, likely appending Tests
  • I believe the rename as per Apple's renaming a projectdoesn't include the updating the module in @testable, so I had to do this manually
  • 在 Project 中选择您的测试目标
  • 导航 Build Settings -> Packaging
  • 将 更改Product Name为您之前的测试目标,可能会附加Tests
  • 我相信按照Apple 重命名项目的重命名不包括更新模块中的模块@testable,因此我必须手动执行此操作

回答by Nuno Gon?alves

[UPTADE]This helped Jean-Frederic figuring out his answer. That's why I accepted it.

[UPTADE]这帮助让-弗雷德里克找到了他的答案。这就是为什么我接受了它。

I finally managed to fix this, but I'm not happy with the solution.

我终于设法解决了这个问题,但我对解决方案不满意。

Consider my app name AppName

考虑我的应用名称 AppName

For some weird reason I cannot explain, my test target Build Settings -> Product Module Namehad $(PRODUCT_NAME:c99extidentifier)which value was the Appname, instead of AppNameTests. I checked with other projects and the value is the same but converted to AppNameTestsso in this case I'm not sure what I can do to make it automatically update and include the Test part in the name.

对于一些奇怪的原因,我无法解释,我的测试目标Build Settings -> Product Module Name$(PRODUCT_NAME:c99extidentifier)其价值是Appname,而不是AppNameTests。我检查了其他项目,值是相同的,但转换为AppNameTests所以在这种情况下我不确定我能做些什么来使它自动更新并在名称中包含测试部分。

What I ended up doing was setting the name as $(PRODUCT_NAME:c99extidentifier)Tests. Not sure if this is a proper solution, but for now it works.

我最终做的是将名称设置为$(PRODUCT_NAME:c99extidentifier)Tests. 不确定这是否是一个合适的解决方案,但现在它有效。

If anybody has a better solution, please feel free to share.

如果有人有更好的解决方案,请随时分享。

回答by Duncan Babbage

This is a directly parallel solution to Jean-Frederic Plante, though in my case the problem was not that my test target name matched my primary target, but rather that the module of the test target had incorrectly been set to match the module name of the main app target. To correct either of these issues:

这是 Jean-Frederic Plante 的直接并行解决方案,尽管在我的情况下,问题不是我的测试目标名称与我的主要目标匹配,而是测试目标的模块被错误地设置为匹配模块的模块名称主要应用目标。要更正这些问题之一:

  • Select your test target in your project.
  • Navigate to Build Settings> Packaging.
  • Ensure both the Product Module Nameand Product Nameare set to $(TARGET_NAME).
  • 在您的项目中选择您的测试目标。
  • 导航到构建设置>打包
  • 确保产品模块名称产品名称都设置为$(TARGET_NAME)