Cannot load underlying module for XCTest
SwiftXcodeXctestSwift Problem Overview
I'm working in swift on Xcode and by default it creates a test file that references XCTest.
When I set the target membership to my main project it causes this error
> Cannot load underlying module for XCTest
If this target membership is not set the tests runs properly and everything works fine.
Swift Solutions
Solution 1 - Swift
Double check that the file in question is not in the main target but instead only the test target. Only the test target will have that framework to import.
Solution 2 - Swift
The main project does not link with the XCUnit framework. You should create a separate testing target for your project, if one does not already exist, and add your test source files to that target.
- Select your project in the Project Navigator. This will open the project's settings in the editor.
- Click the "+" button at the bottom of the column listing your Targets.
- If you are working on an iOS project template, select
iOS > Test > iOS Unit Testing Bundle
.
If you are working on an OS X project template, select OS X > Test > OS X Unit Testing Bundle
.
Solution 3 - Swift
You've just added your tests file/class into main target and not into test target by mistake. Simple as that.
Solution:
-
Remove test file from "Compile Sources" list on "Build Phases" tab for main target
-
Add same file into "Compile Sources" on "Build Phases" tab for test target
It must resolve the issue
Solution 4 - Swift
I get this error if I create a macOS dynamic framework target, which links to XCTest.framework
(the target is a framework target, not a testing target!).
In this case the problem is solved by adding
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks
to project's "Build Settings / Framework Search Paths".
Solution 5 - Swift
After lots of troubleshooting and bouncing around Stack Overflow pages such as this one, I managed to find one detail that was not mentioned on the other iOS Unit Test troubleshooting pages.
If your project uses CocoaPods, make sure to include your test target nested inside your main application's target. Your podfile might not include it if you created your test target after running pod init
. Your podfile should look something like this:
target 'YourApp' do
target 'YourAppTests' do
inherit! :search_paths
# Pods for testing
end
end
Remember to save your podfile and run pod install
after doing so.
Hope it helps!
Solution 6 - Swift
This maybe a little late but what you have to do is:
Click on File > New > Target > iOS Unit Testing Bundle.
And then add your tests in there. This worked for me. The problem was that I didn't select create unit tests when I created my project. Hope this helps someone.
Solution 7 - Swift
This problem occur two times in your project.
- When you Don't Add UnitTestingBundle in your project when create a New Project.
- When you install CocoaPods in Your Project sometime not every time.
1:Answer:
-
Go to the File Click on File.
-
Then You see First option New Click on it.
-
After Clicking on it you see Some Option Click on Target.
-
And then in the Searchbar Search IOS Unit Testing Bundle and Add Problem is solved.
-
if Compiler can't give you a permission to add this then try Second Method when is given Below.
2. Answer CocoaPod Problem:
-
Go to the Product Click on the Product.
-
Click on clean or press cmd+k.
-
Problem is solved.
Solution 8 - Swift
While the accepted answer solves this problem for majority of times, there is one more way you can come across this error.
If you have already ensured that:
As pointed out by @pwc @nick-n and others
- The
ClassTest.swift
file for target membership by ensuring that it is only attached to the Testing target. - The
ClassTest.swift
is not visible under you main app target > Build Sources > Compile Sources
Here is what else you can check:
in your .podspecs
file
Ensure that your source_files
does not directly or indirectly includes the testing directory.
for example :
s.source_files = ["Classes/**/*.{swift}", "Classes/**/*.{xib}"]
s.exclude_files = ["Classes/Exclude", "Classes/MyPodProjTests/"]
Note that Classes/**/*.{swift}
includes everything overriding the fact that the directory MyPodProjTests
must be excluded.
Solution:
s.source_files = ["Classes/MyPodProj/**/*.{swift}", "Classes/**/*.{xib}"]
Note: This is extremely edge case and completely human error but I thought it will be worth point out.
Solution 9 - Swift
Make sure the Target Membership is checked off for all the modules when the test framework is selected. You can view the Target Membership option by selecting View->Utilities->Show File Inspector
Solution 10 - Swift
In my case, I was making the tests for a private pod, so in the podspec was like:
s.test_spec 'Tests' do |test_spec|
test_spec.source_files = 'Tests/*.swift' end
But in the Podfile I forgot to bring the test specification, as such:
target 'MyApp' do
use_frameworks!
pod 'CoconutLib', '~> 1.0', :testspecs => ['Tests'] end
Solution 11 - Swift
I did all the above, but still I was not allowed to access the methods I wanted to test. But before I wrote any test case built the project once and then the methods were accessible to test. So do try it and let me know if it solved any ones problem.
Solution 12 - Swift
I had the same problem and the solution for me was to:
- Select my scheme
- Edit Scheme...
- Then when Scheme configuration window is opened select left tab named "Test" to be redirected here:
- Like you can see my UITests are not added here but exist in my project so I had just to tap plus button and select my UITests target like this:
- Then tap "Add" button and close configuration view
- Finish with a clean of project and try again it should work
- And don't forget to verify that too: https://developer.apple.com/library/archive/qa/qa1954/_index.html
Hope this answer will help.
PS: one last think in my Test target in build settings I had to check these property that was not configured correctly:
DEBUG_INFORMATION_FORMAT = dwarf;
ONLY_ACTIVE_ARCH = YES;
VALIDATE_PRODUCT = NO;
Solution 13 - Swift
Just as @Alfishe mentioned above:
You have to remove the test files from Compiler Sources
under Build Phases
As shown below
Solution 14 - Swift
Check Target Membership of your Test class.
Solution 15 - Swift
The problem is that Unit Testing Bundle
as a target should be created beforehand