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?

I submitted a request to DTS asking for their assistance, through which I skimmed down the project where I was having this issue. Attached is this project and I'm sending the link to this thread this evening. Hopefully in the next few days they will be able to answer the question here for the benefit of all concerned.

Link to sample project: https://www.icloud.com/iclouddrive/01bLWvFE-5ycofgxbpBsxDqDw#TransportBase-AbstractionForCodeLevelSupport2024-06-14

For those who have just stumbled upon this thread, my situation is that I am trying to run the code above in the first post to verify that the colors that I get from an asset catalog are being correctly converted into SwiftUI Color instances so that frequently used custom colors can be more easily managed. This conversion is done automatically using a Swift package plugin that generates the accessors automatically based on the asset catalogs. The persistent issue that I've been having for a longer period of time as I am writing this is that neither XCTest nor Swift Testing find the two instances equal because one is instantiated using the initializer that takes three Double instances (one red, one green, and one blue) and the other is instantiated using the initializer that specifies the color as it appears in an asset catalog.

These tests have been consistently failing as a result and are marked as being known issues. My updated version of this project uses Swift Testing for these tests, though the issue occurs regardless of the testing framework in use. I have thought about how it may not be necessary to do as deep as comparison. I could make sure that the correct symbols are being generated by filling in manually what I would expect from the generated code. I recently contacted Apple Developer Technical Support and am working with them to resolve my issue. I will occasionally post new updates on the status of my issue.

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