Search code examples
iosswiftcocoapodsui-testing

iOS/Swift: "No such module..." for UI Testing


I'm trying to create automated UI tests for my iOS app. After generally failing to get this working on my existing app, I created a new one from scratch and tried there. It always seems to fail because it can't import dependencies I've installed with Cocoapods.

I'm currently running XCode Version 10.2.1 (10E1001)

Instructions to replicate:

  1. Launch XCode, create new project (Single View App, Swift, unit tests and UI tests). I named my project UITestProto
  2. Run pod init on the project.
  3. Add the HydraAsync dependency

The Podfile should look like this:

# Uncomment the next line to define a global platform for your project
platform :ios, '12.2'

target 'UITestProto' do
  # Comment the next line if you're not using Swift and don't want to use dynamic frameworks
  use_frameworks!

  # Pods for UITestProto

  target 'UITestProtoTests' do
    inherit! :search_paths
    # Pods for testing
  end

  target 'UITestProtoUITests' do
    inherit! :search_paths
    # Pods for testing
    pod 'HydraAsync'
  end
end
  1. Go to the Build Settings of the UITestProtoUITests target in XCode and set "Always Embed Swift Standard Libraries" to $(inherited):

Embedded swift std libs

  1. Run pod install
  2. Open the project using UITestProto.xcworkspace
  3. Open the UITestProtoUITests.swift file and try to import the OHHTTPStubs module.
import XCTest

import Hydra

class UITestProtoUITests: XCTestCase {
...

At this point you should see the error:

No such module 'Hydra'

I've tried:

  1. adding @testable import UITestProto because I've had to do that for my unit tests
  2. Making sure "Enable Testability" in "Build Settings" is set to "Yes"

And I've cleaned the build folder and closed/open XCode after each of those steps, but still no luck on the Hydra import.

Note: I'm not actually using Hydra for testing, it's just a library that I've successfully used in projects in the past


Solution

  • This is related to a CocoaPods issue. The workaround suggested here does the trick for me. You may need to rebuild the project though.

    For future reference, I've cc-ed the post:

    # Moving the UITests target outside of the main target 
    # in the Podfile seems to have helped. So now instead of this:
    
    target 'Target' do
      use_frameworks!
      ...
    
      target 'TargetTests' do
        inherit! :search_paths
        ...
      end
    
      target 'TargetUITests' do
        inherit! :search_paths
        ...
      end
    
    end
    
    ## we have this:
    
    target 'Target' do
      use_frameworks!
      ...
    
      target 'TargetTests' do
        inherit! :search_paths
        ...
      end
    end
    
    target 'TargetUITests' do
        inherit! :search_paths
        ... # all the pods we normally use
     end
    

    Credits to PWrzesinski