问题
Someone has tried to include mock data with the new Xcode 7 UI tests?
- Have you used an specific framework?
- How have you managed targets?
回答1:
I think there are a lot of ways to approach this one - the difficulty is that Apple has intentionally designed UITests to run entirely separate from the app under test. That said, there are a few hooks you can use to coordinate logic in the app with logic in your tests to feed in mock data or alter the behavior of your app in any way. The two I have found most useful are launchEnvironment
and launchArguments
.
in your test - XCUIApplication().launchArguments
corresponds to NSProcessInfo.processInfo().arguments
in your app code
likewise:
XCUIApplication().launchEnvironment
-> NSProcessInfo.processInfo().environment
launchEnvironment is a straight forward dictionary whereas launch arguments is an array. In your test you can feed any values you like into either of these parameters before you launch the app:
let app = XCUIApplication()
app.launchEnvironment["-FakedFeedResponse"] = "success.json"
app.launch()
Then in your application logic you can switch on these values however you like. Something like:
func fetchFeed() -> JSON {
if let fakedJSONFilename = NSProcessInfo.processInfo().environment["-FakedFeedResponse"] {
let fakePayload = fakeDataFileNamed(fakedJSONFilename)
return fakePayload
} else {
//Make network call and return a real JSON payload
}
}
Using this pattern your faked/mock data will need to be files included as members of the app target.
来源:https://stackoverflow.com/questions/31403684/how-to-mock-data-in-uitest-on-xcode-7