jerney
jerney

Reputation: 2243

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

Upvotes: 3

Views: 8304

Answers (3)

Marco Boerner
Marco Boerner

Reputation: 1556

Same behaviour still with XCode 12.3 in 2021. Easy fix for me by just running the tests once.

I had the same issue with another pod added to the unit tests or UI tests. The podfile had the pod in the right target section. So it should recognize it.

The error went away as soon as I ran the tests (Cmd+U) Only building or running it is not enough since it won't try to build anything of the test targets.

The same problem sometimes happens when adding a new module, class etc. but also fixes itself after building the propper target.

Upvotes: 1

fguchelaar
fguchelaar

Reputation: 4909

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

Upvotes: 3

Václav
Václav

Reputation: 1000

Try this:

platform :ios, '12.2'

target 'UITestProto' do

  #some pods for your main target

  target 'UITestProtoTests' do
    inherit! :search_paths
    #pods for your unit tests
  end

  target 'UITestProtoUITests' do
    pod 'HydraAsync'
  end
end

And run pod deintegrate and pod update && pod install after this.

Upvotes: 2

Related Questions