When do app sources need to be included in test targets?

前端 未结 7 1515
别那么骄傲
别那么骄傲 2020-12-02 05:30

In a new project I have this simple test

#import 
#import \"ViewController.h\"

@interface ViewControllerTests : XCTestCase
@end

@imp         


        
相关标签:
7条回答
  • 2020-12-02 05:47

    For me it was just a case of having no test targets added for the Scheme.

    For the app target go to Edit Scheme, then click Test on the right hand side then add a test target with the + button at the bottom:

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

    I ran into this as well and followed jackslash's recommendation but with one more addition: Select your main target and looks for Symbols Hidden by Default (under Apple LVM 5.0 - Code Generation), if the value is Yes, change it to No. This seems to 'un hide' all the symbols of the compiled sources that the unit test target is looking for. Works for me. Please make sure that you include all the steps that jackslash outlined as well.

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

    When you create a Unit Testing Bundle(Unit Testing target) for testing application you have two options

    1. Enable Allow testing Host Application APIs
    General -> Host  Application <app_name> -> >check< Allow testing Host Application APIs 
    
    • slow build
    1. Add every app's tested file into Target Membership[About]
    • you should take care of classes dependencies which are used into testable class(they should be added too)

    When you write a test and no one option was not enabled you can get

    Undefined symbol: nominal type descriptor for <class_name>
    Undefined symbol: type metadata accessor for <class_name>
    
    0 讨论(0)
  • 2020-12-02 05:57

    The answer was a combination of jackslash's and eph515's answers.

    As in eph515's answer symbols hidden by default should be No for debug.

    enter image description here

    Also deployment postprocessing should be No for debug.

    enter image description here

    Also all libraries that are included in the test target should be removed from the unit test. All that should be left are the 3 in the screen shot plus anything that is specific to unit testing.

    enter image description here

    Also if there is a run build script build phase at the end of the list, then it should be removed (since it is an artefact of unit testing).

    Then do everything in jackslash's answer.

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

    I spent some time figuring this out.

    If you read this documentation you find that Xcode has two modes for running tests. Logic Tests and Application Tests. The difference is Logic tests build their own target with your Classes and symbols built right in. The resulting executable can be run in the simulator and reports test output back to Xcode. Application tests on the other hand build a dynamic library linking to your code which is injected into the app at runtime. This allows you to run tests in iPhone environment and test Xib loading and other things.

    Because the symbols are missing from your test target when you unlink the source files it appears your older project seems to have a test target configured for logic tests, not Application (unit) tests.

    As these days Xcode seems to be trying not to distinguish between the two and defaults to creating an Application Tests target lets walk through all the things you might have to change to turn your Logic Test Target into a unit test one.

    I'm also going to assume that you have an Application Target and not a static library target as the directions will be a little different.

    1. In the build settings for your test target delete "Bundle Loader" and "Test Host" build settings. We will get Xcode to add these back later
    2. You need to remove all the .m files from your application from the test target. You can either do this by selecting all the .m files and removing the test target in the Xcode File inspector or you can use the compile sources build phase of the test target.
    3. Change the "Framework search paths" for your test target. For Xcode 5 they should be $(SDKROOT)/Developer/Library/Frameworks $(inherited) $(DEVELOPER_FRAMEWORKS_DIR) in that order and with no extra quotes or backslashes
    4. Go to the General pane of your test target's build settings and select your target from the drop down menu. If the menu already specifies your application target you should toggle it off and on again. This will make Xcode reconfigure the Bundle loader and Test Host settings with the correct value.
    5. Finally double check your application's scheme. In the scheme drop down select edit scheme. Then click the test action. Make sure you test target is in the list on the info pane and make sure all the tests are selected.

    This information more or less comes from the above linked documentation, but I updated the steps for Xcode 5.

    EDIT:

    Hmm 100% note what eph515 is saying about debug symbols being visible but you might also want to check that someone didn't set your scheme's test action to build in the Release or other configuration. Click the scheme selector an choose edit scheme. Click the test action and then make sure the Build Configuration is Debug

    build configuration screen for test action in a scheme

    If you have a Static Library Target

    So if you have a static library target you have two options: 1. Logic Tests 2. Application tests in a host app

    For 1. you have to make sure that Bundle Loader and Test Host are empty for your static library target. Your sources then have to be compiled into the test target as they would have no other way to be run.

    For 2. You need to make a new app Project in Xcode and add your static Library project as a subproject. You then need to manually copy the Bundle Loader and Test Host build settings from your New App's test target to your Static Lib test target. Then you open the scheme for your new Test App and add your test target to the tests action for the new app. To run the tests on your lib you run the test action for your host app.

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

    In my case in Xcode 6.2 was error in different architectures in project target and tests target.

    Project target has only armv7 and armv7s architectures (because of some older library)

    Project Tests target has armv7, armv7s and arm64 architectures.

    Removing arm64 architecture solve this issue for my case.

    Project Editor -> Project Tests target -> Build Settings -> Valid Architectures = armv7 armv7s
    

    (perhaps it is needed also to set "Architectures" instead of $(ARCHS_STANDARD) to $(ARCHS_STANDARD_32_BIT))

    0 讨论(0)
提交回复
热议问题