UPDATE Swift 2.x, 3.x, 4.x and 5.x
Now you don't need to add the public
to the methods to test then.
On newer versions of Swift it's only necessary to add the @testable
keyword.
PrimeNumberModelTests.swift
import XCTest
@testable import MyProject
class PrimeNumberModelTests: XCTestCase {
let testObject = PrimeNumberModel()
}
And your internal methods can keep Internal
PrimeNumberModel.swift
import Foundation
class PrimeNumberModel {
init() {
}
}
Note that private
(and fileprivate
) symbols are not available even with using @testable
.
Swift 1.x
There are two relevant concepts from Swift here (As Xcode 6 beta 6).
- You don't need to import Swift classes, but you need to import external modules (targets)
- The Default Access Control level in Swift is
Internal access
Considering that tests are on another target on PrimeNumberModelTests.swift
you need to import
the target that contains the class that you want to test, if your target is called MyProject
will need to add import MyProject
to the PrimeNumberModelTests
:
PrimeNumberModelTests.swift
import XCTest
import MyProject
class PrimeNumberModelTests: XCTestCase {
let testObject = PrimeNumberModel()
}
But this is not enough to test your class PrimeNumberModel
, since the default Access Control level is Internal Access
, your class won't be visible to the test bundle, so you need to make it Public Access
and all the methods that you want to test:
PrimeNumberModel.swift
import Foundation
public class PrimeNumberModel {
public init() {
}
}