Whats the alternative of @testable import in Objective C
Asked Answered
R

2

20

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.

Rankle answered 27/4, 2016 at 12:2 Comment(5)
Can't you just #import them? Or do you want to access private methods of Objective C?Gapeworm
@Gapeworm : Then i need to import all files to compile sources of UnitTestCase target which doesn't seems to be good solution.Rankle
Your test target is linked against your main target. What do you need other than importing the .h?Fernand
@Jon Ried : UITests has different target,thats why i need to import app files to my ui testing target.Rankle
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.Sucrase
R
10

@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.

Reinertson answered 27/4, 2016 at 12:33 Comment(2)
According to Apple DocumentationNote: @testable provides access only for “internal” functions; “private” declarations are not visible outside of their file even when using @testable.Rankle
@BabulPrabhakar All right, thanks, I have fixed that.Reinertson
G
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.

Gapeworm answered 27/4, 2016 at 12:54 Comment(3)
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.Rankle
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.Len
@Len - add them to the test target and then they'll be included in the AppTest-Swift.hDoering

© 2022 - 2024 — McMap. All rights reserved.