How do I access my app code from my tests in a Swift project?

3 Solutions Collect From Internet About “How do I access my app code from my tests in a Swift project?”

As it has been said already, the library code and the test code are 2 different modules. So you have to import the library into the test code and also make the functions that you want to test public, e.g:

public class Utils: NSObject {
    public class func cleanString (input: String, trim: Bool) -> String {
        // ...
    }
}

and

import XCTest
import Utils

class AppTests: XCTestCase {
    func testConfiguratio() {
        Utils.cleanString("foo", trim: true)
    }
}

If you want to see working code look at my IBANtools library project which implements exactly this scenario (class functions, swift framework, lots of testing).

The module that contains your tests is distinct from the module that contains your app code. When you want to access classes that are contained within a separate module you need to ensure that the to-be-imported classes are marked as public:

public class Utils: NSObject {
    class func cleanString (input: String, trim: Bool) -> String {
        // ...
    }
}

If this is an OSX project – make sure you included

@Testable import YOURPROJECTNAME

Above the ‘class AppTests: XCTestCase’
and clean your project files.

My Previous question where I had a similar issue is here

Hope this helps ( even a year later…)