For most cases, swift test is sufficient to build and run tests. The
instructions below are for when you need to attach LLDB to debug test code
directly. Doing this differs significantly between Darwin and non-Darwin
platforms.
On Darwin, SwiftPM packages tests into xctest bundles. These bundles are not
directly executable and must be run using the swiftpm-testing-helper tool.
The helper is located within the toolchain:
# Xcode toolchain
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/libexec/swift/pm/swiftpm-testing-helper
# Custom toolchain
/path/to/toolchain/usr/libexec/swift/pm/swiftpm-testing-helperRequired arguments:
--test-bundle-path: Path to the test bundle executable (e.g.,MyTests.xctest/Contents/MacOS/MyTests)--testing-library swift-testing: Specifies that Swift Testing should be used.
Additional swift test arguments (such as --filter) can also be passed. Run
swift test --help for the full list of available options.
lldb -- /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/libexec/swift/pm/swiftpm-testing-helper \
--test-bundle-path MyTests.xctest/Contents/MacOS/MyTests \
--testing-library swift-testingThis launches LLDB with the testing helper configured to run your test bundle. You can then set breakpoints and debug your tests normally.
Swift Testing ships in two forms: Testing.framework and libTesting.dylib. The compiler you use to build your test bundle determines which library it links against. At runtime, the dynamic linker must find the matching library. Problems occur when the build-time and runtime environments don't match—for example, if you build with a custom toolchain but run with system libraries, or vice versa.
If you encounter missing symbols or unexpected behavior, export the environment
variables DYLD_LIBRARY_PATH or DYLD_FRAMEWORK_PATH as appropriate to point
to the correct location.
On non-Darwin platforms, SwiftPM builds test targets as standalone executables rather than bundles. You can debug them directly:
lldb -- .build/debug/MyPackagePackageTests.xctestWhen using Swift Testing on non-Darwin, swift test invokes the test binary
twice:
- First invocation: Runs XCTest-based tests via
XCTestMain() - Second invocation: Runs Swift Testing tests by passing
--testing-library swift-testing
This is an internal SwiftPM implementation detail. When debugging, you likely want to skip the XCTest invocation and run Swift Testing directly.
To debug only Swift Testing tests, pass --testing-library explicitly:
lldb -- .build/debug/MyPackagePackageTests.xctest --testing-library swift-testingArguments such as --filter are passed through to Swift Testing directly. Run
swift test --help for the full list of available options.