How to Import a Module with Spaces in Swift Using @testable

How to Import a Module with Spaces in Swift Using @testable

11 April 2024 Stephan Petzl Leave a comment Tech-Help

When working with Swift and XCTest, you might encounter a situation where you need to import a module that contains spaces in its name to write unit tests. This can be confusing as directly using spaces in module names is not permitted in Swift. Let’s examine how to correctly import such modules using the @testable attribute.

Understanding @testable

The @testable attribute is a feature in Swift that allows test modules to access internal entities in the module being tested. It is widely used in unit testing to ensure the code behaves as expected.

Importing Modules with Spaces

To import a module with spaces in its name, you cannot simply use a space in the import statement. Swift requires module names to be valid identifiers, and spaces are not allowed. Instead, you replace each space with an underscore (_).


Suppose your module name is “foo bar”. To import this module in your test file, you would write:

@testable import foo_bar

Practical Steps

  1. Open your Swift test file where you want to import the module.
  2. Locate the import statements at the top of the file.
  3. Use the @testable attribute followed by the import keyword.
  4. Replace any spaces in the module name with underscores.
  5. Ensure that there are no syntax errors and that the module name matches exactly, with spaces replaced by underscores.


Importing modules with spaces in their names using @testable in Swift is a straightforward process once you know the correct syntax. Remember to replace each space with an underscore to avoid any errors during compilation. This simple yet effective solution allows you to write comprehensive tests for all modules, regardless of their naming conventions.

Like this article? there’s more where that came from!