Setting up XCTests for SwiftData model debugging

I am trying to create a document-based (macOS) app using SwiftUI and SwiftData. I'm trying to set up XCTests to validate my model behaviors.

Clearly, I need to set up a container in my test class for my model objects. Failing to do so shows that SwiftData is trying: my app launches into the New Document dialog; if I dismiss it, the app fails with a "failed to find an active container" for my model object.

Any pointer on setting up a container and context within XCTest is welcome. TIA

Replies

After re-watching some of the WWDC video, here's what I've got so far:

final class ModelTests: XCTestCase {
    
    var context: ModelContext?

    override func setUpWithError() throws {
        // Put setup code here. This method is called before the invocation of each test method in the class.
        let schema = Schema([MyModelClass.self])
        let config = ModelConfiguration(schema: schema, inMemory: true)
        guard let container: ModelContainer = try? ModelContainer(for: schema, configurations: [config]) else {
            XCTFail("Failed to set up model container")
            return
        }
        context = ModelContext(container)
    }
…

I feel like this gets me to the point where I can test the functionality of my model objects, and probably test persistence by re-fetching objects from the store.

Comments and questions welcome.