Adding Unit Tests to an already existing project? - iphone

When you create an XCode 4 project, it asks you if you want to include unit testing. But how do you add it to a project that's been created without them?
I started a project with Unit Tests to try to figure it out, and it looks like the only difference between it and my existing project are the Test.h and Test.m files in the new project.
But when I move them into my old project and try to build, it says no such file or directory: SenTestingKit/SenTestingKit.h. That looks like a framework to me, but if I go to build phases and try to add the framework, there isn't one available called SenTestingKit :( And the new project only links the usual suspects: UIKit, CoreGraphics, and Foundation, but no SenTestingKit.

Answer updated for Xcode 10.2.
Glad you asked this question — it's never too late to add tests to a project!
Open your targets panel by selecting your project in the navigator,
Click the + button that's at the bottom of the target list,
Select iOS Unit Testing Bundle under Test section in iOS tab,
Provide the required information and click Done.
It's that simple. Xcode will generate a target, some boilerplate files such as Info.plist and an initial test case file for you. Happy testing!

Also, if you want to be able to do cmd-U to run your tests then delete the scheme that was created for the test bundle and instead edit your main application scheme and add the test bundle in the Test configuration. See this screenshot:

Xcode 7 update
Method one
File > New > Target...
Choose iOS Unit Testing Bundle. (If you want to add UI Testing then choose the UI Testing Bundle.)
Method two
Click your project name in the Project Navigator. You will see TARGETS listed. At the bottom of the screen press the plus (+) button and add the iOS Unit Testing Bundle.
The test targets in this image have already been added, but you can see where the add button is.
Related
How to do a Unit Test in Xcode
Xcode UI Test example

One more variant using Test navigator
Xcode version 10.2.1
Open Test navigator(⌘ Command + 6)
+ at the bottom
New Unit Test Target...
Choose options
Do not forget add next import to test internal[About] elements
#import "myClass.h" for Objective-C
#testable import module_name for Swift

This is pretty descriptive how-to guide:
Adding Unit Tests to an existing iOS project with Xcode 4

Some more tips to the correct answer:
In XCode 6 it is much easier now since you don't need to fix any build settings.
Change the bundle id on a test target to the correct one, if needed, by clicking the target -> Info -> Bundle Identifier.
Press CMD + U and your tests will run.
If you use CocoaPods (which is likely), you should also add Pods to the Configurations of your Project.

[Updated for Xcode 10.2]:
Open your Xcode project
Open targets
Click "Add Target" button
Go to "Test" section
Select "iOS Unit Testing Bundle"

Related

Can't seem to import a framework into my current project in Xcode 10.2.1

Here's what I've done so far:
Create a Single View App project called MyApp
Create a Cocoa Touch Framework project called MyFramework
Created a Swift file with a class as public class FrameworkClass
Dragged MyFramework.xcodeproj into MyApp's Project Navigator - This caused Xcode to create a workspace.
I added the framework to MyApp.General.EmbeddedBinaries
Now it looks like this so far but I can't seem to import my framework still:
Please follow the steps to resolve your problem:
Choose your project, rather than a specific target, and open the
Build Settings tab.
In the Other Linker Flags section, add -ObjC.
If these settings are not visible, change the filter in the Build Settings bar from Basic to All.
Right before alt-tabbing Xcode to submit this response, I built on reflex and it turns out that it won't detect the references until you build once. After that, it should start to be detected by the text editor.
I was going to delete this question however, I already typed it up anyway so might as well. Hope this helps someone!

Xcode Unable to add test target to existing swift project - Target to be tested is grayed out

I have an existing project that I am trying to add unit tests to.
I'm on Xcode 10.1 and project is purely in Swift.
I start by clicking on "New Target..."
In the first dialog I am able to choose the project but the dialog box for "Target to be tested" is grayed out.
I have tried to go ahead and add the test target without choosing and this results in my test bundle not being able to find my existing classes - "No such Module error"
Here is the dialog for adding the test bundle:
Not sure what I'm doing wrong to prevent the this selection
Thanks for any help!
In Xcode 10.2.1, with your project open, click on the diamond with a dash in it:
Then down at the bottom left of the project window click on the '+' and select "New Unit Test Target".

Cannot add Alamofire to Swift Project

Trying to add alamofire to swift project using unstruction from here
Did all these steps, clean project a lot of timer and restarted XCode, nothing helps. Error does not dissappear
http://i.stack.imgur.com/kNU3R.png
"Cannot load underlying module for 'Alamofire'" and nothing to do
Changes I did:
1) added project file to my project
i.stack.imgur.com/eUe8E.png
2) added to build phases panel
i.stack.imgur.com/1wanl.png
3) added to general tab
i.stack.imgur.com/L3TTR.png
What`s wrong with that?
I had the same problem. I was able to fix it by doing the following.
Download the Alamofire "Source" folder.
https://github.com/Alamofire/Alamofire/tree/master/Source
Drag it into your xCode Project, and click "Copy Items if Needed". If you view the project in Finder, the Alamofire Source folder should be in the project itself. (Ex: I have 3 items. Example App, Example App.xcodeproj, Example App Tests. The folder should be in Example App.)
Finally, you should be done! However, now, you do not need to import Alamofire, since you are not actually loading a framework.
For example, instead of doing Alamofire.upload, you will now only do upload.

How to drag & drop file in xcode?

when i drag and drop any files than its show like below image.
so i want to know that what is "PrizeperdayTests"?
is i have to checked it when adding some files? what if i not cheked it? i ask this question because when i run my app from Xcode it will run but when i try to run my app using api file than its just close instantly.
This is a unit test target created by Xcode, probably as part of the project template you chose.
Unless you are dragging in files required by your unit tests, you can leave "PrizeperdayTests" unchecked.

How to add new iPhone target

I'm trying to add a new target to my Xcode project so that I can run the same app, but with subtle difference.
Please can someone guide me through the setup of a new target since it is my first time and I'm not sure how to go about doing it.
In particular, I'm interested how I make the new target run the code in the original app. When I tried creating a new target it just made a new app delegate, and viewController file.
Sorry if this is simple, I'm just quite confused.
EDIT:
Please note that I'm after after instructions based in Xcode 4.
In xcode 4, make sure you're in the folder view where you have the project. Select the blue project button, then next to that you can see Targets, Select the target you have there, right click & select duplicate target.
Now you have two build targets.
To apply subtle differences in your app, make a global C flag. in Build settings there is a paragraph named GCC 4.2 - Language, it has a property named Other C Flags. Add your flag here like so:
-DOTHER_VER
Now in your code you can check for this using:
#ifdef OTHER_VER
// some code.
#else
// the subtle difference.
#endif
After you create your new target from settings of your project, you can create an identifier class to check the target. If you use macros everywhere in your code, it'll not be readable and feasible. You can check this tutorial-blog to learn how to do it, besides you may see some best practices there.