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

Stop Xcode UITests running for light/dark and orientation permutations
I'm trying to run UITests just once. However, Xcode by default seems to run every permutation of light/dark color theme and device orientation and localizations. This means all UITests run 8 times (with 1 additional localization besides English). With tests that can take a couple minutes, the time to run all these adds up. Nothing in my UITest depends on the color theme, so I'd like to turn those permutations off always. The orientation and localization variations I might want sometimes but would generally also like to turn off. Questions: Can I disable any/all of these permutations? How can I get the locale string for the current run? I've tried using Locale(identifier: Locale.preferredLanguages.first!) but this just gets the device language, not the language being used on that run of the UI Test. Minimal reproducible example: Make a new SwiftUI app project in Xcode 15. Add a UI Testing Bundle target Add a localization in the general app settings, then add a string catalog called Localizable and at least one string's translation in that string catalog. Run a UITest Observe that it runs 8 times. Thanks for the help!
0
0
443
Nov ’23
Unable to resolve dynamically linked SPM target in Xcode 15
Hello all, we have ran into a problem building our test target that only occurs in Xcode 15. Our team has an SPM package that we use for shared code across our apps. This SPM package also has a dynamically linked target that contains utilities for testing. Within our app we link this test utilities library via Build Phases -> Link Binary with Libraries. On Xcode < 15 we have no problem resolving this library within our test target but on 15 we get a linking error if we try and and link it in the same way. If we remove this library from Build Phases -> Link Binary with Libraries then the linking error goes away, though of course then we will get errors as this library is still used within the XCTests of our test target. This is a simplified snippet of what our Package.swift looks like for our Swift Package let package = Package( name: "Library", platforms: [ .iOS(.v15) ], products: [ .library( name: "Library", targets: ["Library"] ), .library( name: "LibraryTestUtilities", type: .dynamic, targets: ["LibraryTestUtilities"] ) ], dependencies: [ // Dependencies declare other packages that this package depends on. ], targets: [ .target( name: "Library", dependencies: [ // Dependencies ]) , .target( name: "LibraryTestUtilities", dependencies: [ "Library" ]), .testTarget( name: "LibraryTests", dependencies: ["Library", "LibraryTestUtilities"]), ] ) I have tried to use the linker flags -ld_classic and -ld64 to no avail and I'm not sure where to go from here. It's not exactly obvious what linking change in Xcode 15 is resulting in the failure to resolve this library. We would appreciate any help on this matter, thanks!
1
1
893
Nov ’23
XCUITest: Reset authorization status for Biometrics?
Hello everybody. I have a pack of UI tests for the Biometrics authentication. And in the beginning of each test I need there to be no permissions for Biometrics (granted or denied). I found the resetAuthorizationStatus(for:) method that allows resetting everything except Biometrics :( Is there any way I can reset this permission without deleting the whole app in tearDown()?
0
1
698
Nov ’23
Xcode 15 performance tests ignore baseline
I'm trying to write performance tests with Xcode 15, but it's not working: even though setting a baseline seems to work (baseline data files are created), Xcode seems to ignore that baseline altogether whenever the test is run. As a result, performance tests never fail, no matter how bad performance gets. The source editor persistently displays a “No baseline average for Time” message. Has anyone else encountered this? Figured out how to sidestep the issue? Of note, the issue is easily reproduced: create a new (macOS in my case) app project with tests, add a dummy performance test; run the test once and save its baseline, and despite this subsequent tests will always succeed, even if you significantly slow the dummy test down. (I've filed this as FB13330139)
8
8
1.4k
Feb ’24
Unit test never runs
I am using Xcode 14.3.1 to develop and I have this unit test: @testable import bimb_authenticate_ios import XCTest import RxSwift import RxBlocking final class BindingApiClientTests: XCTestCase { private var disposeBag = DisposeBag() override func setUpWithError() throws { // Put setup code here. This method is called before the invocation of each test method in the class. disposeBag = DisposeBag() } override func tearDownWithError() throws { // Put teardown code here. This method is called after the invocation of each test method in the class. } func testPostInternetBankingCredentials_WhenSuccess() throws { XCTAssertEqual(1, 1) // let expectedParams = getExpectedIBCredsParams() // let expectedResponseModel = getExpectedIBCredentialsResponseModel() // let expectedData = try expectedResponseModel.jsonData() // Encodable to Data from JSON string // let expectedOutput = Single.just(expectedData) // let apiDataSource = MockDataSource(postOutputEvent: expectedOutput) // let sut = BindingApiClient(apiDataSource: apiDataSource) // // let expectation = expectation(description: "Expect Success!") // var actualResponseModel: IBCredentialsResponseModel? = try sut // .postInternetBankingCredentials(with: expectedParams) // .debug() // .toBlocking() // .first() // //// sut.postInternetBankingCredentials(with: expectedParams) //// .subscribe(onSuccess: { //// actualResponseModel = $0 //// expectation.fulfill() //// }) //// .disposed(by: disposeBag) //// //// wait(for: [expectation], timeout: 0.1) // XCTAssertEqual(expectedResponseModel, // actualResponseModel) // XCTAssertEqual(expectedParams.toJSONString(), // apiDataSource.postParamsInput?.toJSONString()) // XCTAssertTrue(apiDataSource.isPostCalled) } }` But it never run even though it only had one simple line, XCTAssertEqual(1, 1). It just started and will be processing ad infinitum. There is no log whatsoever except for the logs for successful compilations of pods and some swiftlint warnings. Can anybody help me? Thanks.
2
0
505
Nov ’23
Query - iOS 15 - WebDriverAgent prompts for passcode / Touch Id for "Enable UI Automation"
Sometimes our Test devices prompt for the passcode/touch ID to "Enable UI Automation", when WebDriverAgent is installed on the device using XCode. Then it works for some hours or days, but after some time it prompts again, even when WebDriverAgent is already installed. "Enable UI Automation" is already enabled in "Settings" - "Developer" menu. The devices are located remotely and we can't access them directly, so this is a big issue for us right now. Removing the passcode is no valid workaround since we have apps that require the passcode to start. Why it is random even when "Enable UI Automation" is already enabled and WebDriverAgent is already installed or no change.
0
0
394
Nov ’23
iOS 17: How to use `HKWorkoutBuilder` inside unit tests
Hi, with iOS 17 creation of HKWorkout is deprecated via its init functions and the recommendation is to use HKWorkoutBuilder: The problem is that I am creating this HKWorkout object inside unit tests in order to test a service that works with such objects. And HKWorkoutBuilder requires a HKHealthStore which itself requires to be authenticated to be able to create HKWorkoutActivity instances, like it would be when an app is running. But since the unit tests cannot accept the request on the HKHealthStore I am not sure if using HKWorkoutBuilder inside unit tests is possible. Any ideas on how to proceed with creating HKWorkout for unit test purposes?
0
1
452
Oct ’23
Operation not permitted - MacOS - Swift - UITests
I`m trying create simple http sever, when I do it in main target of my macOS app then it works, but when I do it in uitests target it fails (result == -1 (Operation not permitted)). Interestingly when I run it in iOS uiTests target then it works. let sockfd = socket(AF_INET, SOCK_STREAM, 0) var serverAddress = sockaddr_in(sin_len: __uint8_t(MemoryLayout<sockaddr_in>.size), sin_family: sa_family_t(AF_INET), sin_port: CFSwapInt16HostToBig(8080), sin_addr: in_addr(s_addr: inet_addr("127.0.0.1")), sin_zero: (0, 0, 0, 0, 0, 0, 0, 0)) let result = withUnsafePointer(to: &serverAddress) { $0.withMemoryRebound(to: sockaddr.self, capacity: 1) { Darwin.bind(sockfd, $0, socklen_t(MemoryLayout<sockaddr_in>.size)) } } I've got set in my main target entitlements: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>com.apple.security.app-sandbox</key> <true/> <key>com.apple.security.network.client</key> <true/> <key>com.apple.security.network.server</key> <true/> </dict> </plist> What can be reason that it fails? how can I fix it?
5
0
1.4k
Oct ’23
Sonoma temp folder permission alert breaks XCUITest
I used to be able to test my app by making it save files to a temp folder (with a file URL created in the XCUITest runner), and then comparing them to files in the test bundle, but in Sonoma, this will issue a blocking "[App name] wants to access files from other apps" security alert, which has to be dismissed manually. This breaks the test flow. It kind of makes sense, as I'm trying to save from the app to the temp folder of another app (the test runner.) Is there any way around this? Is there a temp folder that both applications can access without security alerts?
3
0
610
Oct ’23
[tvOS][XCTest] Detect which keyboard key has focus
I am writing a UI automation test for a view on Apple TV. The view has a numeric input field(TVDigitEntryViewController) which accepts 5 digits password. When the view loads the focus is on key '1' of the numeric apple input keyboard for tvOS. However when I try po pinEntryKeyboard.keys["1"].hasFocus in the debug terminal it says false. Is there a way to find out which keyboard key has focus for tvOS input keyboard? For more context I am adding some debug terminal output here: (lldb) po pinEntryKeyboard.keys ... ... ↪︎Find: Descendants matching type Key Output: { Key, 0x102e4d2f0, {{645.0, 691.5}, {70.0, 80.0}}, label: '1' Key, 0x102e4d410, {{707.0, 699.5}, {54.0, 64.0}}, label: '2' Key, 0x102e4d530, {{761.0, 699.5}, {54.0, 64.0}}, label: '3' Key, 0x102e4d650, {{815.0, 699.5}, {54.0, 64.0}}, label: '4' Key, 0x102e4d770, {{869.0, 699.5}, {54.0, 64.0}}, label: '5' Key, 0x102e4d890, {{923.0, 699.5}, {54.0, 64.0}}, label: '6' Key, 0x102e4d9b0, {{977.0, 699.5}, {54.0, 64.0}}, label: '7' Key, 0x102e4dad0, {{1031.0, 699.5}, {54.0, 64.0}}, label: '8' Key, 0x102e4dbf0, {{1085.0, 699.5}, {54.0, 64.0}}, label: '9' Key, 0x102e4dd10, {{1139.0, 699.5}, {54.0, 64.0}}, label: '0' Key, 0x102e4de30, {{1193.0, 699.5}, {74.0, 64.0}}, identifier: 'delete', label: 'delete' } (lldb) po pinEntryKeyboard.keys["1"] ... ... ↪︎Find: Elements matching predicate '"1" IN identifiers' Output: { Key, 0x1032baa80, {{645.0, 691.5}, {70.0, 80.0}}, label: '1' } (lldb) po pinEntryKeyboard.keys["1"].hasFocus t = 956.09s Find the "1" Key false
0
0
388
Oct ’23
XCUITest "MyApp" Would Like to Access Your Photo Library
I can't work out how to deal with the dialog presented when an app first tries to access a photo library: This screenshot shows the dialog still presented after the app has quit, which suggests to me that it's a system-level rather than app-level dialog; but the handler for addUIInterruptionMonitor(withDescription:handler:) isn't called (whereas it is for both the "Allow MyApp to use your location?" and "MyApp Would Like to Send You Notifications" dialogs). But nor does it appear to be an app-level dialog under XCUIApplication. How can I handle this dialog in my UI testing? Thanks, Hamish
0
0
611
Oct ’23
XCUIElement's click is broken in iOS 17 simulator
Based on official docs click is available for iPadOS apps. We utilized click action to create universal UITests for macOS and iPadOS targets. It works fine on iPadOS 16.x but, unfortunately, stops to work in iOS 17 simulators for iPad. Environment: MacOS - 13.5.2 (22G91). Xcode - Version 15.0 (15A240d). Simulator - Version 15.0 (1015.2). SimulatorKit 935.1. CoreSimulator 920.6. Steps to reproduce: Create new multiplatform app Add simple button. For example: struct ContentView: View { @State var title = "Click Me" var body: some View { VStack { Button(title) { title = "Clicked" } .background(Color.blue) .foregroundColor(.white) } .padding() } } Add UITest that clicks on button. For example: func testClick() throws { let app = XCUIApplication() app.launch() app.buttons["Click Me"].click() XCTAssertTrue(app.buttons["Clicked"].exists) } Expected result: test is passed Actual result: test is failed since buttons["Click Me"].click() doesn't click in button. Workaround: Replacement .click() to .tap() fixes the issue but it makes impossible to create universal tests with single action for both platforms
0
2
359
Oct ’23
Swift Interop Build Issue - Linker Error with Undefined Symbols when running tests
Hi All, I have a Swift Package I'm building. I need to import an existing C++ Static Library into this package. It seems to build fine, but when I attempt to run the tests I get a weird linker error. I've put a repro repo up at https://github.com/flighttactics/CppIssueRepro Here's the steps I took: I have the C++ library's .a file and the headers. I created a folder in my package for the library, drug the include folder into it, and then added the .a file at the root of the folder. I created an Umbrella header in the include folder, along with a module.modulemap. I then created a shim file (per https://github.com/apple/swift-package-manager/issues/5706) Finally, I modified my package file to build the target, and reference it as a dependency (Note I also had to go into the GeographicLib headers for DST and comment out two functions I'm not using which reference std::function since that's not supported by Swift) I find that including the library module works in the main code base, and I can build successfully. However, when I attempt to run tests against the main code base when it's calling into the C++ library I'm getting the following build error: ld: Undefined symbols: double GeographicLib::Math::AngNormalize<double>(double), referenced from: GeographicLib::GeoCoords::Reset(double, double, int) in CppIssueRepro.o GeographicLib::UTMUPS::Forward(double, double, int&, bool&, double&, double&, double&, double&, int, bool), referenced from: GeographicLib::GeoCoords::Reset(double, double, int) in CppIssueRepro.o GeographicLib::GeoCoords::MGRSRepresentation(int) const, referenced from: CppIssueRepro.CoordinateCalculator.coverttoMGRS(lat: Swift.Double, lon: Swift.Double) -> Swift.String in CppIssueRepro.o clang: error: linker command failed with exit code 1 (use -v to see invocation) Again, if my main code does not call into the C++ library I'm able to run tests and build just fine. Not sure what else I can do - it seems like I'm following everything based on the 5.9 docs but I also feel I'm maybe missing something really simple here.
2
2
672
Nov ’23
Xcode 15 vs. iOS: xcodebuild build-for-testing fails with linker error
The error is: ld: building for 'iOS', but linking in dylib (/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks/XCTest.framework/Versions/A/XCTest) built for 'zippered(macOS/Catalyst)' clang: error: linker command failed with exit code 1 (use -v to see invocation) And the command is following: xcodebuild -project "my_project.xcodeproj" -scheme "UITests" -destination generic/platform=iOS build-for-testing It's caused by updating Xcode to 15, it was working well under Xcode 14. The same UITests scheme is built and executed successfully from Xcode. And xcodebuild still builds successfully the app bundle: xcodebuild -project "my_project.xcodeproj" -scheme "App" -destination generic/platform=iOS archive -archivePath "build/app.xcarchive" I haven't found this problem so far in forums, so looking for help/suggestions.
0
0
716
Oct ’23
Running tests that doesn't use SwiftData with iOS 16
Hi! I'm using SwiftData for iOS 17 widgets in a project that with a minimum target of iOS 15. Our CI is having issues running the iOS 17 simulator (it takes more than 10 minutes to start up) so I'm trying to run the tests using iOS 16, obviously skipping all the SwiftData usages. The app runs just fine in iOS 16 and it has all the @available(iOS 17, iOSApplicationExtension 17, *) annotations for the @Model classes. But the tests crashes when trying to start the app because it tries to get the metadata of my model classes, which crashes with a nil as the SwiftData library doesn't exist, because the test framework initializes all classes before running. Is there a workaround for this? Running the tests in an app-supported iOS version should work as we should be able to test different behaviors, right? Thanks in advance! Emanuel (I'm attaching the stack trace of the crash) Thread 1 Queue : com.apple.main-thread (serial) #0 0x0000000000000000 in 0x00000000 () #1 0x00000001029d13ee in type metadata completion function for WidgetModel.Set () #2 0x00000001248cd660 in swift::MetadataCacheEntryBase<(anonymous namespace)::SingletonMetadataCacheEntry, int>::doInitialization(swift::MetadataWaitQueue::Worker&, swift::MetadataRequest) () #3 0x00000001248bc327 in swift_getSingletonMetadata () #4 0x00000001029ccdd5 in type metadata accessor for WidgetModel.Set () #5 0x00000001029d132d in ObjC metadata update function for WidgetModel.Set () #6 0x0000000115b25b18 in realizeClassMaybeSwiftMaybeRelock(objc_class*, locker_mixin<lockdebug::lock_mixin<objc_lock_base_t>>&, bool) () #7 0x0000000115b2c907 in realizeAllClasses() () #8 0x0000000115b2f05f in objc_copyClassList () #9 0x000000015aea6dcd in +[XCTestCase(RuntimeUtilities) _allSubclasses] () #10 0x000000015aea6f17 in +[XCTestCase(RuntimeUtilities) allSubclasses] () #11 0x000000015ae6de91 in +[XCTestSuite suitesForBundlesIncludingEmptySuites:] () #12 0x000000015ae718c2 in -[XCTestSuite _initWithTestConfiguration:] () #13 0x000000015ae722b0 in +[XCTestSuite testSuiteForTestConfiguration:] () #14 0x000000015ae33943 in __53-[XCTTestRunSession initWithTestConfiguration:error:]_block_invoke_2 () #15 0x000000015ae6d0d0 in +[XCTContext _runInChildOfContext:forTestCase:markAsReportingBase:block:] () #16 0x000000015ae6cfb0 in +[XCTContext runInContextForTestCase:markAsReportingBase:block:] () #17 0x000000015ae338df in __53-[XCTTestRunSession initWithTestConfiguration:error:]_block_invoke () #18 0x000000015ae33525 in +[XCTTestRunSession performTestSuiteConstructionInBlock:] () #19 0x000000015ae33757 in -[XCTTestRunSession initWithTestConfiguration:error:] () #20 0x000000015ae34e4c in -[XCTTestRunSessionProvider executionExtensionWithTestConfiguration:reportingSession:completion:] () #21 0x000000015ae3b11c in __128+[XCTExtensionProvider(XCTExecutionExtension_Internal) allExecutionExtensionsWithTestConfiguration:reportingSession:completion:]_block_invoke () #22 0x000000015ae83a4b in __69-[NSArray(XCTestAdditions) xct_arrayByApplyingAsyncBlock:completion:]_block_invoke () #23 0x000000015ae83bd9 in __XCTAsyncEnumerate_block_invoke.39 () #24 0x000000015ae837cb in -[NSArray(XCTestAdditions) xct_arrayByApplyingAsyncBlock:completion:] () #25 0x000000015ae3af98 in +[XCTExtensionProvider allExecutionExtensionsWithTestConfiguration:reportingSession:completion:] () #26 0x000000015ae68af6 in -[XCTestDriver _runTests] () #27 0x000000015ae3290e in _XCTestMain () #28 0x000000010ffc8759 in __RunTests_block_invoke_2 () #29 0x000000012038e986 in __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ () #30 0x000000012038e148 in __CFRunLoopDoBlocks () #31 0x00000001203889cf in __CFRunLoopRun () #32 0x0000000120388264 in CFRunLoopRunSpecific () #33 0x0000000125a4a24e in GSEventRunModal () #34 0x00000001347a47bf in -[UIApplication _run] () #35 0x00000001347a95de in UIApplicationMain () #36 0x0000000102a3ecef in main at /opt/projects/quizlet-ios/Quizlet-iPhone/AppDelegate.swift:24 #37 0x000000010fe02384 in start_sim () #38 0x00000002075243a6 in start ()
2
0
504
Oct ’23
Xcode build failed due to Tests did not run
I am having trouble building WebDriverAgent on MacOS Sonoma with Xcode 15 and iOS SDK 17.0. The build always fails with the following error.: Parameter of overriding method should be annotated with __attribute__((noescape)) Showing Recent Messages /Users/.nvm/versions/node/v14.18.0/lib/node_modules/appium/node_modules/appium-webdriveragent/PrivateHeaders/XCTest/XCTestCase.h:54:42: Parameter of overriding method should be annotated with __attribute__((noescape))
4
0
2.8k
Oct ’23
Xcode doesn't generate memgraph file after UI Test finishes execution
Hi, I'm following this video Detect and diagnose memory from WWDC 2021: https://developer.apple.com/videos/play/wwdc2021/10180/ In the video, after the UI test finishes execution, a memgraph is generated. I created a new project and added some UI tests, but after all the tests finished execution, Xcode didn't generate any memgraph. I'm using Xcode 14.2. How can I make Xcode generate a memgraph after UI test finishes execution? Thanks for your help!
1
0
420
Sep ’23
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