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

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:
Launch XCode, create new project (Single View App, Swift, unit tests and UI tests). I named my project UITestProto
Run pod init on the project.
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
Go to the Build Settings of the UITestProtoUITests target in XCode and set "Always Embed Swift Standard Libraries" to $(inherited):
Run pod install
Open the project using UITestProto.xcworkspace
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:
adding #testable import UITestProto because I've had to do that for my unit tests
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

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

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.

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.

Related

Why would boilerplate unit tests in a boilerplate Cocoa Touch Framework project with a single Cocoapod dependency fail without even running?

Here are the step needed to reproduce:
(1) Using XCode 9.2, create a new Cocoa Touch Framework project called "Whatever" including Unit Tests.
(2) Close project and add the following podfile to the project folder.
platform :ios, '10.0'
use_frameworks!
target 'Whatever' do
pod 'Signals'
# Signals is used, but any pod seems to produce the problem
end
(3) Run pod setup and pod install.
(4) Open Whatever.xcworkspace.
(5) Open the Test Navigator and run the boilerplate tests.
Not only do the tests fail, but the little diamonds in the line numbers fail to turn red, or any colour
What is happening here, please?
Thank you for reading.
I suspect this question is a duplicate, but you likely need to add your test target to your Podfile. In this example, I assume it is WhateverTests.
platform :ios, '10.0'
use_frameworks!
target 'Whatever' do
pod 'Signals'
# Signals is used, but any pod seems to produce the problem
end
target 'WhateverTests' do
inherit! :search_paths
pod 'Signals'
end
After making this change, run pod install.

iOS test suite - Adding the whole project sources to test

As I started to use test suites I realized (supposing I'm not doing anything wrong) that I have to manually add all the sources I want to use in my test suites.
i.e: if in my main project I have MyClass.swift, I have to go to the Test target > Build Phases > Compile Sources and add all the sources I want to compile.
Also, I can't see my Cocoapods frameworks there without going into Test target > Build Phases > Link Binary With Libraries and adding Pods_MyProject.
I just want to know if this is how we always have to do.
You do not need to add the sources at all.
At the top of each test file. import your module to test.
So if your apps name is UIKitTesting you would import it like so
#testable import UIKitTesting
For testing you need to include pods that you want to work when your are testing.
Example using Kingfisher for images and you need to test using that framework, you need po add in your pod file.
Below pod file, add pods for testing:
platform :ios, '10.2'
// default name so we don't repeat
def pods
pod 'Kingfisher'
pod 'Firebase/Messaging'
end
target 'ProjectName' do
# Comment the next line if you don't want to use dynamic frameworks
use_frameworks!
pods
target 'ProjectNameTests' do
inherit! :search_paths
# Pods for testing
pods
end
end

How to fix missing module in unit-test file?

So I'm very new with unit test in Swift. In my project I'm using a couple of frameworks installed in my cocoa pods, but when I was about to write some code in my test file, I always get this error. missing module: Firebase, Eureka, ImageRow
I tried to import these modules above the #testable, but somehow it didn't recognize the module. it keeps on saying module not found. I've also tried to remove my pods inside the inherit search paths, and it still on asking that I need to import these module. Here's my cocoa pod.
target 'ProjectRed' do
use_frameworks!
pod 'Firebase'
pod 'Eureka'
pod 'ImageRow'
pod ‘Firebase/Database’
pod ‘Firebase/Storage’
# Pods for ProjectRed
target 'ProjectRedTests' do
inherit! :search_paths
# Pods for testing
end
target 'ProjectRedUITests' do
inherit! :search_paths
# Pods for testing
end
end
target 'ProjectRedTests' do
inherit! :search_paths
pod 'Firebase'
pod 'Eureka'
pod 'ImageRow'
pod ‘Firebase/Database’
pod ‘Firebase/Storage’
For me putting those modules in ProjectRedTests worked.
You need to add those frameworks to your test target also.
Select your test target in the Targets section of your project.
Select the Build Phases tab.
Add your frameworks to the Link Binary With Libraries section.
(Note: you may need to add a Copy Files phase before Link Binary With Libraries to get them into the right position.)

Xcode 8 - Could not build module 'CoreFoundation' in ios10

using Xcode 8.
iOS 10
Objective-C
Pod file consist :- pod 'XMPPFramework'
i want to integrate XMPPFramework in my project.
installed pods.
after installing pods when i open xWorkspace. these errors comes.
podfile :-
Uncomment the next line to define a global platform for your project
platform :ios, ‘8.0’
target 'Roj' do
# Uncomment the next line if you're using Swift or would like to use dynamic frameworks
# use_frameworks!
# Pods for Roj
pod 'XMPPFramework'
target 'RojTests' do
inherit! :search_paths
# Pods for testing
end
target 'RojUITests' do
inherit! :search_paths
# Pods for testing
end
end
these are the errors which comes in my project see this image
Try this solution in Build settings ;
You can resolve this issue by implementing following changes.
1) Change the following flags in you Project's Build Settings.
2) Rename your module.modulemap files. Name them anything except module and then change the path of this renamed file in your Xcode.
Clean it, Build it, it will resolve the issue.
P.S - Other compilation issues will get resolved automatically.
This may help:
Go to Build Settings
Search for Compile Source As
Set it to Objective-C++
See this issue: https://github.com/react-native-community/react-native-google-signin/issues/361#issuecomment-379013147

No such module `Quick` using cocoapods

I've been learning swift for three days. I'm planning to build my skill in swift especially in iOS development. I just advised by my colleague who is an iOS developer to learn swift while writing test in our app -- this is for the benefit to the company and myself which it makes sense for me. Now, I'm trying to follow this tutorial
https://medium.com/#ynzc/getting-started-with-tdd-in-swift-2fab3e07204b
After following the tutorial to rewrite the test in a framework called Quick which is very similar with Rspec. I did the installation of the CocoaPod then follow the install of the Quick framework. Use the .xcworkspaces instead of the .xcodeproj. But still I'm getting the error of No such module 'Quick'.
I did research already and removing the pods but still getting the error.
platform :ios, '9.2'
target 'FizzBuzz' do
use_frameworks!
def test_pods
pod 'Quick', '~> 0.9.0'
pod 'Nimble', '~> 3.2.0'
end
target 'FizzBuzzTests' do
inherit! :search_paths
test_pods
end
target 'FizzBuzzUITests' do
inherit! :search_paths
test_pods
end
end
screenshot of the project folder:
I recently had this issue and none of the current answers solved this for me.
The reason I was getting this error was that the Test/Spec file (NetworkSpec.swift) that I had created had a target membership of the main application target, not the tests target.
To update this, I opened the project in xcode, selected the file in the project explorer and then in the properties window on the right hand side. Then in the target membership area. I had two options.
ProjectName
ProjectNameTests
I unchecked the checkbox next to ProjectName (not the app's real name) and then checked the one next to ProjectNameTests and re-ran the tests. Everything worked as expected.
I ran into the same issue. I did not need to nest the test targets in my podfile. e.g.
source 'https://github.com/CocoaPods/Specs.git'
use_frameworks!
target 'MyAppName' do
pod 'RealmSwift'
pod 'GoogleMaps'
end
target 'MyAppNameTests' do
inherit! :search_paths
pod 'Quick'
pod 'Nimble'
end
target 'MyAppNameUITests' do
inherit! :search_paths
pod 'Quick'
pod 'Nimble'
end
post_install do |installer|
installer.pods_project.targets.each do |target|
target.build_configurations.each do |config|
config.build_settings['SWIFT_VERSION'] = '3.0' # or '3.0'
end
end
end
I am using Xcode 8.1. Click on the currently selected scheme.
Select 'manage schemes'.
Selected scheme.
Put check mark next to test schemes and Quick.
Click on your app scheme again. Select each scheme and go to Product > Build. Finally select your main app scheme again. Now try adding 'import Quick' in one of your test classes.
Schemes
ISSUE RESOLVED
I was getting same issue. Files within Test folder did not see Nimble and Quick pods despite my Podfile was configured correctly and Target Membership for my test file set up correctly as well. I was unable to import both Pods.
Running below commands on Project folder via CLI resolved issue for me.
$ pod update Quick
$ pod update Nimble
My Podfile implementation of both pods for reference:
def include_test_pods
pod 'Quick'
pod 'Nimble'
end
Try this. Cocoapods may need to be rebuilt.
Quit XCode. Go to terminal and cd into your project.
Run pod deintegrate.
Run pod install.
Open Xcode and clean and build your .xcworkspaces project.
I had this issue that persisted through build cleans. I noticed that it was only happening in one file and files that did not have this issue had import foundation. After importing foundation on the line before it, it then worked. I thought maybe it was simply modifying the file that had done it so I removed the foundation import and the error came back. So try adding that import if you do not have it.
First thing first you just need to use pod inside your target project, so it gona share for your test target like that
# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'
target 'TestNetWorkLayer' do
use_frameworks!
pod 'SwiftyJSON'
pod 'Quick'
pod 'Mockingjay'
pod 'Nimble'
pod 'Alamofire', '~> 5.0.0-rc.3'
target 'TestNetWorkLayerTests' do
inherit! :search_paths
end
end
After that you can import it inside your project
import Quick
import Nimble
class NativeApiClientSpec{
}
In my Podfile i use smth like this:
target 'Specs' do
pod 'Quick'
pod 'Nimble'
shared_pods
end
Additionally, re-open the project or use the following command: Product -> Clean and build folder
I had same issue. After a lot of trial and error, I found fix here: https://github.com/Quick/Quick/issues/402#issuecomment-149459840. I had to delete derived data and regenerate project to get Quick as build scheme
Try this;
Close xcode project
Run $ pod deintegrate
Delete Podfile.lock
Run $ pod install
and Build again!

Resources