Rohan
Rohan

Reputation: 551

Swift 3 Unit Testing - Linker fails when using import @testable

I'm currently trying to write unit tests (NOT UI Tests) for my (macOS) Xcode project. I have created a new Unit Test target which creates a blank unit test file for me.

This complies and links fine, but there are no tests.

As soon as I add the line @testable import Pilot, where Pilot is the name of my App Target, and I try to compile and run, it fails with this message:

Linker command failed with exit code 1 (use -v to see invocation)

I've tried everything I can find, but nothing seems to be working. The other posts I have read on here deal with this problem in UI Tests, but that is because you cannot use @testable in UI Tests. You are supposed to be able to use it in Unit Tests, but I can't figure out why this is not working.

Does anyone have any insight?

If it helps, my project is located at: https://github.com/RohanNagar/pilot-osx

Here is the full PilotTests.swift file:

import XCTest

@testable import Pilot

class PilotTests: XCTestCase {

    override func setUp() {
        super.setUp()
        // Put setup code here. This method is called before the invocation of each test method in the class.
    }

    override func tearDown() {
        // Put teardown code here. This method is called after the invocation of each test method in the class.
        super.tearDown()
    }

    func testExample() {
        // This is an example of a functional test case.
        // Use XCTAssert and related functions to verify your tests produce the correct results.
    }

    func testPerformanceExample() {
        // This is an example of a performance test case.
        self.measure {
            // Put the code you want to measure the time of here.
        }
    }

}

Upvotes: 2

Views: 2874

Answers (4)

Binh Le
Binh Le

Reputation: 556

  1. Select your test target
  2. Select Build Phases
  3. In the Framework Search Paths, add this "${PODS_CONFIGURATION_BUILD_DIR}/" with recursive option

Upvotes: 0

tania_S
tania_S

Reputation: 1370

Set Host Application to your Project Target. Also, check the Allow testing Host Application APIs is turned on.

enter image description here

Upvotes: 1

raja
raja

Reputation: 1161

If your CocoaPods frameworks not included in the Test targets. It will throw this error.

I made sure running a pod install, but still it failed.

So 'pod deintegrate Yourproject.xcodeproj' and reinstall (pod install), clears the issue.

Upvotes: 4

Andre
Andre

Reputation: 1145

The import fails because the project fails to link together. I downloaded it and I get the following error trying to run it:

Showing Recent Issues
ld: framework not found Realm for architecture x86_64

Try to clean your build folder, or download the project to a new folder, and fix this issue... after that you'll be able to compile and @testable import Pilot.

Upvotes: 2

Related Questions