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

Posts under XCTest tag

161 Posts
Sort by:

Post

Replies

Boosts

Views

Activity

How to test against non-iOS17 SDKs with Xcode15 series command line tools?
Hello. I would like to perform UnitTest using the xcodebuild command in Xcode15 series, but it does not work. Specifically, I want to run the test on an iPhone 14 with iOS 16.4 simulator environment, but for some reason it fails due to a certificate mismatch. For some reason I don't want to test on iOS17, so I specify iOS16.4. The following is a pseudo-command to run the test. Thank you! xcodebuild -workspace AppWorkspace.xcworkspace -scheme App -sdk iphonesimulator -destination "platform=iOS Simulator,name=iPhone 14,OS=16.4" -configuration TestConfiguration test
1
0
533
Oct ’23
unit tests executing App's init() function?
I am adding unit tests to an existing SwiftUI project, and I've discovered the main 'App' struct's init() function is being called automatically before the individual test functions are run. (1) Is this expected behavior? (2) Is there a way to disable the App struct's init() from being called when running unit tests? In my use case, the App struct's init() does some initialization, including network activity, that I would prefer not happen during unit tests. Below is simplified version of what I am seeing beginning with the App struct: import SwiftUI import os.log @main struct MyTest2App: App { init() { os_log("===== In MyTest2App.init() =====") } var body: some Scene { WindowGroup { ContentView() } } } Here is an example of a struct that I want to exercise in a unit test: import Foundation struct Foo { var name: String var count: Int init(name: String, count: Int) { self.name = name self.count = count + 1 } } Here is a unit test file: import XCTest final class MyTest2Tests: XCTestCase { override func setUpWithError() throws { } override func tearDownWithError() throws { } func testExample() throws { let foo = Foo(name: "Apple", count: 3) XCTAssertEqual(foo.count, 4) } } Here is part of the output console. The first line is the output from the App struct's init() function: 2023-09-27 10:41:26.798791-0700 MyTest2[66138:9429362] ===== In MyTest2App.init() ===== 2023-09-27 10:41:26.869966-0700 MyTest2[66138:9429362] ApplePersistenceIgnoreState: Existing state will not be touched. New state will be written to /var/folders/yg/vfstql350kqgzp17lmh11jg80000gn/T/com.ennetix.MyTest2/com.ennetix.MyTest2.savedState ...
1
0
633
Sep ’23
UI tests ran on simulator hang after a couple of minutes on M1 Mac
Hi there! I have a problem running UI tests on simulator, while using M1 Mac. After running for a couple of minutes, usually 3 minutes is enough, tests begin to fail every ±60s on XCUIApplication().launch() with Failed to terminate com.***.***:10552: Failed to terminate com.***.***:0 At this point, my simulator window does not even react to any manual input. That issue have been happening since Xcode 13. Unit tests are running completely fine though. Also, that issue does not appear on an Intel-based Mac. Sadly, I haven't found any information on that issue, however, there are some posts on SO mentioning the same issue, and only one of those posts has an answer, but it mentions unchecking 'Open using Rosetta' option for Xcode, which is not possible anymore. I would appreciate any advice, as I feel like I have tried everything already :(
0
2
602
Sep ’23
XCTest - addUIInterruptionMonitor not working on iOS 17
For my test automation project I use addUIInterruptionMonitor to keep the tests running smoothly. On iOS 17 this function seems to never get triggered. This code snippet contains a test that passes on iOS 15 and 16, but not on iOS 17 (tested on iPhone 12, iOS 15.5, iPhone 13 Mini, iOS 16.4.1 and iPhone 14 Pro, iOS 17.0). To run the test, disable wifi and cell data on the test device first. import XCTest class Interruptions: XCTestCase { func testCatchInterruption() { // Run test on device with wifi and cell data disabled var caughtInterruption = false addUIInterruptionMonitor(withDescription: "Generic Alert Handler") { element -> Bool in element.buttons["OK"].tap() caughtInterruption = true return true } let safariApp = XCUIApplication(bundleIdentifier: "com.apple.mobilesafari") safariApp.launch() // Expect to see pop-up saying "Cellular Data is turned off" safariApp.tap() sleep(5) // Pop-up should be gone XCTAssert(caughtInterruption) } }
9
3
2.4k
Feb ’24
iOS 17 Xcode 15 during Unit Test restoreCompletedTransactions always Fails
When calling SKPaymentQueue.default().restoreCompletedTransactions() during unit testing it always fails. It calls the restoreCompletedTransactionsFailedWithError with an error message: Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost." UserInfo={NSLocalizedDescription=The network connection was lost., NSUnderlyingError=0x600000eb2790 {Error Domain=kCFErrorDomainCFNetwork Code=-1005 "(null)" UserInfo={_kCFStreamErrorDomainKey=4, _kCFStreamErrorCodeKey=-4, NSErrorPeerAddressKey={length = 28, bytes = 0x1c1ef212 00000000 00000000 00000000 ... 00000001 00000000 }}}} This is/was working fine in iOS 16 and Xcode 14. Anyone else seen this issue?
3
1
1.2k
Oct ’23
XCUIElementTypeToolbar disappears from the page source
I have a problem with test automation of my company app. We're using an external tool called Appium to automate our app, but under the hood it's using XCTest to obtain page source, and it looks like it might not work correctly, or we're doing something wrong. The problem is that since iOS 16.4 XCUIElementTypeToolbar is completely disappearing from the page source XCTest provides to Appium. What's even stranger, it's working when the app is opened for the first time after the phone restart. But when the app is simply put to background and opened again, this toolbar is no longer recognizable and instead of having XCUIElementTypeToolbar with arrows up and down and "Done" button inside it, XCTest returns only XCUIElementTypeOther there with nothing inside. And the only thing to have it found again is to restart the phone. It's happening in WebView. We are aware that since iOS 16.4 inspectable for WebViews is turned off by default (link to docs), but we have it implemented, and it did not help. It was working perfectly correct on iOS 16.3 and below, but completely stopped working on iOS 16.4 and above. Do you have any ideas if it might be anything done incorrectly on our side, or it's maybe some kind of bug in XCTest?
1
0
267
Sep ’23
Preboot simulators in parallel testing
We run our UI tests in parallel on 3 simulators, and I noticed, that the first one starts executing tests way faster then the rest (because others are still booting, I guess). Also we have our backend server running locally which is used in UI tests. And here comes the problem - I noticed that tests on first simulator are often failing because of the problems with local backend(requests take much more time than usual) exactly in period when other simulators are still booting(tests stop failing when all 3 simulators started running tests). When I tried running tests without parallel simulators problems with backend disappear. So my guess is that simulators booting they take some resources(CPU, memory) which interferes with backend. So my question is there some way to start executing tests only when all parallel simulators are booted?
0
0
364
Sep ’23
[UI Tests] Trigger applicationWillTerminate() in UI Testing
Currently I'm facing an issue in creating UI tests: When users terminate the app, applicationWillTerminate() will start some tasks. I want to verify this user flow in UI tests. However, just using terminate() does not actually call applicationWillTerminate(). A way I thought of: In UI test, mock user actions by opening app switcher. Then swipe up the app in app switcher and close the app. However, I haven't found a way to open app switcher in UI testing. Could anyone help me on this? Thank you.
0
0
289
Sep ’23
Unit Testing with Swift Data
Hi, I'm wondering if anyone has a suggestion as to how to access a ModelContainer in the Unit Testing class. I tried making a class to only instantiate the model container once, but it seems that Xcode is running this class in two different contexts (and they don't communicate): once when the test launches and then again from inside the XCTestCase subclass. If I try to save, I get an error about the two containers not being the same or a ton of validation errors. Appreciate any help to point me in the right direction. import SwiftUI import SwiftData @main struct MyApp: App { var body: some Scene { WindowGroup { ContentView() } .modelContainer(MyModelContainer.shared.container) } } @MainActor final class MyModelContainer { private init() { } @MainActor static var shared = MyModelContainer() var container: ModelContainer { if let internalContainer { return internalContainer } let schema = Schema([ MyModel.self ]) if ProcessInfo.processInfo.environment["XCTestConfigurationFilePath"] != nil { // testing internalContainer = previewContext(with: schema) } else if ProcessInfo.processInfo.environment["XCODE_RUNNING_FOR_PREVIEWS"] == "1" { // previewing internalContainer = previewContext(with: schema) } else { // production internalContainer = productionContext(with: schema) } return internalContainer! } private var internalContainer: ModelContainer? private func previewContext(with schema: Schema) -> ModelContainer { let modelConfiguration = ModelConfiguration(schema: schema, isStoredInMemoryOnly: true) do { let previewData = fetchPreviewData() let container = try ModelContainer(for: schema, configurations: [modelConfiguration]) let context = container.mainContext context.insert(previewData) try context.save() print("Loaded preview Container \(Date.now)") return container } catch { print("Could not create ModelContainer: \(error)") fatalError("Could not create ModelContainer: \(error)") } } func productionContext(with schema: Schema) -> ModelContainer { let modelConfiguration = ModelConfiguration(schema: schema, isStoredInMemoryOnly: false) do { print("Loaded FlashMeContainer") return try ModelContainer(for: schema, configurations: [modelConfiguration]) } catch { fatalError("Could not create ModelContainer: \(error)") } } } final class FlashMeTests: XCTestCase { @MainActor func testImport() throws { let data = NSDataAsset(name: "myCodableModelData")?.data let myCodableModel = try oldData!.decode(as: MyCodableModel.self) let myModel = myCodableModel.convertToSwiftData() let context = MyModelContainer.shared.container.mainContext context.insert(myModel) try context.save(myModel) } }
1
0
967
Sep ’23
Xcode 15 beta 8 | iOS 17 simulator RAM consumption significantly grows with each test run
When using Xcode 15 beta 8 / iOS 17 beta 8 simulator runtime, we can see in Activity Monitor that memory used by "SimRenderServer" proccess grows significantly with each UI test run. It reproduces even on an empty test, that just wait for a second without launching the application. Memory usage doesn't go down when test run finishes, so each consecutive run adds more to it. For example, create an new project with a UI test // The contents of the test don't actually matter func testExample() async throws { try await Task.sleep(for: .seconds(1)) } Run this test repeatedly 100+ times and observe the "SimRenderServer" process in the Activity Monitor. From what we observed it only reproduces when "Preferred Capture Format" in the test plan configuration is set to "Video". Setting it back to "Screenshot" does no result in growing memory usage. This was reproducible on macOS Sonoma beta / macOS Ventura 13.5.1. Feedback was submitted FB13098749.
3
7
1.7k
Dec ’23
Xcode 15 swipe in XCUITest not strong enough
All of our XCUI tests were passing with Xcode 14, now on 15 the swipeLeft() and swipeRight() calls don't seem "strong" enough to advance a UIPageControl to the next page. I've added in the velocity parameter and set it to .fast, but it still seems to fail. Anyone have any issues with this? No idea how to solve it other than explicitly stating touch points to then drag across the screen. Seems odd
0
0
431
Aug ’23
How can I effectively utilize XCUITest, , to conduct UI tests for iOS applications on the pCloudy platform?
In the context of iOS development, I've been leveraging XCUITest for UI automation. Given its native support from Apple, it's been instrumental in ensuring our app's UI consistency. Now, I'm looking to scale our testing efforts. How can I integrate XCUITest with the pCloudy platform to run these tests, and are there any specific considerations or best practices I should be aware of when doing so?
0
0
327
Aug ’23
Apple id account for testing, without 2 factor
I created a fake apple id for testing/automation, but it forced me to setup 2 factor. I need one without 2 factor. There seems to be no way to turn it off, and creating an "app specific password" isn't accepted in places like the simulator settings app, to sign in with Apple id Am I missing something? Is there another process to go through? Is there a way to turn off 2FA? Is there a dummy set of credentials that all simulators will accept to sign in with apple? etc
0
1
542
Aug ’23
GroupBox breaks ability of XCTest to find popovers?
I'm using Xcode 14.3.1 on macOS 13.5, and I've managed to reproduce my issue in a trivial application. All the project settings are left at the defaults for a macOS project. It looks like using a GroupBox breaks the ability of XCTest to find popovers connected to buttons (I suspect any UI element) inside the GroupBox. The debug console output from the code below lists 15 descendants from my window with the outside-the-GroupBox popover open, and one of them is definitely a popover. With the inside-the-GroupBox popover open, my window only shows nine descendants, and no popover (the rest of the difference is the popover's contents). It's simple enough I don't see what I could be doing wrong: import SwiftUI @main struct GroupBox_Popover_DemoApp: App { var body: some Scene { WindowGroup { ContentView() } } } struct ContentView: View { @State var outsidePopoverPresented: Bool = false @State var insidePopoverPresented: Bool = false var body: some View { VStack { Button("Outside GroupBox") { outsidePopoverPresented = true } .popover(isPresented: $outsidePopoverPresented, attachmentAnchor: .point(.leading), arrowEdge: .leading) { Popover(selected: .constant("Item A"), isPresented: $outsidePopoverPresented) } .padding() GroupBox { Button("Inside GroupBox") { insidePopoverPresented = true } .popover(isPresented: $insidePopoverPresented, attachmentAnchor: .point(.leading), arrowEdge: .leading) { Popover(selected: .constant("Item B"), isPresented: $insidePopoverPresented) } .padding() } } .padding() } } struct Popover: View { @Binding var selected: String @Binding var isPresented: Bool var body: some View { VStack(alignment: .leading) { Picker("", selection: $selected) { Text("Item A").tag("Item A") Text("Item B").tag("Item B") Text("Item C").tag("Item C") } .pickerStyle(.radioGroup) HStack { Spacer() Button("Cancel") { isPresented = false } } } .padding() .frame(width: 200) } } Then in my UI tests: import XCTest final class GroupBox_Popover_DemoUITests: XCTestCase { let mainWindow = XCUIApplication().windows override func setUpWithError() throws { continueAfterFailure = false XCUIApplication().launch() } func testPopovers() { let myDescendants = mainWindow.descendants(matching: .any) mainWindow.buttons["Outside GroupBox"].click() print("Window descendants with outside popover open:") print(myDescendants.debugDescription) mainWindow.popovers.buttons["Cancel"].click() mainWindow.buttons["Inside GroupBox"].click() print("Window descendants with inside popover open:") print(myDescendants.debugDescription) mainWindow.popovers.buttons["Cancel"].click() XCTAssert(true, "Test was able to hit cancel on both popovers.") } } Any ideas? Have I missed unchecking some "Ignore anything in a GroupBox" checkbox somewhere?
2
0
443
Sep ’23
[UI Testing] Verify Local Notifications When App Is Terminated
When my app is terminated, a warning is immediately presented to the users. I want to write a UI test for this behavior. I now have an XCUI application, I know I can terminate the app using terminate()from XCTest. After the XCUI application is terminated, a local notification should show up. Is there any way to access the notification and verify its existence and content?
1
0
439
Sep ’23
Is there any way to do Automation Testing of the macOS Login Flow?
We are working on developing an Authorization Plugin and I'm wondering if there is any way to automate the testing of the macOS login flow. In other words, something like Selenium for the login flow. I'm fairly certain the answer is "no" and that we need to any automated testing using a testing harness that runs our Auth Plugin. I'm basically doing due diligence on this now, so if anyone (especially from Apple) to weigh in, I'd be very grateful. Thanks, Francis
1
0
445
Aug ’23
Reset Authorization Status for WorkoutKit UITest
Hi! I'm working on WorkoutKit application and, when I'm running my UI Tests, I added an InterruptionMonitor for the WorkoutScheduler access popup, but the second time I launch the UITests, the Popup does not appear anymore cause the permission is guaranteed. The only solution that I've dounded is to add the app.resetAuthorizationStatus into the tearDown, but the parameter I should provide to this method, is a XCUIProtectedResource which does not contain any enum case that match with what I'm looking for. Does someone find a solution for that? Thanks, Victor Kπ
0
0
493
Aug ’23