“No such module” when using @testable in Xcode Unit tests

后端 未结 30 1370
春和景丽
春和景丽 2020-12-02 05:33

I recently updated to Xcode 7 beta 5. I tried adding a unit test to an earlier project, but I am getting the error message \"No such module [myModuleName]\" on the @te

相关标签:
30条回答
  • 2020-12-02 05:53

    So this is how I went about getting my code to work after trying all suggested solutions from prior suggestions.

    • I set 'Enable testability' to 'YES' in project's Build Settings
    • I also set 'Defines Module' to 'YES' in my project's Build Settings.
    • For the regular .swift file(s) within my project, say MyApp, I was going to write test cases for, I have both the main "MyApp" and the "MyAppUnitTests" Targets checked under Target Membership.
    • I then selected my unit test file(s), declared the '@testable import MyApp' at the top, beneath the 'import XCTest', and only checked the "MyAppUnitTests" under Target membership

    And everything worked like charm. Hope this helps.

    0 讨论(0)
  • 2020-12-02 05:54

    This was fixed for me when I changed the Deployment Target from 9.3 to 11.0.

    General > Deployment Target > "11.0"

    0 讨论(0)
  • 2020-12-02 05:55

    Click the MyAppTests.swift in the project navigator, and click the right panel, check your module in target Membership. It works in mine.

    0 讨论(0)
  • 2020-12-02 05:57

    The answer that worked for me

    The answer was that I had some errors in my project that was making the build fail. (It was just your standard every day bug in the code.) After I fixed the errors and did another clean and build, it worked.

    Note that these errors didn't show up at first. To get them to show up:

    • Comment out your entire Test file that is giving you the "No such module" error.
    • Try to run your project again.

    If there are other errors, they should show up now. Fix them and then uncomment your Test file code. The "No such module" error was gone for me.


    In case this doesn't solve the problem for other people, you can also try the following:

    Clean the build folder

    Open the Product menu, hold down Option, and click "Clean Build Folder..."

    Make sure that Enable Testability is set to Yes

    In the Project Navigator click your project name. Select Build Settings and scroll down to Build Options. Make sure that Enable Testability is Yes (for debug).

    Delete and re-add your Tests target

    If you have done the other things my guess is that you probably don't need to do this. But if you do, remember to save any Unit Tests that you have already written.

    Click your project name in the Project Navigator. Then select your Tests target. Click the minus (-) button at the bottom to delete it.

    Then click the plus (+) button and choose iOS Unit Testing Bundle to add it back again. As you can see, you can also add a UI Testing Bundle in the same way.

    A few other ideas

    • Make sure that all required classes are members of your test target.
    • Make sure that you have added all the required libraries.
    • Make sure that the module name is written correctly (see this answer).

    Or...

    Leave a comment or answer below if you found something else that worked.

    Related

    • How to do a Unit Test in Xcode
    • Xcode UI Test example
    0 讨论(0)
  • 2020-12-02 05:58

    One other thing to check: If you have an Objective-C project, but are writing unit tests in Swift, make sure the main target uses at least one Swift file!


    More info:

    I was working on an Objective-C project, but wanted to write unit tests in Swift.

    I added a Swift file to the main target to generate the necessary ProjectName-Bridging-Header.h file, wrote my tests and everything was working properly.

    Later on I deleted the Swift file because I thought I didn't need it (all of the main target's code is in Objective-C... I was only writing tests in Swift).

    I didn't notice a problem until later, after I did a "clean/clean build folder" and the "No Such Module" problem showed up. After some head scratching I added a new blank Swift file and the problem went away.

    I've tested it multiple times with/without the Swift file, and it only works with it... so, I'll either need to leave the blank file in the project, convert some Objective-C into Swift, or add some new code to the project written in Swift.

    0 讨论(0)
  • 2020-12-02 06:00

    The problem for me was the iOS deployment target of the tests was not set to be the same as the main target. So be sure to check this.

    In your test target:

    Build Settings -> iOS Deployment Target -> iOS<same as the target you are testing>
    
    0 讨论(0)
提交回复
热议问题