Cannot load underlying module for XCTest

SwiftXcodeXctest

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

  1. Select your project in the Project Navigator. This will open the project's settings in the editor.
  2. Click the "+" button at the bottom of the column listing your Targets.

enter image description here

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

  1. Remove test file from "Compile Sources" list on "Build Phases" tab for main target

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

  1. When you Don't Add UnitTestingBundle in your project when create a New Project.
  2. 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

  1. The ClassTest.swift file for target membership by ensuring that it is only attached to the Testing target.
  2. 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:

  1. Select my scheme
  2. Edit Scheme...
  3. Then when Scheme configuration window is opened select left tab named "Test" to be redirected here: enter image description here
  4. 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: enter image description here
  5. Then tap "Add" button and close configuration view
  6. Finish with a clean of project and try again it should work
  7. 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


enter image description here

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

[How to add it]

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbrducaView Question on Stackoverflow
Solution 1 - SwiftAaron ZinmanView Answer on Stackoverflow
Solution 2 - SwiftpwcView Answer on Stackoverflow
Solution 3 - SwiftAlfisheView Answer on Stackoverflow
Solution 4 - SwiftStanislav PankevichView Answer on Stackoverflow
Solution 5 - SwiftOrlando G RodriguezView Answer on Stackoverflow
Solution 6 - SwiftClinton D'SouzaView Answer on Stackoverflow
Solution 7 - SwiftMuhammad ZeeshanView Answer on Stackoverflow
Solution 8 - SwiftthesummersignView Answer on Stackoverflow
Solution 9 - SwiftkarmaView Answer on Stackoverflow
Solution 10 - SwiftHugo JordaoView Answer on Stackoverflow
Solution 11 - Swiftsunil chayagolView Answer on Stackoverflow
Solution 12 - SwiftO. BoujaouaneView Answer on Stackoverflow
Solution 13 - SwiftSuhaibView Answer on Stackoverflow
Solution 14 - SwiftDenis KutlubaevView Answer on Stackoverflow
Solution 15 - SwiftyoAlex5View Answer on Stackoverflow