Using UIImage imageNamed inside XCTestCase

Since your xctest project acts like different project It's mandatory to add your .png file for xctest also.

add your plane.png image to xctest project also. Make sure you have checked like

enter image description here

It works fine for me with the same code

enter image description here


In XCTest you aren't in mainBundle. So get your image from your XCTest bundle.

(this solution is for iOS8+)

ObjC

[UIImage imageNamed:@"..." inBundle:[NSBundle bundleForClass:[self class]] compatibleWithTraitCollection:nil];

Swift

UIImage(named: "...", in: Bundle(for: type(of: self)), compatibleWith: nil)

The accepted answer didn't work for me; here's what did.

let image = UIImage(contentsOfFile: 
    Bundle(for: type(of: self))
    .path(forResource: "plane", ofType: "png")!)!