skip to content

In Swift, what does `@testable import` let a test target see, and why must the imported module be built with `-enable-testing`?

level: middleimportance: should knowfreq 55%

answer

  1. internal becomes reachable
  2. public classes behave as open
  3. private and fileprivate stay hidden
  4. a build flag the release build lacks

basics

~20 s

A testable import treats the module's internal declarations as public and its internal or public classes and members as open, so tests can call internals and subclass. It fails with an error unless the module was compiled with -enable-testing.

solid answer

~40 s

`@testable import Core` imports Core's `internal` declarations as if they were `public`, and its `internal` and `public` classes and class members as if they were `open`, so a test can call internal helpers, use the internal memberwise initializer and subclass a non-open class for a test double. It does not touch `private` or `fileprivate` declarations, nor a `private(set)` or `fileprivate(set)` setter. The imported module must be compiled with `-enable-testing`, whose help text says it allows the module's internal API to be accessed for testing; without it the import fails with `module 'Core' was not compiled for testing`. Swift's own diagnostic notes recommend that flag only for debug or other non-production builds, which is why a testable import breaks when tests run against a release configuration.

code

swift · 14 lines
swift
// Core module
public class FeatureFlags {
    public init() {}
    public func isEnabled(_ name: String) -> Bool { overrides[name] ?? false }
    var overrides: [String: Bool] = [:]                // internal
    func reload() {}                                  // internal
}

struct RetryPolicy {                                  // internal, memberwise init internal
    var attempts: Int
    var delay: Double
}

fileprivate func backoff(_ n: Int) -> Double { Double(n) * 0.5 }

go deeper

for a junior

Recall that a testable import lets tests use internal declarations and that the module needs to be built for testing.

for a middle

Explain the two widenings, internal to public and internal or public classes to open, and what stays hidden.

for a senior

Show the production judgment: why the flag belongs in non-production builds, and how testable imports can hide an unusable public API.

for a principal

Set a team rule for when tests may use a testable import and which suites must exercise only the public surface.

## What a testable import changes A test target is a separate module. With a plain `import Core` it sees only Core's `public` and `open` API, like any other client. Writing `@testable import Core` changes how Core's declarations are imported into that test module: - declarations marked **`internal`**, explicitly or by default, are imported as if they were **`public`**; - classes and class members marked **`internal` or `public`** are imported as if they were **`open`**, so a test can subclass a non-open class and override its members; - `private` and `fileprivate` declarations remain invisible; - a `private(set)` or `fileprivate(set)` setter stays out of reach: a `public private(set)` property is still read-only to the test. In practice that means a test can call an internal `reload()`, read an internal dictionary of overrides, construct an internal `RetryPolicy` through its internal memberwise initializer, and write `final class StubFlags: FeatureFlags` even though `FeatureFlags` is only `public`. ## Why the module must be built with `-enable-testing` A normal build lets the compiler assume that nothing outside Core can reach Core's internal declarations. The `-enable-testing` flag withdraws that assumption; its help text describes it as allowing "this module's internal API to be accessed for testing". If Core was built without it, the testable import fails: ```plaintext error: module 'Core' was not compiled for testing [#ModuleNotTestable] ``` Swift's documentation for that diagnostic group recommends `-enable-testing` "only in debug builds or other builds not intended for production use". In Xcode the flag is controlled by the Enable Testability build setting, usually on for the Debug configuration only, which is why the same test suite can fail with this error when someone runs it against a release configuration. ## Pitfalls a reviewer should catch 1. **Tests pass on API real clients cannot use.** A `public struct Session` whose initializer is the implicit memberwise one is constructible in `@testable` tests, because that initializer is internal, yet the Feature module gets an inaccessible-initializer error. At least one test, or a real client module, should use a plain import. 2. **Testable imports in production code.** Shipping code that writes `@testable import` forces its dependency to be built for testing; it is a sign that a declaration should be `package` or `public`. 3. **Dependency loading.** The proposal on access-level imports notes that a module with a `@testable` import of Core must load all of Core's dependencies, even those Core imports `private` or `fileprivate`, because internal declarations may mention them. 4. **Access-level imports still bound it.** A testable import can carry an access level like any import, and that level caps what the imported internals become in the file: through `@testable internal import Core`, Core's internals act as internal in that file. ## A worked example Picture a Core framework with a `public class FeatureFlags` whose `overrides` dictionary and `reload()` method are internal, an internal `struct RetryPolicy`, and a `fileprivate` backoff helper. Its test module, CoreTests, writes `@testable import Core` and then: - declares `final class StubFlags: FeatureFlags` and overrides `reload()`, both of which a plain import would reject because the class is not `open` and the method is internal; - writes `flags.overrides["newCheckout"] = true` to drive a code path directly; - builds `RetryPolicy(attempts: 3, delay: 0.5)` with the memberwise initializer, which is internal. Calling the backoff helper still fails with `cannot find 'backoff' in scope`: the helper is `fileprivate` and no import can see it. When Core is rebuilt without `-enable-testing`, the very first line, the import, fails before anything else is checked. ## What it does not reach Nothing makes `private` or `fileprivate` members visible to another module. When a test wants one, the usual fix is to move the logic behind an internal function or type the test can call, not to widen the member. Writing the tests themselves, with Swift Testing's `@Test` or XCTest's test cases, is covered with those frameworks.

  • Through `@testable import Core`, can a test assign a property Core declares as `public private(set)`?
    No. The testable import widens `internal` to `public` and makes internal and public classes behave as `open`, but a `private` setter is still private. The test can read the property and call Core's mutating methods; assigning it reports that the setter is inaccessible.
  • Why can a test suite that passes in Debug fail to compile when run against a Release configuration?
    Release builds normally do not pass `-enable-testing`, since it is meant for non-production builds. A test file using `@testable import Core` then fails with `module 'Core' was not compiled for testing`. Either test the Debug configuration, enable testability for that configuration, or limit those tests to the public API with a plain import.

  • @testable import exposes private and fileprivate members to tests
  • @testable works on any module whatever flags it was built with
  • Without -enable-testing a testable import silently falls back to public API
  • Test doubles can only subclass classes that the module marked open
  • Passing tests through @testable prove the public API is usable by clients