|
| 1 | +# Debugging Swift Testing on the Commandline using LLDB |
| 2 | + |
| 3 | +<!-- |
| 4 | +This source file is part of the Swift.org open source project |
| 5 | +
|
| 6 | +Copyright (c) 2026 Apple Inc. and the Swift project authors |
| 7 | +Licensed under Apache License v2.0 with Runtime Library Exception |
| 8 | +
|
| 9 | +See https://swift.org/LICENSE.txt for license information |
| 10 | +See https://swift.org/CONTRIBUTORS.txt for Swift project authors |
| 11 | +--> |
| 12 | + |
| 13 | +For most cases, `swift test` is sufficient to build and run tests. The |
| 14 | +instructions below are for when you need to attach LLDB to debug test code |
| 15 | +directly. Doing this differs significantly between Darwin and non-Darwin |
| 16 | +platforms. |
| 17 | + |
| 18 | +## Darwin Platforms |
| 19 | + |
| 20 | +On Darwin, SwiftPM packages tests into xctest bundles. These bundles are not |
| 21 | +directly executable and must be run using the `swiftpm-testing-helper` tool. |
| 22 | + |
| 23 | +### swiftpm-testing-helper |
| 24 | + |
| 25 | +The helper is located within the toolchain: |
| 26 | + |
| 27 | +```sh |
| 28 | +# Xcode toolchain |
| 29 | +/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/libexec/swift/pm/swiftpm-testing-helper |
| 30 | + |
| 31 | +# Custom toolchain |
| 32 | +/path/to/toolchain/usr/libexec/swift/pm/swiftpm-testing-helper |
| 33 | +``` |
| 34 | + |
| 35 | +Required arguments: |
| 36 | + |
| 37 | +1. `--test-bundle-path`: Path to the test bundle executable (e.g., |
| 38 | + `MyTests.xctest/Contents/MacOS/MyTests`) |
| 39 | +2. `--testing-library swift-testing`: Specifies that Swift Testing should be |
| 40 | + used. |
| 41 | + |
| 42 | +Additional `swift test` arguments (such as `--filter`) can also be passed. Run |
| 43 | +`swift test --help` for the full list of available options. |
| 44 | + |
| 45 | +### Example: Debugging with LLDB |
| 46 | + |
| 47 | +```sh |
| 48 | +lldb -- /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/libexec/swift/pm/swiftpm-testing-helper \ |
| 49 | + --test-bundle-path MyTests.xctest/Contents/MacOS/MyTests \ |
| 50 | + --testing-library swift-testing |
| 51 | +``` |
| 52 | + |
| 53 | +This launches LLDB with the testing helper configured to run your test bundle. |
| 54 | +You can then set breakpoints and debug your tests normally. |
| 55 | + |
| 56 | +### How Swift Testing Is Linked |
| 57 | + |
| 58 | +Swift Testing ships in two forms: Testing.framework and libTesting.dylib. The |
| 59 | +compiler you use to build your test bundle determines which library it links |
| 60 | +against. At runtime, the dynamic linker must find the matching library. Problems |
| 61 | +occur when the build-time and runtime environments don't match—for example, if |
| 62 | +you build with a custom toolchain but run with system libraries, or vice versa. |
| 63 | + |
| 64 | +If you encounter missing symbols or unexpected behavior, export the environment |
| 65 | +variables `DYLD_LIBRARY_PATH` or `DYLD_FRAMEWORK_PATH` as appropriate to point |
| 66 | +to the correct location. |
| 67 | + |
| 68 | +## Non-Darwin Platforms |
| 69 | + |
| 70 | +On non-Darwin platforms, SwiftPM builds test targets as standalone executables |
| 71 | +rather than bundles. You can debug them directly: |
| 72 | + |
| 73 | +```sh |
| 74 | +lldb -- .build/debug/MyPackagePackageTests.xctest |
| 75 | +``` |
| 76 | + |
| 77 | +### Double-Invocation Gotcha |
| 78 | + |
| 79 | +When using Swift Testing on non-Darwin, `swift test` invokes the test binary |
| 80 | +twice: |
| 81 | + |
| 82 | +1. **First invocation**: Runs XCTest-based tests via `XCTestMain()` |
| 83 | +2. **Second invocation**: Runs Swift Testing tests by passing |
| 84 | + `--testing-library swift-testing` |
| 85 | + |
| 86 | +This is an internal SwiftPM implementation detail. When debugging, you likely |
| 87 | +want to skip the XCTest invocation and run Swift Testing directly. |
| 88 | + |
| 89 | +### Example: Debugging with LLDB |
| 90 | + |
| 91 | +To debug only Swift Testing tests, pass `--testing-library` explicitly: |
| 92 | + |
| 93 | +```sh |
| 94 | +lldb -- .build/debug/MyPackagePackageTests.xctest --testing-library swift-testing |
| 95 | +``` |
| 96 | + |
| 97 | +### Passing Arguments |
| 98 | + |
| 99 | +Arguments such as `--filter` are passed through to Swift Testing directly. Run |
| 100 | +`swift test --help` for the full list of available options. |
0 commit comments