Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you create public extensions, in a shared framework, for XCTest?

Tags:

swift

xctest

For example, I never use the description of XCTestCase.expectation, so I'd like to use a function to provide a default for it, and make it clear via naming that I'm initializing the expectation, as you can't really use an initializer for XCTestExpectation. But if the extension is not in a test target, then it can't compile:

Cannot load underlying module for 'XCTest'

import XCTest

public extension XCTestCase {
    func makeExpectation() -> XCTestExpectation {
        return expectation(withDescription: "")
    }
}
like image 303
Jessy Avatar asked Jul 21 '16 03:07

Jessy


1 Answers

I've created an xcworkspace here (https://github.com/dtweston/TestFrameworkSample) that demonstrates a solution to your issue. There are two projects in this workspace:

  1. SampleApp project with an iOS app target and a unit test target.
  2. SharedTestFramework project that imports XCTest and declares the single extension you put in your question.

The SampleAppTests target links to the SharedTestFramework to be able to use the extension it defines. The single test file imports the SharedTestFramework.

With those steps, I also encounter the Cannot load underlying module for 'XCTest' when building the SharedTestFramework.

The fix for that is to update the Framework Search Paths to include "$(PLATFORM_DIR)/Developer/Library/Frameworks". Now the SharedTestFramework compiles correctly, and as you can see in the workspace I uploaded, the SampleAppTests target is able to use it successfully.

Old and busted answer

Are you building a separate framework that is designed to be imported into test targets? If that's the case then I think you just need to reference XCTest.framework from this custom framework you're building.

On the other hand, if you're trying to add this extension to a framework that is used by your app target, that seems like a bad idea, because it would mean linking XCTest.framework to the binary that goes to the store and runs on people's devices.

I'm not sure if that's possible. I'm more confident that it's not a scenario Apple expects or supports.

like image 111
Dave Weston Avatar answered Nov 18 '22 17:11

Dave Weston