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

Posts under XCTest tag

146 Posts
Sort by:

Post

Replies

Boosts

Views

Activity

Code coverage problem with symbolic linked source files
Let's say I have a local package Foo, Bar and Baz. Foo has Bar and Baz as dependency and has test target,FooTests. Below is the Package.swift manifest that describes package Foo. import PackageDescription let packages: [Package.Dependency] = [ .package(path: "../Bar"), .package(path: "../Baz"), ] let products: [Target.Dependency] = [ .product(name: "Bar", package: "Bar"), .product(name: "Baz", package: "Baz"), ] let package = Package( name: "Foo", platforms: [ .iOS(.v16), ], products: [ .library( name: "Foo", targets: ["Foo"] ), ], dependencies: packages, targets: [ .target( name: "Foo", dependencies: products, path: "Sources" ), .testTarget( name: "FooTests", dependencies: [.target(name: "Foo")], path: "Tests", swiftSettings: swiftSettings ), ] ) Bar and Baz is also local packages. Due to the limitations of the project, Baz has sources with symbolic links instead of exact directories, and these sources are what FooTests will test. If I run FooTests, test succeed with proper logs indicating that all test methods are run correctly, but test coverage says that Baz's coverage is 0% and there is no executable lines unlike the Bar which is not what be tested. Is there any way to fix this problem?
0
0
59
5d
Mergeable Library in XCTest with iOS on Mac Target: 3588 Error
I have an XCTest unit test that's running in an iOS host but with the My Mac (Designed For iPad) Target. Enabling mergeable libraries causes two errors: Symbol not found: _$s10Foundation3URLV9RVAPIPackE8isGCSURLSbvg And "loading failed because of an error Error Domain=NSCocoaErrorDomain Code=3588" for the Test bundle because one of the frameworks that's supposed to be merged couldn't be loaded. Disabling mergeable libraries fixes it. It also works with mergeable libraries enabled but with a regular simulator target. So not sure if I'm missing something or if I need to disable mergeable libraries now:/
2
0
99
1w
Error in gathering code coverage in Xcode16
Fails to gather code coverage and throws this error Showing All Messages Failed to merge raw profiles in directory /Users//Library/Developer/Xcode/DerivedData/Receiver-ekqrbpsaciuxmlfslviajhoecyat/Build/ProfileData/0B0C6B69-FD46-4801-B106-56B7FCD44370 to destination /Users//Library/Developer/Xcode/DerivedData/Receiver-ekqrbpsaciuxmlfslviajhoecyat/Build/ProfileData/0B0C6B69-FD46-4801-B106-56B7FCD44370/Coverage.profdata: Aggregation tool '/Users/shwethamugeraya/Downloads/Xcode 2.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/llvm-profdata' failed with exit code 1: warning: /Users/shwethamugeraya/Library/Developer/Xcode/DerivedData/Receiver-ekqrbpsaciuxmlfslviajhoecyat/Build/ProfileData/0B0C6B69-FD46-4801-B106-56B7FCD44370/2F4EFBF7-1CCF-4E9E-8FD6-482EEDB98B6C-34646.profraw: raw profile version mismatch: Profile uses raw profile format version = 4; expected version = 8 PLEASE update this tool to version in the raw profile, or regenerate raw profile with expected version. error: no profile can be merged
0
0
116
2w
[bundle infoDictionary] doesn't contain some keys in info.plist file.
I try to do unit test for an ES system extension's code using Unit Testing Bundle.  the code tries to get NSEndpointSecurityMachServiceName in info.plist by [bundle objectForInfoDictionaryKey:] but failed.  I have added NSEndpointSecurityMachService to info.plist of Test Bundle, and I did check info.plist file in bundle, NSEndpointSecurityMachServiceName was listed there.
1
0
152
2w
Error while reading test baseline file
When running Unit tests on my project, I get an error: Error while reading test baseline file: "The file “05ACE933-24F8-449B-9949-77312B3070BC.plist” couldn’t be opened because there is no such file." at path: "/private/var/folders/qy/g9s46vp88xggk_001s62kxb00000gn/X/54A3BAF3-89F8-52E2-8575-47176446C08E/d/Wrapper/MyApp.app/PlugIns/MyAppTests.xctest/xcbaselines/05ACE933-24F8-449B-9949-77312B3070BC.plist" This happened after a rebase / merge, so I must have accidentally deleted a file from the repository. This only seems to happen on the 'My Mac (Designed for iPad) device. When I run the unit tests on my iPhone, all seems to be well. How can I either remove the reference to the missing file, or reset the baselines in the project? Kind regards, Wouter Wessels
0
0
121
2w
Can't seem to get Unit Test target working when I create. Multiplatform project
In Xcode Version 16.1 Create a new Project, choose Multiplatform, App For testing system, choose XCTest and UI Tests In the project, open the newly generated template Unit test file Click either of the diamonds in the left margin (either to run the example test or the entire file) Expected Result: Code compiles and then Runs the test/tests Actual Result: Code compiles, but then never completes testing (the top status bar is stuck saying "Testing..." forever.) Am I missing something?
0
0
152
2w
`xcodebuild test` stuck because of app data container protection
Hi, I have recently noticed a very-very annoying thing in my CI pipeline, that I managed to reproduce locally. Using macOS Sonoma 14.7.1 and Xcode 15.4, running a test target from Terminal, the first run produces this output: $ xcodebuild test -workspace [redacted].xcworkspace -scheme [redacted] -quiet --- xcodebuild: WARNING: Using the first of multiple matching destinations: { platform:macOS, arch:arm64, id:[redacted], name:My Mac } { platform:macOS, arch:x86_64, id:[redacted], name:My Mac } { platform:macOS, name:Any Mac } Testing started Test suite '[redacted]' started on 'My Mac - [redacted] (75944)' Test case '[redacted].[redacted]()' passed on 'My Mac - [redacted] (75944)' (0.001 seconds) Test case '[redacted].[redacted]()' passed on 'My Mac - [redacted] (75944)' (0.001 seconds) 2024-11-29 19:31:02.311 xcodebuild[75908:47784216] [MT] IDETestOperationsObserverDebug: 1.380 elapsed -- Testing started completed. 2024-11-29 19:31:02.311 xcodebuild[75908:47784216] [MT] IDETestOperationsObserverDebug: 0.000 sec, +0.000 sec -- start 2024-11-29 19:31:02.311 xcodebuild[75908:47784216] [MT] IDETestOperationsObserverDebug: 1.380 sec, +1.380 sec -- end so the test actually completes, but the process does not end, because I get a popup that “Terminal.app” would like to access data from other apps. Removing the "App Sandbox" entitlement immediately alleviates this problem, but it would be very silly to do so because of unit tests. This ultimately means that whenever we use a new device or include a new project or test target, this popup comes up. Very undesirable for CI servers (especially if the servers are ephemeral virtual machines created for the current job). Is there a permanent solution that disables this popup for test runs?
0
0
215
3w
Struggling to add a test target to an existing project in Xcode
Sorry if this question is too vague, however I've tried this multiple times and see the same result. I'm pretty sure I'm doing something wrong, but don't know what. I have a Multiplatform (iOS and macOS) project that builds, and runs I add a new target of type Unit Test Bundle I click the diamond in the margin beside the XCTestCase declaration at the top of the new Test file The target project builds, then Xcode says 'Testing...' and it stays like this forever. I've also tried creating a new scheme that targets the target created in the above steps. attempting to run my tests behaves the same. The top status bar will get stuck saying 'Testing...' and never get anywhere. I'm pretty sure this is something basic. thanks, in advance for any guidance. Mike
2
0
175
3w
How to run Catalyst xctest bundle with xcrun
I'm trying to run an xctest bundle, built for catalyst, with xcrun. i.e. xcrun xctest path_to_my_test.xctest It fails, complaining that: The bundle "MyBundle" couldn't be loaded because it is damaged or missing necessary resources. Try reinstalling the bundle.....but incompatible platform (have 'MacCatalyst', need 'macOS') So it seems like because I'm not executing it in a sim it wants the bundle to be a macOS bundle. But I would have thought it would be possible to run a Mac Catalyst target directly on a macOS host the same as a native macOS test target. Is this not possible?
0
0
118
3w
No screenshot files in XCResult files using Xcode 16.1
Hello, I am using Xcode 16.1 (16B40) on MacOS Sequoia 15.1.0 using a Macbook pro M1 Max I am developing an app for iOS 17 and 18 using SwiftUI I created UITests to take the screenshots for the appStore on the simulator The tests run well and all of them are succeded The problem appears when I try to get the screenshot files from the xcresult files after the test. There is not any screenshot inside it. I found a data folder and a Info.plist file. In the data folder there are a lot of files with this pattern data.03zD4C6IGFFthK14NwA8mNvcwFHT16g6Tl40Tl1YmBC1bNh6d0YIcnWKyUaQPDXoa8fYo6C3Xcv8xvMtE3_NEXA== and other files with this pattern refs.03zD4C6IGFFthK14NwA8mNvcwFHT16g6Tl40Tl1YmBC1bNh6d0YIcnWKyUaQPDXoa8fYo6C3Xcv8xvMtE3_NEXA== Ok, I tryed to use fastlane to automatize the screenshots but the problem is still present. The xcresult files have not any png file. I had no problems doing this action (getting screenshots from a xcresult file) in previous versions of MacOS and Xcode in my current machine. I just updated my machine to MacOS Sequoia 15.1.1 and the problem is still present Honestly I don't know how to fix this situation. With Xcode 15 I had not any problem with that but I am not sure if Xcode 16.0 was runing without problems because I didn't need to use this functionality in those months Here is my code for a UITest: import XCTest final class ScreenshotsUITests: XCTestCase { let app = XCUIApplication() let device = "iPhone16" override func setUpWithError() throws { continueAfterFailure = true } override func tearDownWithError() throws {} @MainActor func testEnglishScreens() throws { let lang = "en" app.launchArguments.append("UITestMode") app.launchArguments += ["-AppleLanguages", "(en)"] app.launchArguments += ["-AppleLocale", "en_US"] app.launch() executeTestsForMenus(lang: lang, backLabel: "Back") executeTestForMatch(lang: lang) } @MainActor func testSpanishScreens() throws { let lang = "es" app.launchArguments.append("UITestMode") app.launchArguments += ["-AppleLanguages", "(es)"] app.launchArguments += ["-AppleLocale", "es_ES"] app.launch() executeTestsForMenus(lang: lang, backLabel: "Atrás") executeTestForMatch(lang: lang) } private func executeTestForMatch(lang: String) { let startButton = app.buttons["start-button"] startButton.tap() let key4 = app.buttons["key-4"] XCTAssertTrue(key4.waitForExistence(timeout: 30), "Key 4 in match screen is not found") key4.tap() let key2 = app.buttons["key-2"] XCTAssertTrue(key2.exists, "Key 2 in match screen is not found") key2.tap() makeScreenShot("playing", lang: lang) let closeButton = app.buttons["close-button"] XCTAssertTrue(closeButton.exists, "Close button in match screen is not found") closeButton.tap() } private func executeTestsForMenus(lang: String, backLabel: String) { let mainHeader = app.staticTexts["Math match"] XCTAssertTrue(mainHeader.exists, "Header in main screen is not found") makeScreenShot("mainMenu", lang: lang) let settingsButton = app.buttons["settings-button"] XCTAssertTrue(settingsButton.exists, "Settings button in main screen is not found") settingsButton.tap() makeScreenShot("Settings", lang: lang) let backButton = app.buttons[backLabel] XCTAssertTrue(backButton.exists, "Back button in match screen is not found") backButton.tap() let helpButton = app.buttons["help-button"] XCTAssertTrue(helpButton.exists, "Help button in main screen is not found") helpButton.tap() makeScreenShot("Help", lang: lang) backButton.tap() let scoreButton = app.buttons["score-button"] XCTAssertTrue(scoreButton.exists, "Scores button in main screen is not found") scoreButton.tap() makeScreenShot("Scores", lang: lang) backButton.tap() let playButton = app.buttons["play-button"] XCTAssertTrue(playButton.exists, "Play button in main screen is not found") playButton.tap() makeScreenShot("matchBuilder", lang: lang) let startButton = app.buttons["start-button"] XCTAssertTrue(startButton.exists, "Start button in match builder screen is not found") } private func makeScreenShot(_ name: String, lang: String) { takeScreenshot(app, named: "\(lang)-\(name)-\(device)") } } import XCTest extension XCTestCase { func takeScreenshot(_ app: XCUIApplication, named name: String, fullScreen: Bool = false) { let screenshot: XCUIScreenshot if fullScreen { screenshot = app.windows.firstMatch.screenshot() } else { screenshot = XCUIScreen.main.screenshot() } let screenshotAttachment = XCTAttachment( uniformTypeIdentifier: "public.png", name: "screenshot-\(name).png", payload: screenshot.pngRepresentation, userInfo: nil) screenshotAttachment.lifetime = .keepAlways add(screenshotAttachment) } } and here is the content of my testplan file: { "configurations" : [ { "id" : "35BC7C0B-9A5A-4027-9F30-36958C4C1AAF", "name" : "Test Scheme Action", "options" : { "preferredScreenCaptureFormat" : "screenshot", "testExecutionOrdering" : "random", "uiTestingScreenshotsLifetime" : "keepAlways", "userAttachmentLifetime" : "keepAlways" } } ], "defaultOptions" : { "targetForVariableExpansion" : { "containerPath" : "container:myAppProject.xcodeproj", "identifier" : "B27D1B022CA00314001A259B", "name" : "MyAppProject" } }, "testTargets" : [ { "parallelizable" : true, "target" : { "containerPath" : "container:MyAppProject.xcodeproj", "identifier" : "B27D1B122CA00315001A259B", "name" : "MyAppProjectTests" } }, { "parallelizable" : true, "target" : { "containerPath" : "container:MyAppProject.xcodeproj", "identifier" : "B27D1B1C2CA00315001A259B", "name" : "MyAppProjectUITests" } } ], "version" : 1 } I made tests with old projects in my machine and those projects have the same problem with screenshot files in the xcresult bundles I don't know if the problem is in my machine, my Xcode, MacOS or other ting. I don't know how to fix this problem Please, can anyone help me? Thanks in advance
1
0
186
Nov ’24
Unable to inspect SwiftUI view embedded inside UITableView
We have been trying to migrate screens that were developed using UITool Kit to SwiftUI. In the process we have some screens that have SwiftUI embedded inside the UITool kit view. Our developers have defined accessibility ids for all elements in these views and these are inspectable using the native iOS xcode inspector. However when i try inspecting it with the appium inspector i get an empty list with no elements in the hierarchy tree. Attaching a screenshot of the element when inspecting through the native xcode accessibility inspector, Attaching a screenshot of the same screen when inspected through the appium inspector, Also tried printing the XCTest UI dump using appium method, `driver().executeScript("mobile:source", Map.ofEntries(Map.entry("format","description"))) The UI tree i get is the same that i get when inspecting through the appium inspector. Requesting support from the Apple team based on this ticket, [https://github.com/appium/appium/issues/20759)
0
0
186
Nov ’24
Not seeing signposts when profiling a unit test (Xcode 16.1)
While I was recently profiling some code from a Swift library, I noticed that XCTest added in signposts for the measurement tests, which I found really helpful to "home in" on the code I wanted to profile digging around in the stack trace. I tried to add my own signposts to provide just a bit of my own markers in there, but while it compiles and profiles equivalently, the signposts just aren't showing up. This is with Xcode 16.1, macOS Sequoia (15.1) and a swift library, using XCTest and profiling within one of the unit tests. Is there something in this sequence that doesn't allow the library to set up signposts and have instruments collect them? The flow I'm using: import os let subsystem = "MyLibrary" class MyClass { let logger: Logger = .init(subsystem: subsystem, category: "fastloop") let signposter: OSSignposter init() { signposter = OSSignposter(logger: logger) } func goFast() { let signpostId = signposter.makeSignpostID() let state = signposter.beginInterval("tick", id: signpostId) // ... do a bunch of work here - all synchronous signposter.endInterval("tick", state) } } Is there something I'm doing incorrectly in using this API, or not enabling to allow those signposts to be collected by the profiler? I do see the signposts that XCTests injects into the system, just not any of the ones I'm creating.
1
0
238
Nov ’24
XCUITest Screen Recording
In WWDC23: Fix failures faster with Xcode test reports, the presenter showed off an Xcode feature called Automation Explorer, that allows playback of a screen recording corresponding to an XCUITest. For the life of me, I have not been able to find out how to enable/use this feature in Xcode 16. I am using Test Plans and have set the UI Testing configuration settings for Automatic Screen Capture to On, and keep all and Preferred Capture Format to Video, but am not seeing any screen recordings being produced after running the tests. Am I doing something wrong?
1
0
247
Nov ’24
Run identical UI/Unit tests on different build targets
I have different versions of my iOS App (written in SwiftUI). The app on the store, the App Clip, and one or two next version apps not yet released (e.g. A/B comparison). All good. But now I've started creating UI and Unit tests and I'm confused about how to get this working. Each build target has its own scheme. And in that scheme I have a Test plan for that target. E.g. The App Clip scheme has an App Clip test plan. Since all the app variants are very similar, I only have one set of unit tests and one set of UI tests so each test plan includes the same unit test target and the UI test target. Problem: When I selected a scheme (e.g. for the App Clip) and ran the tests, it turned out that all the tests ran for another build target, not the target of the scheme. I think this might be because within the definition of the test target there's a field specifying the host application. I.e. the build target. Question: How can I set up my project so that the test plan uses the relevant target build? Or do I have to duplicate all the test targets (one for each target)? Or do I have to manually change each test target before running it for a particular build target?
1
0
196
Nov ’24
Crash calling UIHostingController from a Swift Package
Hello team, We recently found a EXC_BAD_ACCESS crash when using UIHostingControllers on a SPM local Package in our application. This is happening from time to time when we run the app on simulators using Debug configurations. Also, this issue is consistent when we run application tests, there is something weird that we found after making a research... If we disable app test target and only keep packages tests, the crash is not happening, but as soon as we re-enable the app test target from the test suite the crash returns. This is the full error message: Thread 1: EXC_BAD_ACCESS (code=1, address=0xbad4017) A bad access to memory terminated the process. Is this s known issue? We've been performing explorations for some days and couldn't find any real solution for this. A basic call on UIHostingController inside of a SPM local Package is enough to make the app crash, nothing custom being done: UIHostingController(rootView: MyView())
2
0
251
Nov ’24
macOS Sequoia doesn't respect Full Disk Access for UITests-Runner
We develop and test App for macOS. We start to see system alert - "UlTests-Runner" would like to access data from other apps on each UITest run. Our test suite does cleanup of files generated by App so we need access outside of UITests-Runner sandbox. We enabled Full Disk Access for UITests-Runner at Settings -> Privacy & Security -> Full Disk Access but unfortunately still see this alert. Is there any way to permanently remove/hide this alert or remove sandbox for 'UITests-Runner' since we want to run tests on CI and having this alert is not an option? Note: everything works fine on previous versions of macOS. Environment: macOS - 15.1 (24B83) Xcode - Version 16.1 (16B40)
0
1
219
Nov ’24