19

I am working with Unit Testing in Xcode using XCTest provided by Xcode in objective C. I know how to import Module in Swift like below.


@testable import  AppName

Whats the alternative in objective C.

Babul Prabhakar
  • 2,373
  • 1
  • 18
  • 25
  • Can't you just `#import` them? Or do you want to access private methods of Objective C? – Tomer Apr 27 '16 at 12:25
  • 1
    @Tomer : Then i need to import all files to compile sources of UnitTestCase target which doesn't seems to be good solution. – Babul Prabhakar Apr 27 '16 at 12:31
  • Your test target is linked against your main target. What do you need other than importing the .h? – Jon Reid Apr 28 '16 at 00:49
  • @Jon Ried : UITests has different target,thats why i need to import app files to my ui testing target. – Babul Prabhakar Apr 28 '16 at 05:47
  • This is huge problem for mixed code, if you have an objc class that uses a swift class you can't test the objc class because the swift one is not included in tests, i don't know how could they not think of that. Did you find any solution except writing all tests in objc and include the files in the tests target? I tried to disable testability but i have some tests that load from nib and the classes have a different module name than when running the app, so it failes. – Cristi Băluță Aug 22 '16 at 09:50

2 Answers2

9

@testable overrides access rights in Swift, allowing you to test internal methods in unit tests.

Objective-C has no such access modifiers, therefore you don't need @testable and you just import the module normally.

If you need to unit test internal Swift methods, you will have to write your tests in Swift.

Sulthan
  • 128,090
  • 22
  • 218
  • 270
  • 1
    According to Apple Documentation`Note: @testable provides access only for “internal” functions; “private” declarations are not visible outside of their file even when using @testable.` – Babul Prabhakar Apr 27 '16 at 12:41
  • @BabulPrabhakar All right, thanks, I have fixed that. – Sulthan Apr 27 '16 at 13:03
7

In Objective C you can simply #import them, as there are no such "internal" method access limitations as in Swift.

Also, On Xcode 6 your main target should be already linked to the test target. If not, try to check the "Allow testing Host Application APIs" checkbox inside Your Test Target > General > Testing. Take a look at this question for more information.

Community
  • 1
  • 1
Tomer
  • 3,149
  • 23
  • 26
  • I have been working with Functional UI Testing where i have to mock Core-Data objects which are used in app,therefore this solution will not work in my case. – Babul Prabhakar Apr 28 '16 at 05:43
  • 7
    Simply #import what? If the Swift files aren't in the test target, then they won't be in AppTest-Swift.h, and App-Swift.h isn't accessible when compiling the test target. – Uncommon Jul 17 '16 at 19:44
  • @Uncommon - add them to the test target and then they'll be included in the AppTest-Swift.h – Inti Apr 06 '21 at 13:00