Cannot load underlying module for XCTest

Solution 1:

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:

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:

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:

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