title: NSImage from Asset Catalog in Test Target
body: <p>My unit tests target needs to load an image resource for use in some of the tests, but I am having trouble loading it.</p>
  
  <ol>
  <li>I have created an <strong>asset catalog</strong> within the test target.</li>
  <li>I have added a new <strong>image set</strong> resource to the catalog, checked the "Mac" box in "Devices", and set its source images for both resolutions (@1x and @2x):</li>
  </ol>
  
  <p><a href="https://i.stack.imgur.com/cPsLl.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/cPsLl.png" alt="enter image description here"></a></p>
  
  <ol start="3">
  <li><p>I have checked the "copy bundle resource" build phase of the test target, and it does include the asset catalog.</p></li>
  <li><p>I have checked the target membership of the asset catalog and image set, and it is indeed the test target.</p></li>
  </ol>
  
  <hr>
  
  <p>When running the tests, I am attempting to load the image using code like below:</p>
  
  <pre><code>guard let image = NSImage(named: NSImage.Name("TestSourceImage")) else {
      fatalError("Test Resource is Missing.")
  }
  </code></pre>
  
  <p>...but the guard fails.</p>
  
  <p>I also tried:</p>
  
  <pre><code>let bundle = Bundle(for: MyClassTests.self)
  
  guard let path = bundle.pathForImageResource(NSImage.Name("TestSourceImage")) else {
      fatalError("Test Resource is Missing.")
  }
  
  guard let image = NSImage(contentsOfFile: path) else {
      fatalError("Test Resource File is Corrupt.")
  }
  </code></pre>
  
  <p>...but the first guard fails (can't retrieve the resource path).</p>
  
  <p>I have tried both formats</p>
  
  <pre><code>NSImage.Name("TestSourceImage")
  </code></pre>
  
  <p>and:</p>
  
  <pre><code>NSImage.Name(rawValue: "TestSourceImage")
  </code></pre>
  
  <p>I have also tried <code>Bundle.urlForImageResource(_)</code>, but it fails too.</p>
  
  <hr>
  
  <p>I have seen similar questions and answers, but they either apply to <strong>iOS</strong> or to resources in the <strong>app</strong> (main) bundle.</p>
  
  <p><strong>What am I missing?</strong></p>
  
  <hr>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>In the meantine, I've <strong>worked around</strong> the problem by adding my test image as a stand-alone image resource (<em>not</em> using asset catalogs), and  loading it with the following code:</p>
  
  <pre><code>let bundle = Bundle(for: MyClassTests.self)
  guard let url = bundle.url(forResource: "TestSourceImage", withExtension: "png") else {
      fatalError("!!!")
  }
  guard let image = NSImage(contentsOf: url) else {
      fatalError("!!!")
  }
  </code></pre>
  
  <p>I don't need to support multiple resolutions in this case (my image is the source for an image processing algorithm; it is already assumed to be at the highest resolution necessary), and even if I did, I would just switch from <strong>.png</strong> to <strong>.tiff</strong>, I guess.</p>
  
  <p>The problem seems to be that, <code>NSImage</code> does <strong>not</strong> have an initializer analogous to <code>UIImage</code>'s <code>init(named:in:compatibleWith:)</code>, which lets you specify the <strong>bundle</strong> from which to load the image (second argument). </p>
  
  <p>You can instead ask a specific bundle (other than <code>main</code>) to create a resource URL and instantiate the image from that (like I did above), but this isn't compatible with asset catalogs it seems. <strong>Further information is welcome...</strong></p>