It is not possible to create the mockup data inside your UITest classes, because the UITest classes cannot access your app's code.
From Apple's Docs:
UI testing differs from unit testing in fundamental ways. Unit testing
enables you to work within your app's scope and allows you to exercise
functions and methods with full access to your app's variables and
state. UI testing exercises your app's UI in the same way that users
do without access to your app's internal methods, functions, and
variables. This enables your tests to see the app the same way a user
does, exposing UI problems that users encounter.
If you want to use mock data when running UITests you have create the mock data inside your app's code and then make sure the mock data is only created when running UITests.
To make that work you have to do the following steps:
1) Add a launch argument when launching your app in your UITest class:
func testExample() {
let app = XCUIApplication()
app.launchArguments.append("IS_RUNNING_UITEST")
app.launch()
// Do your tests
}
2) Add the code that creates the mock data to your app (e.g. in your AppDelegate
) and run it when the launch argument is present:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
if ProcessInfo.processInfo.arguments.contains("IS_RUNNING_UITEST") {
// insert data from a JSON file into the managed object context
}
}