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
4 Replies
1.7k Views
I need to trigger accessory connected notification for testing purposes, for which I need to create an instance of EAAccessory class and send a fake accessory. How do I do that? I get this exception `EAAccessoryInitException`. *** Terminating app due to uncaught exception ‘EAAccessoryInitException’, reason: ‘-init not supported. EAAccessoryManager is responsible for creating all objects.’ Is there any other way to test this behavior?
Posted
by
Post not yet marked as solved
2 Replies
1.2k Views
For my iOS app XCUITest, I'm trying to run parallel distributed testing tests and some of the cases in the test suite fails with the below reason: "Failure requesting automation session for com.app:10909: Timed out waiting for target to check in."This error comes up when the tests are trying to execute XCUIApplication().launchIf I execute in random order or without parallelization all tests are passing as there are no dependency between tests.Has anyone faced this issue and fixed it?
Posted
by
Post not yet marked as solved
1 Replies
442 Views
I'd like to use the new localization screenshots and test plans feature to take screenshots in different languages, for the app store. I end up with images that are half one screen and half another, like it's some kind of timing issue. My test code is below. Is it missing something that would give it the right timing on the screenshots?I wrote a UI test and set "Localization Screenshots" to "On" in the test plan's settings. The UI test walks through a few screens and the resulting test report has a few image files attached labeled "Localization screenshot". Some images are are split so that the left side is one view controller and the right side is another, like it captured a push navigation transition. Another image has two overlaid screens, each half faded.I'm running in the simulator. My test code looks like: func testTakeScreenshots() { let app = XCUIApplication() app.launch() // At workouts page app.tables["workouts"].cells.element(boundBy: 0).tap() // At first workout app.navigationBars.buttons["edit"].tap() // At workout edit screen, click first exercise app.tables.cells.element(boundBy: 0).tap() ... }
Posted
by
Post not yet marked as solved
1 Replies
383 Views
In our application, we are having custom logging. When the project is compiled locally on the XCode, I can see that those logs are appearing properly on Xcode Console (Debug Area). Also, when compiling the UITest scheme, the application logs are appearing as expected. But when I run a uitest, then only test runner logs are showing up in debug area. When I select My application target then it's not having application logs.Is this something expected? Is there any setting that I have to turn ON with in Xcode?Any help will be really appreciated.Thanks
Posted
by
Post not yet marked as solved
3 Replies
718 Views
I am using XCUITesting with performance measurement blocks to ensure that certain tasks in the app don’t regress in how long they take to complete. I’m doing this with OS signposts, by logging signposts when the task begins and ends in the app, and then using the XCTOSSignpostMetric in the test to check the time elapsed against the baseline. This works well except for one case: I have a static library that defines a class that does certain work. That class is instantiated with the StaticString signpost name that should be used when it logs signposts. The app passes this StaticString parameter into the static library class’s constructor. In this case, the XCTOSSignpostMetric in the test never detects the signpost being logged. If I hard code a signpost name in the static library as a StaticString literal, it works. If I pass in a StaticString defined outside the static lib, the test doesn’t detect it when it is logged. Either way, I do see the signposts in Instruments. Only XCTOSSignpostMetric seems to not detect it in this non-functioning case. It seems like a bug, but I’m wondering if anyone else has encountered this or can suggest a workaround.
Posted
by
Post not yet marked as solved
10 Replies
2.1k Views
My app setup is similar to https://forums.developer.apple.com/thread/133495 - I have an app target, some frameworks and local Swift packages. I added the package tests to the test plan.Now, when "gather code coverage" is just "on" in the test plan, I get code coverage from the package targets. But when I selectively add only the targets I want (including the package targets), the package targets don't show up in the code coverage results at all.Gathering code coverage for all targets isn't really what I want, because that includes a lot of third-party code that I'm not interested in.
Posted
by
Post not yet marked as solved
3 Replies
885 Views
Sometimes when running UI tests, the tests fail on keyboard input because the "Speed up your typing" keyboard tutorial overlay obscures the keys. This happens randomly, but sometimes repeatedly. Is there a way to turn this off? Checking for this tutorial before any keyboard input would significantly complicate tests just to check for one edge case...
Posted
by
Post not yet marked as solved
0 Replies
337 Views
Hi, I found problem when my app run UITests using Xcode 12 beta 12A6159. It always give this error Failed to get matching snapshot: Error getting main window kAXErrorAPIDisabled I tried to po app.otherElements or app.collectionViews but always give me empty result. Here is my code snippet: let app = XCUIApplication() app.launch() let homeCollection = app.collectionViews.firstMatch homeCollection.waitForExistence(timeout: 10) measure(metrics: [XCTOSSignpostMetric.scrollDecelerationMetric]) { homeCollection.swipeUp(velocity: .fast) }
Posted
by
Post not yet marked as solved
6 Replies
2.7k Views
When trying to delete and install the app, unable to launch the app in from xcode 11.4.1 and later versions. This scenario is working fine till xcode 10.2.1. Below are the error details: The request to open "org.*******.enterprise.debug" failed. Domain: FBSOpenApplicationServiceErrorDomain Code: 1 Failure Reason: The request was denied by service delegate (SBMainWorkspace) for reason: NotFound ("Application is unknown to FrontBoard"). User Info: {   BSErrorCodeDescription = RequestDenied;   FBSOpenApplicationRequestID = 0x8660; } The operation couldn’t be completed. Application is unknown to FrontBoard. Domain: FBSOpenApplicationErrorDomain Code: 4 Failure Reason: Application is unknown to FrontBoard. User Info: {   BSErrorCodeDescription = NotFound; } System Information: macOS Version 10.15.4 (Build 19E287) Xcode 11.4.1 (16137) Does anyone know how this might be resolved?
Posted
by
Post not yet marked as solved
11 Replies
4.0k Views
We have noticed a 4X increase in app launch time for UI tests in Xcode 12 compared with Xcode 11. App launch times were typically ~15 seconds and are now approximately ~75 seconds. When I pause the debugger, I can see that it is hanging on XCUIApplication.launch() Is this a known issue? Are others experiencing this issue? Should we be expecting to see this increase? Are there mitigations we can implement to improve the launch time?
Posted
by
Post not yet marked as solved
6 Replies
2.8k Views
I am getting this UI Testing error when I try to adjust the month-year calendar in new compact date picker. Assertions: Assertion Failure at ***.swift:4: Unsupported picker wheel "2020" PickerWheel of type 6 My code is let compactDatePicker = self.app.descendants(matching: .any)["Date Picker"] compactDatePicker.tap() self.app.buttons["Show year picker"].tap() self.app.pickerWheels.element(boundBy: 1).adjust(toPickerWheelValue: "2020") self.app.pickerWheels.element(boundBy: 0).adjust(toPickerWheelValue: "September") self.app.buttons["Hide year picker"].tap()     let calendarDateButton = self.app.buttons.containing(NSPredicate(format: "label CONTAINS '%d %@'", 27, "September") calendarDateButton.tap() self.app.swipeDown() I have no idea what this error means. I am just guessing this should be a bug in Xcode 12.0. Anyone can help me to have a look on this?
Posted
by
Post not yet marked as solved
2 Replies
531 Views
I am trying to access UITabBarItem using accessibilityIdentifier in UITestCases but its not working in iOS 11 and iOS 12. On iOS 13 it's working. I have checked this with Appium and Accessibility Inspector. In both its not showing accessibilityIdentifier applied for UITabBarItem. tabBarItem1.accessibilityIdentifier = "tabBarItem1" I have tried calling this in viewDidLoad, viewWillAppear, viewDidAppear. but same results for me. has anyone faced this issue ?
Posted
by
Post marked as solved
2 Replies
590 Views
Our UI Tests require interaction with physical devices. There is a Flask server on Raspberry Pi in our local network which is able to interact with those devices. From within XCTestCase we send request to this server and server does its job. Everything works on iOS13, but on iOS14 there is always an error that there is no internet connection -1009. Application's and Test's info.plist has: App Transport Security Settings: Allow Arbitrary Loads: True Allows Local Networking: True Privacy - Local Network Usage Description I suspect this is something with 'Local Network Premission' but setting its description in does not solve the problem. Thanks
Posted
by
Post marked as solved
2 Replies
1.3k Views
How do I do unit test with multiplatform app? I created a new project and add a class with one object. class MyClass {   static var apples: [Int] = [1,2,3] } When I want to do unit test, it fails ... import XCTest @testable import MyApp class Tests_macOS: XCTestCase { 		func testExample() throws { 				XCTAssertEqual(MyClass.apples.count, 3) 		} } Errors Undefined symbols for architecture x86_64:  "MyApp.MyClass.apples.unsafeMutableAddressor : [Swift.String]", referenced from:    implicit closure #1 () throws -> Swift.Int in Tests_macOS.Tests_macOS.testExample() throws -> () in Tests_macOS.o  "type metadata accessor for MyApp.MyClass", referenced from:    implicit closure #1 () throws -> Swift.Int in Tests_macOS.Tests_macOS.testExample() throws -> () in Tests_macOS.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) Undefined symbol: MyApp.MyClass.apples.unsafeMutableAddressor : [Swift.String] Undefined symbol: type metadata accessor for MyApp.MyClass
Posted
by
Post not yet marked as solved
8 Replies
3.8k Views
We are building an iOS app that connects to a device using Bluetooth. To test unhappy flow scenarios for this app, we'd like to power cycle the device we are connecting to by using an IoT power switch that connects to the local network using WiFi (a Shelly Plug-S). In my test code on iOS13, I was able to do a local HTTP call to the IP address of the power switch and trigger a power cycle using its REST interface. In iOS 14 this is no longer possible, probably due to new restrictions regarding local network usage without permissions (see: https://developer.apple.com/videos/play/wwdc2020/10110 ). When running the test and trying a local network call to the power switch in iOS14, I get the following error: Task <D206B326-1820-43CA-A54C-5B470B4F1A79>.<2> finished with error [-1009] Error Domain=NSURLErrorDomain Code=-1009 "The internet connection appears to be offline." UserInfo={_kCFStreamErrorCodeKey=50, NSUnderlyingError=0x2833f34b0 {Error Domain=kCFErrorDomainCFNetwork Code=-1009 "(null)" UserInfo={_kCFStreamErrorCodeKey=50, _kCFStreamErrorDomainKey=1}}, _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <D206B326-1820-43CA-A54C-5B470B4F1A79>.<2>, _NSURLErrorRelatedURLSessionTaskErrorKey=("LocalDataTask <D206B326-1820-43CA-A54C-5B470B4F1A79>.<2>"), NSLocalizedDescription=The internet connection appears to be offline., NSErrorFailingURLStringKey=http://192.168.22.57/relay/0?turn=on, NSErrorFailingURLKey=http://192.168.22.57/relay/0?turn=on, _kCFStreamErrorDomainKey=1} An external network call (to google.com) works just fine in the test. I have tried fixing this by adding the following entries to the Info.plist of my UI test target: <key>NSLocalNetworkUsageDescription</key> <string>Local network access is needed for tests</string> <key>NSBonjourServices</key> <array> <string>_http._tcp</string> </array> <key>NSAppTransportSecurity</key> <dict> <key>NSAllowsArbitraryLoads</key> <true/> </dict> However, this has no effect. I have also tried adding these entries to the Info.plist of my app target to see if that makes a difference, but it doesn't. I'd also rather not add these entries to my app's Info.plist, because the app does not need local network access. Only the test does. Does anyone know how to enable local network access during an iOS UI test in iOS14?
Posted
by
Post not yet marked as solved
0 Replies
542 Views
I am trying to test paymentCanceled use case using SKTestSession. Here is the code I am using: var session = try? SKTestSession(configurationFileNamed: "PurchasesWithProducts") session?.disableDialogs = true session?.failTransactionsEnabled = true session?.failureError = .paymentCancelled The problem is whatever value I set to failureError the returned error is always: Error Domain=SKErrorDomain Code=0 "UNKNOWN_ERROR" UserInfo={NSLocalizedDescription=UNKNOWN_ERROR, NSUnderlyingError=0x60000138ee50 {Error Domain=ASDServerErrorDomain Code=9999 "The server encountered an error" UserInfo={NSLocalizedDescription=The server encountered an error}}}) To me it seems like failureError property is not respected. Also the docs are a bit misleading: You can force an error by setting failTransactionsEnabled to true and setting failureError value to one of these supported error codes: SKError.Code.unknown, SKError.Code.invalidOfferIdentifier, SKError.Code.invalidSignature, SKError.Code.missingOfferParams, SKError.Code.invalidOfferPrice. At first I would think that I can only use those error codes but even by using those codes I am getting the same error.
Posted
by
Post not yet marked as solved
1 Replies
566 Views
Hello, Did anyone succeed to chose a specific Configuration from the Test Plan to be used for running tests using Test Plans on a Xcode Bot? I tried multiple possibilities with no success. We can pass to xcodebuild the following parameter and it will run the desired test plan "test -testPlan NameOfTheTestPlan", but if there is only one configuration under the test plan , all the tests will run twice because of the default "Share Configurations" Any ideas? Thanks!
Posted
by
Post not yet marked as solved
1 Replies
1.3k Views
Hi! I've run into some problems with running unit tests for iOS framework project recently. It had worked, but it stopped since one of the command line tool's updates. When I try to run unit tests with a command line (xcodebuild) I get 'Failed to create a bundle instance representing' error. But only for an iOS framework project and when -derivedDataPath is set with a custom path. xcodebuild -project TestFramework.xcodeproj -scheme TestFramework -derivedDataPath ./output  -destination 'platform=iOS Simulator,id=***' clean build test In the Xcode I can run tests without any problems. When I remove -derivedDataPath parameter, it works like a charm as well. On the other hand, unit tests for an iOS application project works from command line and Xcode, whenever I set -derivedDataPath flag or not. Has any of you been dealing with the same problem? Or do you know any solution? Or is it a known issue? Cheers! PS. $ xcodebuild -version  Xcode 12.4 Build version 12D4e $ sw_vers  ProductName: Mac OS X ProductVersion: 10.15.7 BuildVersion: 19H2
Posted
by
Post not yet marked as solved
3 Replies
859 Views
I've noticed that XCTMemoryMetric &amp; XCTCPUMetric seem to record empty or nonsensical data when running a UI test flow for an XCUIApplication. I'm attempting to test the memory and CPU footprint for a SwiftUI iOS app using the following code: Swift func testBasicFlowMemory() throws{ let app = XCUIApplication() app.launch() var metrics:[XCTMetric] = [] metrics.append( XCTClockMetric() ) metrics.append( XCTMemoryMetric(application: app) ) metrics.append( XCTCPUMetric(application: app) ) self.measure(metrics: metrics){ /*Method which uses XCUI API to test the app instance*/ self.runBasicFlowTest(app: app) } } When I run the test above, I notice runBasicFlowTest is executed 6 times (even though the metics only record 5 values. Of the three metrics I wanted to track only XCTClockMetric returned meaningful data: [Clock Monotonic Time, s] values: [114.728229, 114.944770, 121.813337, 116.394432, 117.491242] XCTMemoryMetric mostly recorded 0.0 or nonsense data: [Memory Physical, kB] values: [3596.288000, 0.000000, 0.000000, 0.000000, 0.000000] [Memory Peak Physical, kB] values: [0.000000, 0.000000, 0.000000, 0.000000, 0.000000] XCTCPUMetric likewise recorded 0.0 or nonsense data: [CPU Instructions Retired, kI] values: [0.000000, 206223944.266000, 0.000000, 0.000000, 211895544.471000] [CPU Cycles, kC] values: [0.000000, 252096240.472000, 0.000000, 0.000000, 257352232.305000], [CPU Time, s] values: [0.000000, 86.585296, 0.000000, 0.000000, 0.000000] I'm on Xcode Version 12.4 (12D4e), and my app is targeting iOS 14.4 on a simulated iPhone 11 Pro. Has anyone had any luck using XCTMetrics with UI Tests?
Posted
by