Color asset test not working due to color not being loaded when test conditions are evaluated

I've written a Swift package plugin to add the colours provided in the asset bundles in an extension of the SwiftUI Color type but I'm having difficulties testing this since the bundle and/or colour that is in the asset catalogs are not yet loaded when XCTest goes to evaluate the test conditions. Here is my test code: (colours are just random colours, some derive from macOS colour picker; also, the new static method is functionality that I added and is not present in the existing Color APIs)

final class TestCase: XCTestCase {
func testBanana() {
        XCTAssertEqual(Color.banana, Color.new(from: "FEFC78")!)
    }
    
    func testAlexsColor() {
        XCTAssertEqual(Color.alexsColor, Color.new(from: "0432FF")!)
    }
    
    func testMaximumPowerColor() {
        XCTAssertEqual(Color.maximumPower, Color.new(from: "FF2600")!)
    }
    
    func testLongNameColor() {
        XCTAssertEqual(Color.reallyLongNameThatJustKeepsGoingOnAndOnAndOnAndOn, Color.new(from: "AAA000"))
    }
}

Here is an example error message that I get when I run the test:

testBanana(): XCTAssertEqual failed: ("NamedColor(name: "Banana", bundle: Optional(NSBundle </Users/trevorhafner/Library/Developer/Xcode/DerivedData/TransportBase-cbagdabrompfzofwkimswvlsincu/Build/Products/Debug/TransportBaseTests.xctest/Contents/Resources/TransportBase_TransportBaseTests.bundle> (not yet loaded)))") is not equal to ("#FEFC78FF")

Here is the autogenerated code that my Swift Package Plugin creates:

#if canImport(SwiftUI)
import SwiftUI
import Foundation

@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
public extension Color {
    static let banana = Color("Banana", bundle: .module)
    static let alexsColor = Color("Alex's Color", bundle: .module)
    static let reallyLongNameThatJustKeepsGoingOnAndOnAndOnAndOn = Color("really long name that just keeps going on and on and on and on", bundle: .module)
    static let maximumPower = Color("Maximum Power", bundle: .module)
}
#endif

Does anyone know how to somehow instruct the bundle or the Color to load such that the comparison can be made correctly between the two colours?

since the bundle and/or colour that is in the asset catalogs are not yet loaded

I think the not yet loaded message is a red herring. This is Bundle telling you that the bundle’s code isn’t loaded, and that makes sense because the .module bundle has no code.

I recommend that you temporarily expand the implementation of banana to run in multiple steps:

  1. Locate the bundle.

  2. Use a platform specific API (NSColor or UIColor) to get a colour from that bundle.

  3. Create a Swift Color from that.

At which point does it fail?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

While I have a different setup the general problem remains them same. I use SwiftGen to generate symbols for my colors (and images, and Strings) and I get a crash when trying to access the color in a test (because it cannot be found in the bundle. Did you solve this issue somehow?

Color asset test not working due to color not being loaded when test conditions are evaluated
 
 
Q