How does `swift-testing` test discovery work?

Random question: How was test discovery implemented for swift-testing? Does the swift-testing package expose a library for IDEs to discover test functions?

:rotating_light: The following are implementation details of Swift Testing as written today. They are subject to change. If you are thinking of implementing some tooling around this functionality, I strongly recommend not doing so as your code will break in the future.

Test discovery is implemented by searching Swift's type metadata section for types that conform to a particular internal protocol. These types are generated by the @Test and @Suite macros at compile time. To improve performance, these types are given names that are unlikely to occur "naturally".

There is no public interface available for enumerating tests at runtime other than swift test list. It would be possible to expose something here, however there are a number of nuances and potential performance pitfalls we would need to account for before doing so. If you need API for this, please send us a GitHub issue. Thanks!

5 Likes