🚀 How to Test Core Data Code in VSCode/Cursor

TL;DR: To test Core Data in VSCode or Cursor, ensure you’re using the Xcode Swift toolchain by correctly setting DEVELOPER_DIR in your project config. Since VSCode doesn’t auto-convert .xcdatamodeld files, use an xcrun momc script to manually convert them to .momd. This enables Core Data tests to run correctly without relying on Xcode’s build system.

Background

With the development of the Swift for VSCode extension, developers are increasingly inclined to work on Swift projects in VSCode or Cursor. However, when dealing with Apple-specific frameworks such as Core Data, the testing process often encounters obstacles. This article presents the key configuration methods required to successfully test Core Data in these environments.

Using the Correct Swift Toolchain

On macOS, multiple versions of the Swift toolchain may be installed. Some toolchains (such as the open-source version installed by swiftly) do not support closed-source frameworks like Core Data. It is crucial to ensure that you are using the toolchain provided by Xcode.

Finding and Setting the Toolchain Path

Use where swift to locate all installed paths:

Bash
where swift

/Users/yangxu/.swiftly/bin/swift
/usr/bin/swift

Run the following command to confirm the Xcode toolchain information:

Bash
/usr/bin/swift -v

# Sample output:
Apple Swift version 6.1 (swiftlang-6.1.0.110.21 clang-1700.0.13.3)
Target: arm64-apple-macosx15.0
/Applications/Xcode-16.3.0.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-help intro

Welcome to Swift!

Subcommands:

  swift build      Build Swift packages
  swift package    Create and work on packages
  swift run        Run a program from a package
  swift test       Run package tests
  swift repl       Experiment with Swift code interactively

  Use `swift --version` for Swift version information.

  Use `swift --help` for descriptions of available options and flags.

  Use `swift help <subcommand>` for more information about a subcommand.

Configure the Xcode toolchain path in .vscode/settings.json:

JSON
{
  "swift.path": "/usr/bin/",
  "swift.swiftEnvironmentVariables": {
    "DEVELOPER_DIR": "/Applications/Xcode-16.3.0.app"
  },
  "lldb.library": "/Applications/Xcode-16.3.0.app/Contents/SharedFrameworks/LLDB.framework/Versions/A/LLDB",
  "lldb.launch.expressions": "native"
}

Alternatively, you can configure it uniformly in your .code-workspace file:

JSON
{
  "folders": [
    {
      "path": "CorDataCodes"
    }
  ],
  "settings": {
    "swift.path": "/usr/bin/",
    "swift.swiftEnvironmentVariables": {
      "DEVELOPER_DIR": "/Applications/Xcode-16.3.0.app"
    },
    "lldb.library": "/Applications/Xcode-16.3.0.app/Contents/SharedFrameworks/LLDB.framework/Versions/A/LLDB",
    "lldb.launch.expressions": "native"
  }
}

Manually Converting xcdatamodeld to momd

Xcode automatically converts xcdatamodeld to momd during compilation, but VSCode does not have this capability—even if the resources are declared as .process:

Swift
resources: [
    .process("Resources"),
]),

You need to perform the conversion manually. Below is an example script (placed in the Package root directory):

Bash
#!/bin/bash

# Replace with your module, target, and data model names
PACKAGE_NAME="CorDataCodes"
Target_Name="DataHandler"
MODEL_NAME="Model"

SOURCE_MODEL="Sources/$Target_Name/Resources/$MODEL_NAME.xcdatamodeld"
TARGET_DIR=".build/arm64-apple-macosx/debug/${PACKAGE_NAME}_${Target_Name}.bundle/$MODEL_NAME.momd"

if [ ! -d "$SOURCE_MODEL" ]; then
    echo "Error: Source model file not found: $SOURCE_MODEL"
    exit 1
fi

mkdir -p "$TARGET_DIR"
xcrun momc "$SOURCE_MODEL" "$TARGET_DIR"

if [ $? -eq 0 ]; then
    echo "Core Data model conversion successful!"
    echo "Source file: $SOURCE_MODEL"
    echo "Target file: $TARGET_DIR"
else
    echo "Conversion failed!"
    exit 1
fi

Run the command:

Bash
./xcmodel2momd.sh 

# Sample output:
Model.xcdatamodel: note: Model Model version checksum: 0wr8l/hdOcRIaAEOGJvaYn7eMvrqCq3uDvCCQiI3mSQ=
Core Data model conversion successful!
Source file: Sources/Persistent/Resources/Model.xcdatamodeld
Target file: .build/arm64-apple-macosx/debug/CorDataCodes_DataHandler.bundle/Model.momd

You should re-run the script each time you modify the model.

Outcome

After completing the configuration, you can directly click the test button to run unit tests that involve Core Data:

Further Reading

Weekly Swift & SwiftUI highlights!