-
Notifications
You must be signed in to change notification settings - Fork 914
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Problems with async test #1273
Comments
Facing similar error as the UI does not update when the KIF perform a step. |
With some investigation, I could mitigate the failures by adding
Not sure why KIF blocks the UI thread from updating or is there a better way to wait for UI updates. |
I tried to add some class AsyncKIFUITestActor {
let kitTestActor: KIFUITestActor
init(inFile file: String = #file, atLine line: Int = #line, delegate: KIFTestActorDelegate) {
kitTestActor = KIFUITestActor(inFile: file, atLine: line, delegate: delegate)
}
func tapView(withAccessibilityLabel accessibilityLabel: String) async {
return await withCheckedContinuation { continuation in
DispatchQueue.main.async {
self.kitTestActor.tapView(withAccessibilityLabel: accessibilityLabel)
continuation.resume()
}
}
}
func tryFindingView(withAccessibilityLabel accessibilityLabel: String) async throws {
return try await withCheckedThrowingContinuation { continuation in
DispatchQueue.main.async {
do {
try self.kitTestActor.tryFindingView(withAccessibilityLabel: accessibilityLabel)
continuation.resume()
} catch {
continuation.resume(throwing: error)
}
}
}
}
...
}
extension XCTestCase {
func asyncTester(file : String = #file, _ line : Int = #line) -> AsyncKIFUITestActor {
return AsyncKIFUITestActor(inFile: file, atLine: line, delegate: self)
}
} and then changed the test to use it: final class KIFAsyncTests: XCTestCase {
@MainActor
func testAsyncMethod() async throws {
await asyncTester().tapView(withAccessibilityLabel: "Show alert")
await asyncTester().tapView(withAccessibilityLabel: "Show sheet")
try await asyncTester().tryFindingView(withAccessibilityLabel: "Hello world")
}
} I had to create a wrapper for every function that I used, but it appears to work fine. I need to test in more complex projects. |
Thanks for digging in on this! Would this change work for the existing |
I think it is a change in behaviour. Non async functions can't call it. It is possible to overload the current actors to have both functions, one sync and another async, but it will have a lot of duplicated code. |
It may be easier extending I'm not actively working on KIF at the moment, but I'm definitely available for guidance and can review changes if this is something you wanted to work on. Our codebase doesn't have SwiftUI, so there hasn't been a need to solve this problem for ourselves. Thanks for looking into this! |
Hi, I'm having problems when using an async test with KIF. I was able to create a simple app with one tests that always pass if the test is a sync function, but always fails if it is an async func:
I'm able to reproduce the error with iOS 15.4, 15.5, 16.0 and 16.2 simulators (didn't test early versions) and with a real device with iOS 16.2. I'm using Xcode 14.2 and the latests KIF version from master.
The text was updated successfully, but these errors were encountered: