Andrew Ebling
Andrew Ebling

Reputation: 10283

Strange compilation error with @testable import

I've just added some unit tests to a new project. Normally I use @testable import to import the production code, so I can test non-public methods:

@testable import My-Project
import XCTest

final class MyTests: XCTestCase {
   func testOne() {
       // ...
   }
}

However I'm seeing some strange compilation errors, which I've never seen before:

Tests.swift:1:25: Consecutive statements on a line must be separated by ';'
Tests.swift:1:25: Expected expression after unary operator
Tests.swift:1:25: Expressions are not allowed at the top level
Tests.swift:1:18: No such module 'My'

I've checked and double checked everything and just cannot work out why this compilation error is produced.

What am I missing?

Upvotes: 3

Views: 683

Answers (2)

Martin R
Martin R

Reputation: 540055

Project and target names can contain special characters (like spaces or dashes), but module names can not. By default, Xcode generates the module name from the target name by replacing invalid characters with an underscore.

Therefore, in your case

@testable import My_Project

would fix the issue. Alternatively, assign a custom “Product Module Name” in the build settings of the target.

There is no need to rename the entire project (or target).

Upvotes: 4

Andrew Ebling
Andrew Ebling

Reputation: 10283

The project name (and therefore internal module name) contains a dash (-) character.

To fix this:

  1. Select the top level project file icon in the Xcode Project Navigator
  2. Press enter to rename the project
  3. Remove the dash from the project name
  4. Follow prompts to do the final refactoring
  5. Update your @testable import ... statement to reflect the new module nam.

Upvotes: 0

Related Questions