swiftxctestswift-playground

Cannot import XCTest in Swift Playgrounds 4.3.1


I'm working in Swift Playgrounds for Mac, v4.3.1.

There are a number of tutorials on the web (example) that show using XCTest in XCode Playgrounds. But when I try to import it, I get No such module 'XCTest'. Is there some step I'm missing? Was support discontinued?

Swift Playgrounds Screenshot


Solution

  • As of now, you cannot use XCTest in Swift Playgrounds

    There are two applications that create Swift "Playgrounds", and the one mentioned in the article isn't the same one as what you are using.

    • Xcode can create "playgrounds" by clicking File -> New -> Playground...

      These playgrounds have the file extension .playground

    • The "Swift Playgrounds" app (can be installed from the iOS App Store or the Mac App Store) can also create "playgrounds"

      These playgrounds have the file extension .playgroundbook

    The playgrounds created by these two apps are very different. There are some things you cannot do in playgrounds created by the "Swift Playgrounds" app, but you can in playgrounds created by Xcode. XCTest is one of them.

    As the article you linked says:

    Playgrounds was released in September, 2016. It’s a powerful environment integrated in Xcode to code in real-time in Swift.

    Obviously, it is referring to playgrounds created using Xcode. The Swift Playgrounds app, which is what you are using, is not released on the Mac App Store until 2020.

    That said, Xcode playgrounds have existed before 2016. Perhaps the article is referring to when XCTest is available to Xcode Playgrounds.