Including asset catalog in targets

In one of my XCTests classes XCTests I need to load an image from the asset catalog in order to test the image processing logic. However, it seems that using UIImage(named: "imageName") returns nil for the target of the test.

I checked the target testing membership in my asset directory, is there anything else I have to do to enable image reading from my XCTest classes?

+5
source share
2 answers

@Mylovemhz's answer is correct, but incomplete. You need to specify the link:

Swift 3:

 UIImage(named: "imageName", in: Bundle(for: MyTestClass.self), compatibleWith: nil) 

Also make sure your asset is included in the same goal as your test class.

+1
source

According to the documentation you have to add them at runtime.

If your tests use files, data files, images, etc., they can be added to the test suite and available at run time using the NSBundle APIs. Using +[NSBundle bundleForClass:] with your test class ensures that you get the right asset recovery kit. For more information, see NSBundle Class Link .

In Swift, it will be something like:

 let appBundle = Bundle(for: type(of: MyClass) as! AnyClass) 
0
source

Source: https://habr.com/ru/post/1261546/


All Articles