Fetching SKProducts while in UITesting

Viewed 182

So it turns out in iOS 14.0 and Xcode 12.0 in order to test in-app purchases and subscriptions on Simulator - I have to add a StoreKit configuration such as StoreKit.configuration and set it in the scheme Run options. This worked great.

However I am running some UITests on my app and unfortunately this configuration will not take effect when running the app from a UITest. And so, my SKProducts cannot be retrieved.

Anyone has an idea how to get that to work?

p.s I know StoreKitTest will allow me to perform unit testing on the in-app purchases but that is not my goal here.

1 Answers

I find it's incredibly limited, but I can test against the basic stuff in the config file itself, doesn't look like the APIs work much though. You need to create a session.

This needs to be called after your app has already launched

#import StoreKitTest

class ExampleTestSuite: XCTest { 
    func testExample() { 
        app.launch() 
    
        var session: SKTestSession

        // Given your configuration file is called "StoreKit"
        session = try SKTestSession(configurationFileNamed: "StoreKit")

        // test stuff
    }
}
Related