iOS test suite - Adding the whole project sources to test - ios

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

Related

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:
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.

Including a pod inside a framework target: file not found

I'm using framework targets (for better code reuse and IB_Designables), and I've already had a framework target working perfectly. I've decided to move some other classes to a framework target too.
I've set up the pods (just a single one in this case), but whenever I try to include the pod I'm getting not found error.
No change if I try to use the modules approach too:
The problem is that I've already got another framework too, with the same settings (cross checked all the compiler settings/linker flags/build phases etc) and that framework has no issue importing its pods.
Here is my podfile (TUComponents is the working on, TUModels is the failing one):
[...]
target 'TUComponents' do
pod 'AHKNavigationController'
pod 'TTTAttributedLabel'
use_frameworks!
end
target 'TUModels' do
pod 'JSONModel'
use_frameworks!
end
Even weirder; Xcode has no problem code-completing importing the JSONModel/JSONModel.h header (or JSONModel in case of module #import). But when I try to compile, it fails.
What might be wrong with my configuration?
UPDATE: If I give up using frameworks in pods and use regular old static library, and set allow non-modular includes in frameworks to YES, I can build. But I have no idea why I can't build when using Pod frameworks.
Maybe try solution from: https://www.natashatherobot.com/cocoapods-installing-same-pod-multiple-targets/
platform :ios, '9.0'
use_frameworks!
# My other pods
def testing_pods
pod 'JSONModel'
end
target 'TUComponents' do
pod 'AHKNavigationController'
pod 'TTTAttributedLabel'
testing_pods
end
target 'TUModels' do
testing_pods
end
From iOS - Build fails with CocoaPods cannot find header files :
Make sure your Podfile includes link_with on targets missing a config file. Cocoapods only sets the first target by default otherwise. e.g.
platform :osx, '10.7'
pod 'JSONKit', '~> 1.4'
link_with 'Pomo', 'Pomo Dev', 'Pomo Tests'
Restarting Xcode fixed it for me
The following steps worked in my case:
Quit the Xcode n Simulator
Delete Drived data
Again open your project
Clear it

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