30

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"

9
  • 1
    sure you did not miss @testable if Meal is not public Commented Jul 10, 2015 at 8:10
  • @ChristianDietrich my Meal class is public Commented Jul 10, 2015 at 8:23
  • How to use @testable ? Commented Jul 10, 2015 at 8:25
  • then it should work without @testable (that is added to the import) Commented Jul 10, 2015 at 8:28
  • xcode is a bit flaky it simply may help to hit the product/test button Commented Jul 10, 2015 at 8:37

5 Answers 5

25

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

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

I get "no such module" even after changing the configurations to .debug as your picture shows.
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.

6 Comments

@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.
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.
Product Module Name build setting made my day - thank you!
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.
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 stackoverflow.com/a/62456877/198996 and stackoverflow.com/a/45480473/198996
|
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.

1 Comment

I had to reset xcode and when it reloaded the error was still there for a second while rebuilt the project
7

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

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.