Testing: test code shared by several test targets

I would like to know if it is possible to have shared code (that imports `Testing`) for testing, that can be used by test target that do real tests? I cannot define a test target for it, because test targets cannot import other test targets (it seems to work with SPM outside Xcode, but shouldn’t this work the same by now?), and defining a library for that has analog difficulties.

BTW is it possible to run a specific test target via the command line (it is possible from within Xcode)?

You should be able to use basic targets and conditional compilation, you can take a look at this part of snapshot-testing for example. And if you expect Testing to always be available in your environment, you can even merge checks into one top-level check:

// File in a basic target
// You can name it MyPackageTestSupport for example

#if canImport(Testing)
import Testing

func runSharedTests() async throws {
  #expect(true != false)
}
#endif

To run specific test target from the command line try

swift test --filter <#your-target#>

swift test --help output related to --filter option:

--filter <filter> Run test cases that match a regular expression, Format: '<test-target>.<test-case>' or '<test-target>.<test-case>/<test>'.
1 Like