Create and run unit tests, performance tests, and UI tests for your Xcode project using XCTest.

XCTest Documentation

Posts under XCTest tag

182 Posts
Sort by:
Post not yet marked as solved
0 Replies
153 Views
Due to some of what we do with the Metal API in our iOS app, we are unable use Simulator for development / testing purposes. With the introduction of Apple Silicon, we are able to run/build to the "My Mac (Designed for iPad)" destination for our app. However, when we try running UI tests with that destination we get the message "Cannot test target <ui_test_target> on "My Mac": UI tests are not supported ..." Wondering if there's a way to run UI tests against the "My Mac (Designed for iPad)" destination?
Posted
by
Post not yet marked as solved
3 Replies
354 Views
In MacOS I can achieve this via func sendKeyStrike(_ keyCode: CGKeyCode, useCommandFlag: Bool) { let sourceRef = CGEventSource(stateID: .combinedSessionState) if sourceRef == nil { NSLog("FakeKey: No event source") return } let keyDownEvent = CGEvent(keyboardEventSource: sourceRef, virtualKey: keyCode, keyDown: true) if useCommandFlag { keyDownEvent?.flags = .maskCommand } let keyUpEvent = CGEvent(keyboardEventSource: sourceRef, virtualKey: keyCode, keyDown: false) keyDownEvent?.post(tap: .cghidEventTap) keyUpEvent?.post(tap: .cghidEventTap) } Is there a function call either in XCUITest or any other framework to achieve keyboard strokes in IOS? IOS Seems to have HID keycodes but I just can't find a way to send them to the device. Either through XCUITest or any other framework. (I am ok with simulating a HID device using python or something else to command my iOS device as well.
Posted
by
Post not yet marked as solved
1 Replies
202 Views
I have a multiplatform project in Xcode 13.3.1, but when I try to run a unit by clicking the diamond in the gutter, the linker fails. From the link line in the log, it looks as if it isn't even linking with my app at all. I also noticed that I had to put in my own @testable import NameOfMyApp instead of Xcode generating that line like it does for single-platform projects, so that makes me wonder if this something extra i need to do since this project is multiplatform? Everything compiles fine, but the linker seems to be forgeting to link with my app...
Posted
by
Post not yet marked as solved
0 Replies
171 Views
The problem On iOS 15 was changed the view for notifications on the Notification center. All tried operations with XCUITest are not working to expand it by force touch. Environment Xcode 13.3; iOS 15.4; iPhone 8 simulator; macOS Monterey 12.2.1. Details For example, we received any notification in the notification center. Manually we can expand it by force touch ( NB! Should be on: Simulator->I/O -> Touch pressures -> Use trackpad force). But there are no any ways to do this by XCUITest methods. Seem like not enough press is performed for all of them. Could this issue be fixed somehow?
Posted
by
Post not yet marked as solved
1 Replies
187 Views
I have the following protocol that a NetworkEngine conforms to: protocol NetworkEngineProtocol { func request&amp;lt;T: Decodable&amp;gt;(from endpoint: Endpoint) async throws -&amp;gt; T } This protocol is passed as a dependency into a custom ViewModel and I want to unit test the request method of the ViewModel, that itself calls the dependency request method. class ViewModel: ViewModelProtocol { var passthrough: PassthroughSubject&amp;lt;[SomeProperty], Never&amp;gt; = PassthroughSubject&amp;lt;[SomeProperty], Never&amp;gt;() private let networkEngine: NetworkEngineProtocol init(networkEngine: NetworkEngineProtocol) { self.networkEngine = networkEngine } func request() { Task { do { let model = try await networkEngine.request(from: .messages()) as CustomModel // ... do more stuff passthrough.send(someValue) // would want to test this } catch let err as NetworkEngineErrors { // ... error handling } } } } struct CustomModel: Decodable { let message: [String] let status: String } In order to Unit Test this method, I'd have to create a Mock class, that conforms to the NetworkEngineProtocol. Something like: class MockNetworkProtocol: NetworkEngineProtocol { init() {} func request&amp;lt;T&amp;gt;(from endpoint: Endpoint) async throws -&amp;gt; T where T : Decodable { // how to mock this? } } What's the best way to do this? How can I mock the request method of the NetworkEngine? Thanks
Posted
by
Post not yet marked as solved
0 Replies
204 Views
Hello, We are experiencing failing UI Tests on M1 devices (Xcode 13.3.1) while they pass well on Intel devices. The most common errors are: App stucks on splashcreen resulting in xctassert timeout Scrolling is not working properly (like there is no deceleration) resulting in wrong screen position and not found elements Notes: Simulator is running without Rosetta We have to exclude arm64-simulator arch support because of third party libraries Has anyone encountered such a problem? Thanks!
Posted
by
Post not yet marked as solved
0 Replies
168 Views
I would like to create some UI tests for an App Clip. But when adding the UI Test target, I am unable to select the app clip target as the target to be tested (It's greyed out). The only target I can add is the "real" app. Any suggestions? Isn't it possible, or do I need to configure it somehow?
Posted
by
Post not yet marked as solved
0 Replies
104 Views
It seems like there’s no access to many accessibility properties of UI elements exposed in XCUITest framework (XCUIElement). Specifically, I’m trying to test the accessibility hint (accessibilityHint) of an element, but unable to do that. Although this is what I'm missing right now, but in general there are more accessibility properties I'd like to be able to access: accessibilityTraits accessibilityLanguage accessibilityActivationPoint accessibilityPath Is there any way to do this using XCUITest framework? If not, why can't we access them if those are accessibility attributes? What am I missing here?
Posted
by
Post not yet marked as solved
1 Replies
174 Views
In my XCUITest tests, I expected to be able to scroll elements in iOS >= 15 devices (with X,Y deltas) since it’s declared in the header file as: /** * Scroll the view the specified pixels, x and y. */ @available(iOS 15.0, *) open func scroll(byDeltaX deltaX: CGFloat, deltaY: CGFloat) However, in the documentation website (https://developer.apple.com/documentation/xctest/xcuielement/1500758-scroll) it says under “Discussion”: Available in macOS and in iPadOS 15 and later. No iPhones :( Although it says nothing under “Availability”. Calling this action when running on iPhone simulator, indeed raises the error message: Pointer events are not supported for this device. I’d expect this action to work on iOS as well, and not only on iPadOS. But sadly, as I mentioned, it isn't. I made some workaround to make this "work" (partially), using XCUICoordinate but I really don't like it (since it's impossible to find an hittable coordinate without guessing or using some messy heuristics that includes frames work) and would have been happy for something more elegant. Is there any reasonable (elegant and accurate) alternative for this, that is scrolls based on X and Y deltas?
Posted
by
Post not yet marked as solved
0 Replies
92 Views
I'm trying to use this API in my UITest suite: https://developer.apple.com/documentation/xctest/xcuicoordinate/3551692-press press(forDuration:thenDragTo:withVelocity:thenHoldForDuration:) However, the holdDuration parameter has no affect and the action stops once the drag is finished (instead of keep holding the element). I've reproduced this error with a demo application, along with a reproductive UI test and videos: https://github.com/asafkorem/XCUITestHoldBugReproduction Also, I do not find any reasonable workaround for the problem. Thanks.
Posted
by
Post not yet marked as solved
0 Replies
129 Views
I want to perform a unit test that whether my AVPlayerLayer is successfully added as sublayer in my ViewController view . I want to make a separate ViewController for this.Is it possible to make a separate ViewController for Unit Test ?
Posted
by
Post not yet marked as solved
0 Replies
183 Views
Context - We have E2E Testing running on the CI on the iPhone devices. The script performs the following steps - Build the Test runner app from code. Extracts the app file from the IPA. Re-codesign the app with the required Provision Profiles. Creates a .xctestrun file Uses the xcodebuild command like the following to run the test - xcodebuild -sdk iphoneos -destination id=<device_id> -xctestrun <path_to_xctestrun_file> -resultBundlePath <path_to_test_bundle_file> test-without-building -only-testing:E2ETestsBundle.xctest/<test_filter> The above command installs the runner and target app into the device and starts executing the test. The apps get installed and uninstalled at each run during a single execution. Problem - During the execution, the xcodebuild CLI tool, suddenly loses connection to the device and is not able to install the apps into the device and then results in the following error - Testing failed: E2ETestsBundle.xctest: E2ETestsBundle-Runner encountered an error (Early unexpected exit, operation never finished bootstrapping - no restart will be attempted. (Underlying Error: Test runner exited before starting test execution. If you believe this error represents a bug, please attach the log file at /var/folders/1r/0xlnbmzd0yj3m_cc7p95yb4r0000gn/T/tmpl7d9p1s6/testResults.bundle/Staging/1_Test/Diagnostics/E2ETestsBundle-Configuration-Test Scheme Action-Iteration-1/E2ETestsBundle/Session-E2ETestsBundle-2022-04-13_205520-muaMCx.log)) ** TEST EXECUTE FAILED ** Points to note - The xcodebuild CLI tool will keep on failing with the error once it occurred until recovered. Even while the xcodebuild is in a failed state, the devices are still accessible via Xcode IDE and other CLI tools like libimobiledevice and are able to install both the test runner and target applications. This is occurring intermittently across all devices models and across subversions of iOS 14 and 15. Recovery - The only way to recover is to reboot the device. Even after a reboot, the devices start facing this exception again after a few days of execution. As we can't keep on rebooting the device every time before execution, we need a solution to fix this problem. Any help would be appreciated!!
Posted
by
Post not yet marked as solved
0 Replies
124 Views
I'm implementing XCUI tests for an Apple Watch app and have trouble with text fields. I suspect it's a bug (FB9983700) TextField("Hello, World!", text: $text) .accessibilityIdentifier("hellotext") XCTAssertTrue(app.textFields["hellotext"].exists) // Fails on watch but works on iPhone I found a workaround by using otherElements instead of textFields, but then the test will have to be different between iOS and watchOS. Does anyone know of a better way?
Posted
by
Post not yet marked as solved
0 Replies
217 Views
Hi I have a problem with these identifiers I applied it like this to the tabbar self.tabBar.accessibilityIdentifier = @"MyID"; and works!!! But for tabBarItem I've tried everything but it doesn't work it's always empty Called in viewDidLoad of my TabbarController self.tabBarItem.isAccessibilityElement = YES; [self.tabBarItem setIsAccessibilityElement:YES]; self.tabBarItem.accessibilityIdentifier = @"MyID"; self.tabBar.items[0].accessibilityIdentifier = @"MyID"; Nothing works. I always get empty Do you have any idea on how to implement the identifier I want for each single element of the tabbar? Thanks
Posted
by
Post not yet marked as solved
0 Replies
148 Views
Since upgrading to Xcode 13.3.1 unit-tests doesn't start anymore with "execute in parallel" set on. "XYZ" (1234) encountered an error (Unexpected TestCaseDidStart...)
Posted
by
Post not yet marked as solved
0 Replies
121 Views
When using XCTestExpectation, is there a difference between writing XCTAssert in sink or after wait? My example: let exp = expectation(description: "test") repository.delete(id: "2") .sink { [unowned self] in print("in sink closure") XCTAssertEqual(repository.object.count, 2) XCTAssertFalse(repository.object.contains { $0.id == "2" }) exp.fulfill() } .store(in: &cancellables) wait(for: [exp], timeout: 1.0) print("after wait") XCTAssertEqual(repository.object.count, 2) XCTAssertFalse(repository.object.contains { $0.id == "2" }) XCTAssert in sink succeeds, but XCTAssert after wait fails. So I thought the code after wait would be executed without waiting for wait. However, the console outputs the following in the following order in sink closure after wait Why does XCTAssert fail after wait?
Posted
by
Post not yet marked as solved
0 Replies
190 Views
Hi everybody: I want to Sign In with Apple in simulator with the purpose of double check the flow with UITesting, but I'm not able to do it. The simulator stuck in password textfield once the password is entered in, and the button is accepted. (The button appears disable for a second and then again it is going enable, but the textfield loading still appears). are there any trick to test it from UI with simulator? Thanks in advance.
Posted
by
Post not yet marked as solved
1 Replies
226 Views
Hi, Currently I'm working on Apple Watch ui testing using XCTest and I need to interact with Apple Watch's Home screen in order to perform different actions with complications. As I know from my iOS experience, Home Screen can be by using bundle id "com.apple.springboard". I need something similar but for watchOS.
Posted
by
Post not yet marked as solved
1 Replies
253 Views
Hi, Currently I'm working on Apple Watch ui testing using XCTest and I need to interact with Apple Watch's Home screen in order to perform different actions with complications. As I know from my iOS experience, Home Screen can be by using bundle id "com.apple.springboard". I need something similar but for watchOS.
Posted
by