Target individual XCTest unit test cases in Xcode 5 to a specific iOS device for a universal app? Target individual XCTest unit test cases in Xcode 5 to a specific iOS device for a universal app? xcode xcode

Target individual XCTest unit test cases in Xcode 5 to a specific iOS device for a universal app?


To target device specific tests one would need to edit the schemes for a project. Under Product > Scheme > Edit Schemes one can choose to select device specific tests per device.

Scheme Editor


Heres my proposed solution.

Split your tests up into iPhone specific and iPad specific tests.

Now add a new Target (Cocoa Touch Unit Testing Bundle) specifically for iPhone or iPad. If you already have many common tests written, it may be more prudent to duplicate your current test target.

Now ensure that your iPhone specific test classes are only included in your iPhone test target by clicking on the class in the Navigator, and then open up the Utilities panel. You can set which target your class is a member of by using the Target Membership check boxes.

Target Membership panel

To expand on this you can add different schemes for your two targets to make running the tests quicker.


I was looking to do this same thing and came across this old post. My solution involves naming specific device tests to end in either "Pad" or "Phone" and then filtering the list of tests to include the current device's tests:

class MyTests: XCTestCase {    override class var defaultTestSuite: XCTestSuite {        let suite = XCTestSuite(forTestCaseClass: MyTests.self)        let newSuite = XCTestSuite(name: "MyTests")        for test in suite.tests {            // Name is of the form "-[MyTests test*]"            if test.name.hasSuffix("Pad]") {                // iPad only test                if UIDevice.current.userInterfaceIdiom == .pad {                    newSuite.addTest(test)                }            }            else if test.name.hasSuffix("Phone]") {                // iPhone only test                if UIDevice.current.userInterfaceIdiom == .phone {                    newSuite.addTest(test)                }            }            else {                // Can run on both devices                newSuite.addTest(test)            }        }        return newSuite    }    func testAll() { // All devices test }    func testOnlyPad() { // iPad only test }    func testOnlyPhone() { // iPhone only test }}