ios 在 Xcode 单元测试中使用 @testable 时“没有这样的模块”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32008403/
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
"No such module" when using @testable in Xcode Unit tests
提问by Suragch
I recently updated to Xcode 7 beta 5. I tried adding a unit test to an earlier project, but I am getting the error message "No such module [myModuleName]" on the @testable import myModuleName
line.
我最近更新到 Xcode 7 beta 5。我尝试向早期项目添加单元测试,但我收到错误消息“没有这样的模块 [myModuleName]” @testable import myModuleName
。
I tried
我试过
- cleaning the project with OptionClean Build Folder
- checking that "Enable Testability" (debug) was set to Yes in the Build Options
- deleting the tests target and then re-adding the iOS Unit testing bundle
- 使用OptionClean Build Folder清理项目
- 检查“启用可测试性”(调试)在“构建选项”中是否设置为“是”
- 删除测试目标,然后重新添加 iOS 单元测试包
None of this worked for this project (but I have gotten testing to work in another project). Has anyone else had this problem and solved it?
这些都不适用于这个项目(但我已经在另一个项目中进行了测试)。有没有其他人遇到过这个问题并解决了它?
采纳答案by Suragch
The answer that worked for me
对我有用的答案
The answer was that I had some errors in my project that was making the build fail. (It was just your standard every day bug in the code.) After I fixed the errors and did another clean and build, it worked.
答案是我的项目中有一些错误导致构建失败。(这只是代码中每天的标准错误。)在我修复了错误并进行了另一次清理和构建之后,它起作用了。
Note that these errors didn't show up at first. To get them to show up:
请注意,这些错误一开始并没有出现。要让他们出现:
- Comment out your entire Test file that is giving you the "No such module" error.
- Try to run your project again.
- 注释掉给你“没有这样的模块”错误的整个测试文件。
- 尝试再次运行您的项目。
If there are other errors, they should show up now. Fix them and then uncomment your Test file code. The "No such module" error was gone for me.
如果还有其他错误,它们现在应该显示出来。修复它们,然后取消注释您的测试文件代码。“没有这样的模块”错误对我来说消失了。
In case this doesn't solve the problem for other people, you can also try the following:
如果这不能解决其他人的问题,您还可以尝试以下操作:
Clean the build folder
清理构建文件夹
Open the Product menu, hold down Option, and click "Clean Build Folder..."
打开产品菜单,按住Option,然后单击“清理构建文件夹...”
Make sure that Enable Testability is set to Yes
确保启用可测试性设置为是
In the Project Navigator click your project name. Select Build Settings and scroll down to Build Options. Make sure that Enable Testability is Yes (for debug).
在项目导航器中单击您的项目名称。选择构建设置并向下滚动到构建选项。确保启用可测试性为是(用于调试)。
Delete and re-add your Tests target
删除并重新添加您的测试目标
If you have done the other things my guess is that you probably don't need to do this. But if you do, remember to save any Unit Tests that you have already written.
如果您已经完成了其他事情,我的猜测是您可能不需要这样做。但是,如果您这样做了,请记住保存您已经编写的任何单元测试。
Click your project name in the Project Navigator. Then select your Tests target. Click the minus (-) button at the bottom to delete it.
在项目导航器中单击您的项目名称。然后选择您的测试目标。单击底部的减号 (-) 按钮将其删除。
Then click the plus (+) button and choose iOS Unit Testing Bundle to add it back again. As you can see, you can also add a UI Testing Bundle in the same way.
然后单击加号 (+) 按钮并选择 iOS Unit Testing Bundle 将其重新添加回来。如您所见,您还可以以相同的方式添加 UI 测试包。
A few other ideas
其他一些想法
- Make sure that all required classes are members of your test target.
- Make sure that you have added all the required libraries.
- Make sure that the module name is written correctly (see this answer).
- 确保所有必需的类都是测试目标的成员。
- 确保您已添加所有必需的库。
- 确保模块名称写入正确(请参阅此答案)。
Or...
或者...
Leave a comment or answer below if you found something else that worked.
如果您发现其他有用的东西,请在下面发表评论或回答。
Related
有关的
回答by Voda Ion
Please check your Module Name that you try to import with @testable import "ModuleName"
. The module name should be the same on Target->Build Settings-> Product Module Name
请检查您尝试导入的模块名称@testable import "ModuleName"
。模块名称应该相同Target->Build Settings-> Product Module Name
回答by Jess
The problem for me was the iOS deployment target of the tests was not set to be the same as the main target. So be sure to check this.
我的问题是测试的 iOS 部署目标没有设置为与主要目标相同。所以一定要检查这个。
In your test target:
在您的测试目标中:
Build Settings -> iOS Deployment Target -> iOS<same as the target you are testing>
回答by Vick Swift
So this is how I went about getting my code to work after trying all suggested solutions from prior suggestions.
所以这就是我在尝试了先前建议中的所有建议解决方案后让我的代码工作的方式。
- I set 'Enable testability' to 'YES' in project's Build Settings
- I also set 'Defines Module' to 'YES' in my project's Build Settings.
- For the regular .swift file(s) within my project, say MyApp, I was going to write test cases for, I have boththe main "MyApp" and the "MyAppUnitTests" Targets checked under Target Membership.
- I then selected my unit test file(s), declared the '@testable import MyApp' at the top, beneath the 'import XCTest', and onlychecked the "MyAppUnitTests" under Target membership
- 我在项目的构建设置中将“启用可测试性”设置为“是”
- 我还在项目的构建设置中将“定义模块”设置为“是”。
- 对于我的项目中的常规.swift文件(S),说MyApp的,我打算写测试用例,我有两个主要的“ MyApp的”和“ MyAppUnitTests”目标下检查目标成员。
- 然后我选择了我的单元测试文件,在顶部声明了“ @testable import MyApp”,在“ import XCTest”下面,并且只检查了Target 成员下的“MyAppUnitTests”
And everything worked like charm. Hope this helps.
一切都充满魅力。希望这可以帮助。
回答by smileBot
One gotcha to watch for is that if your module name has a dash character in it -
then you will have to refer to it with an underbar instead _
. For some reason I suspected this might be an issue and it was indeed my issue.
需要注意的一个问题是,如果您的模块名称中有一个破折号字符,-
那么您将不得不使用下划线来引用它_
。出于某种原因,我怀疑这可能是一个问题,这确实是我的问题。
eg. @testable import Ocean-Swift
becomes @testable import Ocean_Swift
例如。@testable import Ocean-Swift
变成@testable import Ocean_Swift
Just one other thing, if you do use the @testable
syntax be sure to not include your production code in your test target. I've found this will cause inexplicable weirdness.
还有一件事,如果您确实使用了该@testable
语法,请确保不要将您的生产代码包含在您的测试目标中。我发现这会导致莫名其妙的怪异。
回答by yageek
This sounds to be an error with the build settings of both targets. You need to ensure that:
这听起来是两个目标的构建设置错误。您需要确保:
ENABLE_TESTABILITY
equals Yes for both targets.- The
PRODUCT_MODULE_NAME
value of the test target should differ from the one of the application.
ENABLE_TESTABILITY
两个目标都等于 Yes。PRODUCT_MODULE_NAME
测试目标的值应与应用程序的值不同。
回答by Jim Rhoades
One other thing to check: If you have an Objective-C project, but are writing unit tests in Swift, make sure the main target uses at least one Swift file!
要检查的另一件事:如果您有一个 Objective-C 项目,但正在用 Swift 编写单元测试,请确保主要目标至少使用一个 Swift 文件!
More info:
更多信息:
I was working on an Objective-C project, but wanted to write unit tests in Swift.
我正在做一个 Objective-C 项目,但想用 Swift 编写单元测试。
I added a Swift file to the main target to generate the necessary ProjectName-Bridging-Header.h file, wrote my tests and everything was working properly.
我向主要目标添加了一个 Swift 文件以生成必要的 ProjectName-Bridging-Header.h 文件,编写了我的测试并且一切正常。
Later on I deleted the Swift file because I thought I didn't need it (all of the main target's code is in Objective-C... I was only writing tests in Swift).
后来我删除了 Swift 文件,因为我认为我不需要它(所有主要目标的代码都在 Objective-C 中......我只用 Swift 编写测试)。
I didn't notice a problem until later, after I did a "clean/clean build folder" and the "No Such Module" problem showed up. After some head scratching I added a new blank Swift file and the problem went away.
直到后来我才注意到问题,在我做了一个“干净/干净的构建文件夹”并且出现了“没有这样的模块”问题之后。经过一番摸索,我添加了一个新的空白 Swift 文件,问题就解决了。
I've tested it multiple times with/without the Swift file, and it only works with it... so, I'll either need to leave the blank file in the project, convert some Objective-C into Swift, or add some new code to the project written in Swift.
我已经在使用/不使用 Swift 文件的情况下对其进行了多次测试,但它只适用于它……所以,我需要在项目中保留空白文件,将一些 Objective-C 转换为 Swift,或者添加一些用 Swift 编写的项目的新代码。
回答by Igor P
After spending couple of days on this issues finally I make it to work with my project. Problem was in Bridging Header - path in Tests target can't be empty if you are using Bridging Header in your main target
在这个问题上花了几天时间后,我终于让它与我的项目一起工作。问题出在桥接头中 - 如果您在主要目标中使用桥接头,则测试目标中的路径不能为空
Hope it will save some time for someone.
希望它会为某人节省一些时间。
回答by SwissMark
For those who have scrolled until the last answer and still nothing worked, here is what did it for me after following all other answers advices. I am using Xcode 11:
对于那些滚动到最后一个答案但仍然没有任何效果的人,这是在遵循所有其他答案建议后为我所做的。我正在使用 Xcode 11:
What caused the issue in my case was that I changed my Product Name
在我的案例中导致问题的原因是我更改了产品名称
- I changed my Product Namein the Build Settings of my main target to "New Name"
- I had to re-select the Host Applicationfor my test target
I didn't know that changing the product name would also change the Product Module Name, that is the one used for the module import in my test files. I changed my import as follows:
@testable import New_Name
It worked
- 我在我的主要目标的构建设置中将我的产品名称更改为“新名称”
- 我不得不为我的测试目标重新选择主机应用程序
我不知道更改产品名称也会更改Product Module Name,这是我的测试文件中用于模块导入的名称。我改变了我的导入如下:
@testable 导入 New_Name
有效
I hope it helps
我希望它有帮助
回答by Dave Levy
Here is yet another thing to check that is not listed. For me, it had something to do with my team, perhaps because our Team's Agent had not yet agreed to the latest License Agreement! Once I selected a different Team in my Target's General settings, AND then I specified a specific Deployment Target like 12.1 or 11.0, suddenly the "No Such Module" warning went away.
这是未列出的另一件事要检查。对我来说,这与我的团队有关,可能是因为我们团队的代理尚未同意最新的许可协议!一旦我在目标的常规设置中选择了一个不同的团队,然后我指定了一个特定的部署目标,如 12.1 或 11.0,突然“没有这样的模块”警告消失了。