Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode - Test class File is part of module, ignoring import

Tags:

xcode

ios

So I have an iOS project with tests and everything was working properly.

I felt the need to change the project name. I did as indicated at apple's docs, selected the project and changed its name.

After a while of updating things for this to finally work, the app runs ok, but I'm still having a problem.

I can't run the tests because of this annoying issue. I'm sure it's a basic thing, but still I can't quite figure it out.

Here's my test class (for the sake of the question I'm using a Foo example)

@testable import MyApp // File FooTests is part of module MyApp. Ignoring import.

class FooTests: QuickSpec {

  override func spec() {

     describe("a foo test") {
         it("tests foo") {
             let return = Foo.barMethod()
             XCTAssertEqual(return, "expected", "Expected something, got \(return) instead")
         }
    }
 }

The Foo class is not being recognized, and I get the error that the test class is part of MyApp which is not the case.

Where should I look for?

like image 749
Nuno Gonçalves Avatar asked Jan 20 '16 00:01

Nuno Gonçalves


3 Answers

I just stumbled on the same problem, and your post was helpful. What I found out is by change the Product Name to AppNameTests, as per your example, then the product module name is reflected correctly and you don't have to add Tests at the end of $(PRODUCT_NAME:c99extidentifier). c99extidentifier seems torefers to Product Name.

Summary:

  • select your test target in Project
  • navigate to Build Settings -> Packaging
  • change the Product Name to your previous test target, likely appending Tests
  • I believe the rename as per Apple's renaming a project doesn't include the updating the module in @testable, so I had to do this manually
like image 90
Jean-Frederic PLANTE Avatar answered Nov 11 '22 21:11

Jean-Frederic PLANTE


[UPTADE] This helped Jean-Frederic figuring out his answer. That's why I accepted it.

I finally managed to fix this, but I'm not happy with the solution.

Consider my app name AppName

For some weird reason I cannot explain, my test target Build Settings -> Product Module Name had $(PRODUCT_NAME:c99extidentifier) which value was the Appname, instead of AppNameTests. I checked with other projects and the value is the same but converted to AppNameTests so in this case I'm not sure what I can do to make it automatically update and include the Test part in the name.

What I ended up doing was setting the name as $(PRODUCT_NAME:c99extidentifier)Tests. Not sure if this is a proper solution, but for now it works.

If anybody has a better solution, please feel free to share.

like image 34
Nuno Gonçalves Avatar answered Nov 11 '22 22:11

Nuno Gonçalves


This is a directly parallel solution to Jean-Frederic Plante, though in my case the problem was not that my test target name matched my primary target, but rather that the module of the test target had incorrectly been set to match the module name of the main app target. To correct either of these issues:

  • Select your test target in your project.
  • Navigate to Build Settings > Packaging.
  • Ensure both the Product Module Name and Product Name are set to $(TARGET_NAME).
like image 4
Duncan Babbage Avatar answered Nov 11 '22 21:11

Duncan Babbage