1

UserDefaults does not appear to function in an XCTest when running on Linux. I'm running the below test but userDefaults.string(forKey: "key") returns nil on Linux when on macOS it returns the correct value and passes.

I'm using Docker to execute the tests on Linux. My Dockerfile is as follows:

FROM swiftdocker/swift

WORKDIR /package

COPY . ./

RUN swift package resolve
RUN swift package clean
RUN swift test --parallel

The test file:

import XCTest

class UserDefaultsTests: XCTestCase {

    func testUserDefaults() {

        let userDefaults = UserDefaults.standard

        userDefaults.set("value", forKey: "key")

        _ = userDefaults.synchronize()

        XCTAssertEqual(userDefaults.string(forKey: "key"), "value") //nil is not equal to "value"
    }

    static var allTests = [
        ("testUserDefaults", testUserDefaults)
    ]
}

UserDefaults appears to be implemented in Swift Core Libs here, which is available on Linux, so I'm not sure why it wouldn't work.

Patrick Goley
  • 5,397
  • 22
  • 42

0 Answers0