XCTest does not import the swift file

Platform: iOS.


Programming language: Swift.


What i have done?

Created a test target and given the Host application as the target which i am trying to write test cases.

Checked the Allow Testing Host Application API's.

Added Target dependencies to the particular target that i want to test.

Added pod dependencies to the test target as well.


I am trying to import a swift file in XCTests class but it does not import. If i give the target membership for that particular file, i am facing lot of errors out of no where. What could be the best way to import the swift class. What are the settings that are to be added or modified in the project settings ?Any suggestions?

Replies

When unit testing Swift files you should not import individual Swift files. What you should do is use the @testable import statement in the unit test class to give the unit test class access to all of the application's Swift files.


@testable import MyApp


Where MyApp is the name of the application you are unit testing. If you get link errors after adding @testable import, make sure the Product Module Name build setting matches the name you pass to @testable import, MyApp in the example. The most common cause of the names not matching is your application name having spaces in it.

  • This worked. Thanks.

Add a Comment