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

Posts under XCTest tag

160 Posts
Sort by:

Post

Replies

Boosts

Views

Activity

How to run `xctest` bundle - or how to add `entitlement` to test?
I am writing a SPM based project for MacOS. In this project? I need to access MacOS Keychain. I am write a swift test built by SPM testTarget(). I can see it generates a bundle ./.build/x86_64-apple-macosx/debug/MyProjectTests.xctest with an executable: % file ./.build/x86_64-apple-macosx/debug/MyProjectPackageTests.xctest/Contents/MacOS/MyProjectPackageTests ./.build/x86_64-apple-macosx/debug/MyProjectPackageTests.xctest/Contents/MacOS/MyProjectPackageTests: Mach-O 64-bit bundle x86_64 This bundle file cannot be executed. How can I execute its tests? I tried with xcodebuild test-without-building -xctestrun ./.build/x86_64-apple-macosx/debug/MyProjectPackageTests.xctest -destination 'platform=macOS' without any chance. Obviously the next question is can I 'simply' add entitlement to this bundle with codesign to fix my enttilement error. My error when running the test is A required entitlement isn't present.
1
0
592
Jan ’24
Profiling UITest case always terminates in the middle of testing, which never occurs during normal testing?
In my iOS project, there is an infrequent crash related to virtual memory problem. Therefore, I plan to use UITest in combination with Product/Perform Action/Profile "TestCaseName" to conduct Game Performance-type testing. This allows the automatic testing to continuously operate until the profile stops recording upon a crash. This enables me to observe the various states of the program at the time of the crash. However, I have found that the UITest using Profile is highly unstable. The UITestCase often terminates unexpectedly during execution, leading to failed tests (Instruments is still working). Sometimes, the app is terminated immediately after startup. It seems that the use of sleep() in the code can easily cause interruption issues, which do not occur during normal UI testing. I am wondering if anyone has experience using Profile for UITest and whether they have encountered the issues I described." Working Environment: XCode14.3.1, iPhone Device iOS17.2
0
0
513
Jan ’24
initWithURL vs initWithBundleIdentifier giving different permissions
I am directed from https://discuss.appium.io/t/create-multiple-instances-of-the-same-app/41266/14?u=lxnm, the context is that Appium max2 driver is implementing the activating of MacOS app using XCTest API methods. There are 2 ways to implement the activating of the app, using app path (calling initWithURL) and bundle id (calling initWithBundleIdentifier). This pull request shows how the XCTest methods are called when using the Mac2 driver, specifically in the file WebDriverAgentMac/WebDriverAgentLib/Routing/FBSession.m. The problem is that I am able to launch my MacOS app(it is a company app) using bundle id, but when I launch the app with app path, I receive XcodeBuild errors: [WebDriverAgentMac] [xcodebuild] XCTExpectFailure: matcher accepted Assertion Failure: Failed to launch com.company.companyApp: You do not have permission to run the application “companyApp”. You don’t have permission. To view or change permissions, select the item in the Finder and choose File > Get Info. (Underlying Error: The operation couldn’t be completed. Operation not permitted) I followed this to enable R+W permissions to all users, but the same error is displayed.
3
0
719
Jun ’24
Interacting with macOS SwiftUI Graphical DatePicker in UI Tests
I am currently working on a SwiftUI project and I am trying to write a UI test for a DatePicker. However, I am having trouble interacting with the DatePicker in the test. Here is the code for the DatePicker in my SwiftUI view: DatePicker("Date", selection: $date, displayedComponents: [.date]) .datePickerStyle(.graphical) .labelsHidden() .frame(width: 150) And here is the code for my UI test: func testSelectingDateChangesMainView() throws { let app = XCUIApplication() app.launch() let dateDatePicker: XCUIElement = app.windows["mainUI-AppWindow-1"].datePickers["Date"] dateDatePicker.click() let col = dateDatePicker.descendants(matching: .any) print("\(dateDatePicker.debugDescription), \(col.debugDescription)") } When I run the test, it seems like the DatePicker does not have any descendants, so I am unable to select a date. Does anyone have any suggestions on how I can interact with a .graphical style DatePicker in a UI test? Any help would be greatly appreciated. Thank you, OnlyForF1
3
1
581
Jan ’24
Aggregating Xcode Metrics Measure Test Reporting in Xcode or Command Line
Hi! I'm experimenting with Xcode performance testing. I'm specifically focusing on XCTMemoryMetric. I have a demo project with two performance tests that do some work wrapped with XCTestCase.measure. I can navigate to my Report navigator in Xcode and see the memory footprints of these two tests… but I only see how to view them individually: From this screen it looks like I have the ability to Export this benchmark to CSV. My question is what my options are for viewing all my XCTestCase.measure tests together. I see the Duration for both tests… but I don't see any way to view the memory benchmarks for all my tests in just one place: Ideally… I would also like to run these tests and produce one CSV file (or XML or JSON) report. I'm not opposed to using command line for this (if the option exists there) but Xcode also works for me. I'm also flexible whether or not these tests live in a Swift Package or directly in an Xcode project if that makes any difference. Any more advice about how to set up these performance tests? Thanks!
2
0
526
Jan ’24
Accessibility Identifier lost on Xcode 15.1 and iOS 17.2
We recently updated to Xcode 15.1 and start using iOS 17.2 simulator and ran into a blocker issue with out UITests. Setting accessibility identifier on a UIButton with image no longer works. It seems iOS automatically set the label to the file name used for the button and ignore (overwrite) the id and value we set in code. iOS 17.0 with Xcode 15.1 still works. I spent 2 days on this and still cannot find a solution. Anyone had similar issue? I did file a feedback https://feedbackassistant.apple.com/feedback/13515676 Thanks!
0
0
554
Jan ’24
Issue with running UI automation tests
Hi, I have created the applications (debug and runner) using xcodebuild. I have tried it both using Xcode 14.3.1 and 15.1. I used the xcodebuild build-for-testing... with -configuration set to Debug (I've also built the apps by omitting the -configuration altogether). The configurations are set correctly in Xcode, and I can run the automation from Xcode and running xcodebuild. I am using a different tool other than xcodebuild to run the tests (I'm using go-ios https://github.com/danielpaulus/go-ios). The issue I'm having is that right at the beginning of the test, I'm getting this error "Failed to retrieve test configuration from IDE". I know it's something to do with the two apps (debug and runner appp), because I've made comparison to a brand new application I had created using Xcode. I assume this is something to do with the build configurations we have in the app (Debug or Release), but they're all correctly set in Xcode, and I can also run the tests (albeit using the xctestrun file) without any problems. Do you know what could be causing this? In particular, what could cause the above error to be generated? Any help (however related or unrelated you think it could be) is greatly appreciated. Thanks in advance. Sep
1
0
617
Jan ’24
UI test runner gets "unidentified developer" warning
I'm working on this project for the first time in a while, so I'm not sure if this issue started with Xcode 15 or what, but now when I run my UI tests I get the warning "“MyAppUITests-Runner” is from an unidentified developer and differs from previously opened versions. Are you sure you want to open it?" Code signing identity for the UITest target is set to "sign to run locally". Changing it to "Apple Development" doesn't help, and neither did a clean build. How do I need to configure it to eliminate the warning?
0
0
466
Jan ’24
XCode Cloud ignores maximumTestExecutionTimeAllowance specified in test plan
I've created a test plan that includes the below configurations. {"configurations": [{ ... "options": { "defaultTestExecutionTimeAllowance" : 60, "maximumTestExecutionTimeAllowance" : 60, "testTimeoutsEnabled" : true }], "defaultOptions": { "defaultTestExecutionTimeAllowance" : 60, "maximumTestExecutionTimeAllowance" : 60, "testTimeoutsEnabled" : true, .... } ... } If I write a test that intentionally will take longer than 60 seconds, executing the test locally fails as expected with the error message Test exceeded execution time allowance of 1 minute let app = XCUIApplication() app.launch() let missingButton = app.buttons.firstMatch XCTAssertTrue(missingButton.waitForExistence(timeout: 120), "When a timeout is > 60 seconds, the default max setting of 60 seconds is still enforced") However, when this test is run using this test plan in XCode Cloud, it reports that it took 124 seconds to run and there is no message about exceeding execution time allowance. How can the test step be configured in XCode Cloud to ensure that the execution time allowances are respected?
1
0
2.2k
Jan ’24
Not able to capture individual elements on the home screen of iOS17 device with xCode 15 and appium2
Not able to capture the individual elements on the iOS 17 device (using iPhone 14) using Appium inspector. It always capture/ highlight the entire home screen as one element. As per the Appium support team, this is an issue with XCTest framework. The getPageSource() method is loading indefinitely. Please refer below links- https://github.com/appium/appium-inspector/issues/1225 https://github.com/appium/appium/issues/19429 I am using below configurations- MacBook AIR (Apple M2 2022) - Sonoma 14.1.2 Appium Version - 2.0.1 Appium Inspector - 2023.11.1 Available drivers: xcuitest@5.11.8 (automationName 'XCUITest') uiautomator2@2.34.2 (automationName 'UiAutomator2') Node - v21.3.0 npm - 10.2.4 Xcode - Version 15.0.1 (15A507) Could you please clarify if this is an existing/known issue or am I missing something? Note - Let me know if any other information needed from my end for better clarity. I will appreciate your inputs on this. Thanks in advance!
1
1
350
Dec ’23
How to make a unit test in a swift package with different current locales?
I have a swift package with a function that formats a HKQuantity using the current locale. I want to write a unit test with all locales the package is localized for. I do not want to insert a separate locale parameter, I want to test the function with the current locale as I use the function only with the current locale. Can I set the current locale for this use case in the code somehow? Example: do { let quantity = HKQuantity(unit: unit, doubleValue: 0.955) let result = quantity.formattedAsOxygenSaturation(precision: .better) let expectedResult = "95.5 %" XCTAssertEqual(result, expectedResult) } Thanks!
1
0
407
Nov ’23
Appium inspector is very slow to load my device, which makes me unable to locate the element.
I facing the similar issue where Appium inspector is very slow to load my device, which makes me unable to locate the element. MacBook Pro (Apple M1 Prod) - Ventura 13.6 Appium Version - 2.2.1 Appium Inspector - 2023.11.1 appium driver list ✔ Listing available drivers uiautomator2@2.34.0 [installed (npm)] Node - v21.1.0 npm - 10.2.0 Xcode - Version 15.0.1 (15A507) Simulator - iPhone 15 (17.0) { "appium:deviceName": "iPhone 15", "appium:app": "sim.app PATH", "appium:automationName": "XCUITest", "appium:platformName": "iOS", "appium:platformVersion": "17.0", "appium:bundleId": "com.xxxxxxx", "appium:usePrebuiltWDA": false } https://github.com/appium/appium/issues/19382
0
0
469
Nov ’23
WWDC18 "Testing Tips & Tricks" approach to testing notification center makes unit tests share state
In the following code, test 1 (test_postNotification) fails while test 2 (test_notificationsArePostedOnTheMainQueue) passes. What concerns me, though, is that if I substitute the lines "let result = XCTWaiter.wait(for: [expectation], timeout: 0); XCTAssertEqual(result, .timedOut)" of test 2 with "wait(for: [expectation], timeout: 0.1)", then test number 1 passes. I have cleaned the build folder and restarted Xcode and my computer, but the issue persists. This concerns me because I would have said that the tests of the NotificationPosterTests class were isolated, but apparently they are not, since changing test 2 makes test 1 go from failing to passing. Is this expected behavior? import Foundation import XCTest extension Notification.Name { static let menuPostRequest = Notification.Name("menuPostRequest") static let editingOrderError = Notification.Name("editingOrderError") } class NotificationPoster { let notificationCenter: NotificationCenter init(notificationCenter: NotificationCenter = .default) { self.notificationCenter = notificationCenter } func postNotification(_ notification: Notification) { let _notificationCenter = notificationCenter // you can't use optional chaining nor conditional unwrapping on self to reference self.notificationCenter in the dispatch block because self is nil when self.postNotification(_:) is called DispatchQueue.main.async { _notificationCenter.post(notification) } } } final class NotificationPosterTests: XCTestCase { private var sut: NotificationPoster! private var notificationCenter: NotificationCenter! override func setUp() { super.setUp() notificationCenter = NotificationCenter() sut = NotificationPoster(notificationCenter: notificationCenter) } override func tearDown() { notificationCenter = nil sut = nil super.tearDown() } func test_postNotification() { let notification = Notification(name: .menuPostRequest) let expectation = XCTNSNotificationExpectation( name: notification.name, object: notification.object, notificationCenter: notificationCenter ) sut.postNotification(notification) wait(for: [expectation], timeout: 0.1) // don't make it 0.01 } func test_notificationsArePostedOnTheMainQueue() { let notification = Notification(name: .editingOrderError) let expectation = XCTNSNotificationExpectation( name: notification.name, object: notification.object, notificationCenter: notificationCenter ) sut.postNotification(notification) let result = XCTWaiter.wait(for: [expectation], timeout: 0) XCTAssertEqual(result, .timedOut) } }
0
0
348
Nov ’23
xcodebuild failure: xcodebuild failed with code 70
webdriver: Request failed with status 500 due to An unknown server-side error occurred while processing the command. Original error: Unable to launch WebDriverAgent because of xcodebuild failure: xcodebuild failed with code 70 [0-0] xcodebuild error message: [0-0] . Make sure you follow the tutorial at https://github.com/appium/appium-xcuitest-driver/blob/master/docs/real-device-config.md. Try to remove the WebDriverAgentRunner application from the device if it is installed and reboot the device. I'm using macOs 13.6 xcode 14.3 Iphone Os 16.7.2 running with webdriverIO still getting this error can anyone help me in this please.
0
0
965
Nov ’23
XCTest tvOS UI testing: focus on element without navigation
For iOS, it can send tap() event to an element, that means it doesn't need to explicitly navigate the UI to focus on the element first. Based on the document "Available in iOS and for Touch Bar interactions in macOS.", such events (like tap, click) don't support for tvOS , seems like for tvOS XCTest, I have to explicitly navigate the UI to locate the element first, and then send remote event like "select()" to trigger the click event. Anyone know is this the only way for tvOS?
0
0
386
Nov ’23