175

I'm trying to use Swift's @testable declaration to expose my classes to the test target. However I'm getting this compiler error:

enter image description here

Intervals is the module that contains the classes I'm trying to expose. How do I get rid of this error?

AD Progress
  • 4,190
  • 1
  • 14
  • 33
hgwhittle
  • 9,316
  • 6
  • 48
  • 60

12 Answers12

288

In your main target you need to set the Enable Testability build option to Yes.

As per the comment by @earnshavian below, this should only be used on debug builds as per apple release notes: "The Enable Testability build setting should be used only in your Debug configuration, because it prohibits optimizations that depend on not exporting internal symbols from the app or framework" https://developer.apple.com/library/content/releasenotes/DeveloperTools/RN-Xcode/Chapters/Introduction.html#//apple_ref/doc/uid/TP40001051-CH1-SW326

Cœur
  • 37,241
  • 25
  • 195
  • 267
sgaw
  • 3,118
  • 1
  • 17
  • 6
  • Hi, I'm getting the same error but not seeing the enable testability option and am using xcode 7. Any idea what I should do? – user1601259 Sep 22 '15 at 18:24
  • 3
    @user1601259 - You can find the option by selecting the top level project, going to Build Settings, and looking under the Build Options heading. – hgwhittle Sep 23 '15 at 13:27
  • @hgwhittle - When i select the top level project, all I see in build options is "debug information format" and "validate build product." When I select the target in build options I see "embedded content contains swift code." That's it. When I search in build settings I cannot find it. Is this because of xcode 7? – user1601259 Sep 26 '15 at 19:38
  • 1
    It is possible that you have selected "Basic" in your "Build Settings". If you select "All" on the left side, you will see "Enable Testability" – Oleg Novosad Oct 23 '15 at 09:31
  • Note that if you get this from your Xcode server bot, it might be from the Analyse or Profile stages, which you won't typically run locally. Check the logs. – Robert Atkins Dec 04 '15 at 10:57
  • @sgaw suggest adding a note about only using on debug builds per apple release notes: "The Enable Testability build setting should be used only in your Debug configuration, because it prohibits optimizations that depend on not exporting internal symbols from the app or framework" https://developer.apple.com/library/watchos/releasenotes/DeveloperTools/RN-Xcode/Chapters/xc7_release_notes.html – earnshavian Jan 05 '16 at 15:44
  • 1
    How exactly should this work for release builds, where tests will run (so need `@testable`) but need to submit to the App Store? If `Enable Testability` is just for debug builds, what is the way around this? Do I have to pull out my test code for release? – timgcarlson Feb 05 '16 at 17:49
  • @sgaw Doesnt work for me. But only when I try to archive binary. Any solution? – Bartłomiej Semańczyk Aug 01 '16 at 13:42
  • @timgcarlson did you ever figure this out? you bring up a good point – vikzilla Mar 22 '18 at 16:38
  • How to achieve this using post_install hook in Podfile? – superarts.org Sep 18 '19 at 17:43
22

In my case I used a custom build configuration for testing (called Test) and also cocoapods as a dependency manager

I had to add the following lines to the end of my Podfile to enable testability

post_install do |installer|
    installer.pods_project.targets.each do |target|
        target.build_configurations.each do |config|
            if config.name == 'Test'
                config.build_settings['ENABLE_TESTABILITY'] = 'YES'
            end
        end
    end
end

By default cocoapods sets ENABLE_TESTABILITY to YES only for Debug builds

Tim
  • 1,877
  • 19
  • 27
  • 1
    You save my day, the problem was that I have custom configs so Cocoapods was not finding the config "Debug" – 93sauu Feb 25 '21 at 10:24
19

Make sure that you properly set your checkboxes under your app scheme. You SHOULD UNCHECK your test targets for Archive Build.

enter image description here

Ben Scheirman
  • 40,531
  • 21
  • 102
  • 137
Bartłomiej Semańczyk
  • 59,234
  • 49
  • 233
  • 358
  • 2
    For more information look here: https://developer.apple.com/library/mac/technotes/tn2215/_index.html – P. Pawluś Aug 11 '16 at 13:32
  • If you have separate targets please make sure that both of them have the same schemes. For example, Dev, Prod etc. – Taras Jul 03 '23 at 15:26
11

For those of you who are experiencing this only upon running Xcode Profiler: switch profile build configuration in your scheme management to the one that has testability enabled - and that would be debug in most cases:

enter image description here

Wladek Surala
  • 2,590
  • 1
  • 26
  • 31
  • 1
    This was the answer for me. The Test portion of the scheme was using the wrong build configuration. – Rick Jan 20 '22 at 23:08
  • If you have separate targets please make sure that both of them have the same schemes. For example, Dev, Prod etc. – Taras Jul 03 '23 at 15:26
11

I started getting this error when running tests using Bitrise.

Unlike other users says, this is not per Target basis, or per Schema basis, it is per Configuration basis. Select Target -> Build Settings tab -> look for testability -> Enable it on the Configuration that you are using.

Please notice that Apple recommends to enable this on the configuration that you are using for debugging, not for AppStore.

rgkobashi
  • 2,551
  • 19
  • 25
8

This is probably because your main target Enable Testability is set to NO. You should set it to YES in the debug scheme (which is used for running your tests).

If you use Carthage, this problem can be caused by importing frameworks with @testable, because they are built with a release scheme.

Most of the times it's bad practice to import frameworks with that prefix, so you could avoid it. If you can't, you should Enable Testability in the frameworks' release scheme. https://developer.apple.com/library/content/releasenotes/DeveloperTools/RN-Xcode/Chapters/Introduction.html#//apple_ref/doc/uid/TP40001051-CH1-SW326

Samuel B.
  • 363
  • 1
  • 4
  • 10
2

If by any chance you have

install! 'cocoapods',
         generate_multiple_pod_projects: true,
         incremental_installation: true

Then, this is the way to do it.

    # generated_projects only returns results if the we run "pod install --clean-install"
    # or install a pod for the first time

    installer.generated_projects.each do |project|
        project.build_configurations.each do |configuration|
            configuration.build_settings["ENABLE_TESTABILITY"] = "YES" 
        end
    end
Legonaftik
  • 1,350
  • 3
  • 17
  • 33
Nuno Gonçalves
  • 6,202
  • 7
  • 46
  • 66
  • For a release build of the app, would one need to then do another clean install without the "ENABLE_TESTABILITY"? As development continues, would one repeatedly install with and without this build setting on the cocoapods projects to allow use of UITests? – JKaz Sep 09 '20 at 23:35
2

I'm a beginner and I just can't remember where all the settings and options are in Xcode. So I always feel frustrated when I see "you can set xxx to yyy ..." in answers to this kind of question. Where are those xxx in Xcode? and What are those yyy? We need a map showing how we get things done, but not another clue, like xxx. It seems we are Nicolas Cage in National Treasure!

Thanks for all the clues here, very much! You really help me. But I have a concrete answer here (which is actually from you):

  1. Select the main target of your project
  2. Go to the "Build Settings" tab
  3. Turn on the "All" option
  4. Search for the "Build Options" section
  5. Set "Enable Testability" to "Yes"
  6. Wait for a few minutes or restart Xcode (then the error disappeared)
Cable W
  • 633
  • 1
  • 8
  • 17
1

I got this when my new Tests.swift file belonged to the wrong Target.

Graham Perks
  • 23,007
  • 8
  • 61
  • 83
0

Above solution is fine if you are using pods/Carthage. But if you are using frameworks from iOS itself 'e.g. Contacts', you need add path to these frameworks in 'Library Search Paths' of your main project's target. enter image description here

nikBhosale
  • 531
  • 1
  • 5
  • 27
0

If you trying to test framework:

Go to test target -> Build Phase -> Create new copy files phase -> Choose frameworks -> Add all recursively used frameworks

Abhishek Bedi
  • 5,205
  • 2
  • 36
  • 62
Zaporozhchenko Oleksandr
  • 4,660
  • 3
  • 26
  • 48
-5

This didn't occur in my projects prior to Xcode 8, but after I upgraded to Xcode 8, it made me perplexed.

The answers posted here didn't get my problems resolved. For me, I just ditched these tests as it is not needed. So uncheck the test buttons:

enter image description here And now the error has gone out.

Blaszard
  • 30,954
  • 51
  • 153
  • 233
  • 3
    If you don't need the tests, it might be better to remove the test targets from your project instead of removing them from all the builds. – Jonathan Cabrera Jun 21 '18 at 21:08