28

I have class named Meal.swift in my project and a unit test

func testMealInitialization() {
    // Success case.
    let potentialItem = Meal(name: "Newest meal", photo: nil, rating: 5)
    XCTAssertNotNil(potentialItem)

    // Failure cases.
    let noName = Meal(name: "", photo: nil, rating: 0)
    XCTAssertNil(noName, "Empty name is invalid")

}

But the problem is that: Use of unresolved identifier "Meal"

Swift Dev Journal
  • 19,282
  • 4
  • 56
  • 66
Imran Sh
  • 1,623
  • 4
  • 27
  • 50

5 Answers5

25

@testable import MyApp should work fine. Just remember to set appropriate configurations within Debug for your UITest target.

enter image description here

Bartłomiej Semańczyk
  • 59,234
  • 49
  • 233
  • 358
  • I get "no such module" even after changing the configurations to .debug as your picture shows. – Matt Jun 21 '17 at 23:25
  • I had to do this: https://stackoverflow.com/questions/32008403/no-such-module-when-using-testable-in-xcode-unit-tests/44687800#44687800 – Matt Jun 21 '17 at 23:35
22

Xcode 7 adds the @testable import statement to simplify unit testing. At the top of your unit test class files, add the following statement:

@testable import MyApp

Where MyApp is the name of your iOS app. Now the unit test target should be able to find the classes in your app and run the tests. If you get link errors saying that Xcode cannot find your app classes, make sure the Product Module Name build setting's value matches the name you use in the @testable import statement, MyApp in this example.

If @testable import does not work for you, a workaround is to make your app classes members of the unit test target. You can set the target membership of a file in Xcode using the file inspector.

Swift Dev Journal
  • 19,282
  • 4
  • 56
  • 66
  • 1
    `@testable import` doesn't work for my UITest class. Why? There must be a way to do this without manually adding every file to the UITest target. – Bartłomiej Semańczyk Aug 16 '15 at 20:35
  • It's glitchy – I haven't been able to make @testable work on our large project as of yet. Usually, the problem is a "no such module" error during compile time, and after endless hours of fiddling I decided that it just doesn't work reliably, and definitely not on UI test classes. – I'm pretty certain it has problems if you have more than one package (or "module") in your project. For instance, we have everything organized into different packages, such as Utility, Model, View, etc. We can't get the test classes to successfully import these modules. – Zaphod Aug 23 '15 at 23:19
  • 2
    **Product Module Name build setting** made my day - thank you! – user1244109 Nov 24 '15 at 14:57
  • The workaround (manual add of class to the test project) is the only thing that worked for me in a workspace project with multiple pods and a Carthage framework. – Tommie C. Apr 14 '17 at 04:15
  • Had the same problem, solution was to set "Define modules" in project settings. Afterwards I had to make sure both targets are using the same deployment target. Thanks to https://stackoverflow.com/a/62456877/198996 and https://stackoverflow.com/a/45480473/198996 – TomTasche Nov 08 '20 at 09:44
  • Also if you are using CocoaPods you might need this too: https://stackoverflow.com/a/58164638/198996 – TomTasche Nov 08 '20 at 09:56
7

I also encountered this issue, what worked for me is reloading my test file and retyping the

@testable import FoodTracker

then at that point, it detected my FoodTracker classes (Meal class) and errors are gone.

ivyr
  • 71
  • 1
  • 1
7

Click on your Meal class. Then on the right side you'll see 'Target Membership' section. Select your test project. (Xcode 7) Voilà.

Manu
  • 629
  • 7
  • 6
0

In my case, I got error only in the new class I've just made, and it makes me confuse. So, it works by select Unit Test target under class membership of my new class. Or delete the class, make new class again, then select Unit Test Target in that new class.

Firda Sahidi
  • 1,227
  • 1
  • 11
  • 22