Test Driven Development (TDD): Writing unit tests in Xcode 4 - objective-c

I am keen to write a library project with tests, even if I then include the code in other projects (as opposed to linking to the lib.) for inclusion on ios projects.
It seems checking the 'Include unit tests' isn't enough for xcode 4 to properly initialise a project with tests, and after looking through the Apple documentation I can't find anything relevant.
Does anybody know of any resources or tutorials for getting this working?
Even with the default Cocoa Touch Static Library project, upon choosing "Test" from the "Product" menu, it fails (when linking) with this error (the project is called test1):
library not found for -ltest1
Command /Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/llvm-gcc-4.2 failed with exit code 1
Any help would be much appreciated.

OK I finally got this working.
The trick was to create the library without checking the 'Include unit tests' option in XCode 4. Then I created a new target myself.
(in xcode 4)
Create a new Cocoa Touch Static Library project and DON'T check "Include Unit Tests"
Select the project in the navigator
Go into the "Build Phases" tab
Click 'Add Target' and choose 'Cocoa Touch Unit Testing Bundle'
Go through the wizard until the target is created
Then go in the "Product" menu and choose "Edit Scheme..."
Make sure your project (not the test target) is selected and, select "Test"
Click + to add a new target
Choose your test project and click Add
Now press Cmd+U (this is the shortcut for "Product > Test" menu item) and you'll notice the default test fails.
I hope this helps.

Related

XCUITest recording fails with "Please select a scheme where "XCUITodo" is the executable"

I'm using Xcode 8 to automate my application.
I get started by downloading an existing code: XCUIToDo project. I opened the project in Xcode, and when I click on the Record button, I'm seeing a warning message "Please select a scheme where "XCUITodo" is the executable" and the record feature does not start.
Please let me know where I'm missing.
TIA
Ensure that an executable is selected for your UITest scheme.
Select Product > Scheme > Manage Schemes
Select your UITest scheme in the list
Select Edit
Select the Info tab under Run
Ensure that the required Executable is selected
The problem is not on the UITest schema or target. When starting to record, the main schema (the one which runs the application) needs to be selected regardless if you have your UI tests on a different schema.
Click on Project Root, then Targets -> XCUIToDoUITests target. From the General tab, check the "Target Application" setting under Testing section. The value in the dropdown should match the XCUIToDo target.
For me after making sure everything mentioned above is selected properly, I had to choose same scheme (as selected under info in edit scheme) next to device selection on the top.

No coverage in 'all classes in scope' in Intellij

In Intellij IDEA 14.1.5 Community edition, I imported maven to get coverage from jacoco.exec file. Steps followed
right click on imported module.
select Analyze-->Show Converage Data.
provided valid jacoc.exec file and click 'show selected'
Instead of coverage i'm getting error as no coverage in 'all classes in scope'
can anybody suggest what is wrong?
I had the same thing happen to me.
I was able to fix this by going to "Edit Configurations", to the "Code Coverage" tab.
I'm not sure what caused it, but the wrong package namespace was listed there. I updated the entry there and my subsequent test run with code coverage succeeded.
It happens if your test class and class to test are in different package structures.
My test class was in:
com.tools.api
Class to be tested was in:
com.tools.ws
Once I've corrected the pattern as com.tools.* in the code coverage tab, I was able to see coverage results.
I had the same problem and found solution here.
In the Code Coverage tab, define the following options:
Specify the scope to measure code coverage for. Do one of the following:
To specify a class, click the Add Class button.
To specify a package, click the Add Package button.
I was able to fix this issue by:
Open Edit Configurations menu
Click on Modify Options > Coverage settings > Specify classes and packages
In the new box that appears, click on the plus to add a package
Select a high level package from the project

How do I add .m files to Xcode without conflicts

I'm currently learning Objective-C and working my way through a book which contains lots of exercises. At the beginning of the book I set up an OS X project command line project and have been working in the main.m file, block commenting prior exercises, but it's becoming tiresome working in such a long file, constantly commenting and un-commenting the exercise code I want to build. I'd like to create separate files in the Navigator for each exercise so they are easy to find. How would I go about doing this without causing issues with the file in the Products folder?
I'm currently getting this red alert when I try to build after adding a new .m file:
Undefined symbols for architecture x86_64:
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
This might not be a direct answer to the question (multiple files...) and a somewhat unorthodox approach to it, but it is a fun and convenient way to achieve what you describe by utilizing unit testing.
I understand that you are just starting to learn the language and the tools so I will make it as easy as possible. Let's get started!
1. Add a test target to your project
You just select the project in the navigator, and press the + button under 'targets' to add a new one.
select OS X > Other > Cocoa Testing Bundle and click 'Next'
add a product name (something like 'Exercises' would make sense in your case) and click 'Finish'
Now you should be able to see your new target in the list
2. Configuring the test target
Click and hold on the 'Run' button, select 'Test' from the popup menu
Now Xcode will ask you if you'd like to configure the project for testing. Click 'Edit Scheme...'
Select 'Test' from the left and click on the + button
select the test target we have created in previous step and click 'Close'
3. The Fun part
Now if you click on the 'Test Navigator' you'll see a list of your tests (your 'exercises'). Adding a test is as simple as adding a method starting with test - for example -(void)test_Exercise_x.
Furthermore you will now be able to verify that your answer to each exercise is what should be by using assertions and selectively run a test (exercise) on its own by clicking on the icon in the left of each test (the circled one in the screenshot) or of course, all of them by clicking on the 'Test' button or selecting Product > Test from the menu
Objective-C, C and C++ programs must include precisely one definition of main().
I expect it would work for you do something like:
int main( int argc, char *argv[] )
{
printf( "Solution to exercise #%d\n", getExerciseNumber() );
mySolution();
return 0;
}
you could reuse this file in each of the Xcode projects for your exercises. Alternatively you could have just one project that contained multiple targets, one for each exercise.
Does that help? I'm not completely clear that I understand your question.
Adding multiple files to my Xcode project looks like it's beyond my current abilities. I'll try and attempt to break the project down at a later date, when I'm more confident with Objective-C and Xcode. Thanks for your comments and help.

How to embed ParseKit as a private framework in a Mac App bundle

I need to install ParseKit to compile with cocoa under Mac Os X, I use xcode 4.
I have searched online but there is only a guide for installing parse kit for iPhone.
Where do I find the download for Mac Os X and/or a guide?
Developer of ParseKit here.
OK, after working through a tricky issue in Xcode 4, I have figured out my preferred way to do this: Create a new Workspace ("MySuite") which contains two sub-Projects
Your Mac Cocoa Application Project ("MyApp")
The ParseKit Framework Project ("ParseKit")
You can choose different names than "MyApp" and "MySuite" of course.
There's a few different ways to make this happen. Here's one way:
First, make sure you update to the very latest version of the ParseKit from the Google Code trunk. I have recently modernized the Xcode project for Xcode 4.3.1.
svn checkout http://parsekit.googlecode.com/svn/trunk/ parsekit-trunk
Make sure you do not have the ParseKit Xcode Project window open. This is an issue in Xcode up to version 4.3.1 (and maybe later, not sure).
Create a Mac "Cocoa Application" Project named "MyApp". File > New > Project…. (You may have already created your app. That's fine. Then skip this step.)
Drag the ParseKit.xcodeproj file from the Finder to the very top of the Project Navigator in the "MyApp" Xcode Project window. NOTE: make sure you drop the file at the very top of the Project Navigator tree. Otherwise it will not work.
Xcode will present a dialog: "Do you want to save this project in a new workspace?" Click "Save" and name the Workspace something like "MySuite".
Select the "MyApp" Project in the Project Navigtor.
Select the "MyApp" Target in the "Targets" list.
Select the "Build Phases" tab.
Click the disclosure triangle next to "Target Dependencies" to open the list.
Click the "+" button at the bottom of the list.
Select "ParseKit.framework" from the resulting dialog and click "Add" to add ParseKit as a dependency of your target. This ensures ParseKit is built before your target.
Click the disclosure triangle next to "Link Binary With Libraries" to open the list.
Click the "+" button at the bottom of the list.
Select "ParseKit.framework" from the resulting dialog and click "Add".
Click the disclosure triangle next to "Link Binary With Libraries" to open the list.
Click the "+" button at the bottom of the list.
Select "ParseKit.framework" from the resulting dialog and click "Add".
See "ParseKit.framework" in the "Link Binary With Libraries" list.
Click the "Add Build Phase" Button, choose "Copy Files" in the popup.
In the new "Copy Files" build phase, select "Frameworks" in the "Destination" popup.
Drag "ParseKit.framework" from the Project Navigator to the list in the new "Copy Files" build phase.
In MyAppDelegate.m, import the ParseKit header:
#import <ParseKit/ParseKit.h>
In -[MyAppDelegate applicationDidFinishLaunching:] do:NSString *g = #"#start = Word+;";
PKParser *p = [[PKParserFactory factory] parserFromGrammar:g assembler:self error:nil];
NSError *err = nil;
id result = [p parse:#"foo bar baz" error:&err];
NSLog(#"%#", result);
Build and run.
For more info on this topic, see:
Apple's docs
Chapter 16 of Mastering Xcode 4 by Joshua Nozzi
Open your project in Xcode 4
Make sure Project Navigator is shown
Ctrl click on your project file (the blue page icon)
Select Add Files to "YourProjectName"
Select ParseKit.xcodeproj
In Project Navigator select your project file
Go to Build Phases
Disclose Link Binary With Libraries and either add ParseKit.framework by clicking + or drag'n'drop it from Project Navigator's Products directory of ParseKit.framework
Add Copy File phase by clicking bottom right Add Build Phase button.
Set destination to frameworks
Repeat 8, but for new build phase
You may also need to add Target Dependency: repeat step 8, but for this phase. But looks like Xcode can sort out them itself.
I also recommend you to create a Workspace, it makes managing of subproject much easier and looks like this fixes most part of potential problems, because if Xcode fails to resolve dependencies, you can always add script to copy files manually, since products of projects will share the same build directory.
I managed to do this using cocoapods. Try that if your stuck still
Developer of ParseKit here.
I'm sorry I don't have a good answer for you. I have some outdated docs on how to use ParseKit in your iOS application using Xcode 3.
However, I've just tried to go through the process of embedding ParseKit.framework within a Mac OS X app using Xcode 4, and I honestly could not figure out how to do it in Xcode 4. I am baffled.
What I can say, is that embedding ParseKit.framework in your Mac app should not be very different from embedding any other framework in your Mac app. There's nothing particularly special or unusual about ParseKit in this regard. The problem is I just can't figure out how to do that at all in Xcode 4.
Here's Apple's documentation on how to do this:
Embedding a Private Framework in Your Application Bundle > Using Separate Xcode Projects For Each Target
Unfortunately, Apple's docs are also out of date, and also describe the process in Xcode 3.
I have asked a question here on Stack Overflow to try to solve this problem.

libxml/tree.h no such file or directory

I am getting following errors.
libxml/tree.h no such file or directory
I have already added libxml2.dylib to my project, however I am getting this type of trouble.
Please help me.
Follow the directions here, under "Setting up your project file."
Setting up your project file
You need to add libxml2.dylib to your project (don't put it in the Frameworks section). On the Mac,
you'll find it at /usr/lib/libxml2.dylib and for the iPhone, you'll
want the
/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.0.sdk/usr/lib/libxml2.dylib
version.
Since libxml2 is a .dylib (not a nice friendly .framework) we still
have one more thing to do. Go to the Project build settings
(Project->Edit Project Settings->Build) and find the "Search Paths".
In "Header Search Paths" add the following path:
$(SDKROOT)/usr/include/libxml2
Also see the OP's answer.
Adding libxml2 in Xcode 4.3 / 5 / 6
Adding libxml2 is a big, fat, finicky pain in the ass. If you're going to do it, do it before you get too far in building your project.
You need to add it in two ways:
1. Target settings
Click on your target (not your project) and select Build Phases.
Click on the reveal triangle titled Link Binary With Libraries. Click on the + to add a library.
Scroll to the bottom of the list and select libxml2.dylib. That adds the libxml2 library to your project.
2. Project settings
Now you have to tell your project where to look for it three more times.
Select the Build Settings tab.
Scroll down to the Linking section.
Under your projects columns double click on the Other Linker Flags row.
Click the + and add -lxml2 to the list.
Still more.
In the same tab, scroll down to the Search Paths section.
Under your projects column in the Framework Search Paths row add /usr/lib/libxml2.dylib.
In the Header Search Paths and the User Header Search Paths row add $(SDKROOT)/usr/include/libxml2.
In those last two cases make sure that path is entered in Debug and Release.
3. Clean
Under the Product Menu select Clean.
Then, if I were you (and lets face it, I probably am) I'd quit Xcode and walk away. When you come back and launch you should be good to go.
For Xcode 6, I had to do the following:
1) add the "libxml2.dylib" library to my project's TARGET (Build Phases -> Link Binary With Libraries)
2) add "$(SDKROOT)/usr/include/libxml2" to the Header Search Paths on the TARGET (Build Settings -> Header Search Paths)
After this, the target should build successfully.
You also need to add /usr/include/libxml2 to your include path.
Form the link of #Matt Ball,
I found following helpful to me.
You need to add libxml2.dylib to your project (don't put it in the Frameworks section). On the Mac, you'll find it at /usr/lib/libxml2.dylib and for the iPhone, you'll want the /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS2.0.sdk/usr/lib/libxml2.dylib version.
Since libxml2 is a .dylib (not a nice friendly .framework) we still have one more thing to do. Go to the Project build settings (Project->Edit Project Settings->Build) and find the "Search Paths". In "Header Search Paths" add the following path on the Mac:
/usr/include/libxml2
Ray Wenderlich has a blog post about using gdata that solves this problem. Basically these simple steps:
In XCode, click Project\Edit Project Settings and make sure “All Configurations” are checked.
Find the Search Paths\Header Search Paths setting and add /usr/include/libxml2 to the list.
Finally, find the Linking\Other Linker Flags section and add -lxml2 to the list.
original post: read and write xml documents with gdataxml
I found the same, I had to add $(SDKROOT)/usr/include/libxml2 for the latest Xcode (4.3.x). ALSO, what kept me circling around for hours is the fact that I was modifying the "TARGET" and not the "PROJECT" (the new UI of Xcode is so intricate that its easy to overlook this). You need to modify the PROJECT!
Another solution. do all the steps in header search path etc. and make sure your selected configuration in project in Project settings is the correct one. When you double click on project build settings ,you may be changing in Distribution settings, But you are trying to add header search path in "Debug" settings. So make sure you are in correct settings. or choose all settings
I found that with xCode 4.3.2 I had to enter
$(SDKROOT)/usr/include/libxml2
into the Header Search field rather than simply
/usr/include/libxml2
As of Mavericks (OS X 10.9) the /usr/include directory is gone. Half of the answers here are obsolete, as the application will not compile until you sort out the include directory.
I solved the problem creating a symbolic link to MacOSX SDK in terminal, using the following command:
sudo ln -s /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/include /usr/include
The application now compiles again.
I'm not sure what the difference is but add the include path to the project as well as the target.
On Mountain Lion I was facing same issue, which was resolved by adding /usr/include/libxml2 to include paths with flag "recursive", use this if all above is not fruitful.
I had this problem when I reopened a project (which was developed on XCode 3.something on Leopard) after upgrading to Snow Leopard and XCode 3.2. Curious enough, it only affected some kinds of builds (emulator builds went fine, device ones gave me the error). And I have libxml2 at /usr/include, and it indeed contains libxml/tree.h.
Even the magic "Clean" did not work, but "Empty Caches..." under the "XCode" menu (between the Apple logo and File) did the trick (was that menu there in previous versions?). Beats me the reason, but after a clean there were no more complaints regarding libxml/tree.h
Also select "Always Search User Paths" to YES. In XCode 4.3.3 its by default NO
Please follow the following steps
Adding libxml2
libxml2.dylib can be found on your mac machin at /usr/lib/libxml2.dylib
Change "Header Search Paths"
Click on [Project Name] (in left panel) -> Project -> Build Settings -> Select All (default is Basic)
Type Header Search Paths in search box
Double click on Header Search Paths -> + -> "$(SDKROOT)/usr/include/libxml2"
Add -lxml2 to "Other linker flag"
Search for "Other Linker Flags" as search in step 2
click on the "Other Linker Flags" row. Click the "+" and add "-lxml2" to the list.
Change your project type to ARC -> No i.e Automatic Reference Counting to No
You can search ARC as per in step 2
Xcode 4.5
I have used The CW's solution entirely.
The only exception is that $(SDKROOT)/usr/include/libxml2 didn't work for me, and I had to add "$(SDK_DIR)"/usr/include/libxml2 to my Projects Header Search Paths and User Header Search Paths.
After that project builds successfully.
EDIT: I have Google GData project inside my project (called MyProject) (my project uses). GData requires libxml. To build project MyProject successfully, I add "$(SDK_DIR)"/usr/include/libxml2 to Header Search Paths of MyProject and no to Header Search Paths of GData . If I didnt add it to MyProject, project did not build).
I found this visual tutorial useful.
Blockquote
Adding libxml2 in Xcode 4.3.x
Adding libxml2 is a big, fat, finicky pain in the ass. If you're going to do it do it before you get too far in building your project.
Here's how.
Target settings
Click on your target (not your project) and select "Build Phases". Click on the reveal triangle titled "Link Binary With Libraries". Click on the "+" to add a library. Scroll to the bottom of the list and select "libxml2.dylib". That adds the libxml2 library 2 your project… but wait.
Project settings
Now you have to tell your project where to look for it three more times.
Select the "Build Settings tab". Scroll down to the "Linking" section. Under your project's columns double click on the "Other Linker Flags" row. Click the "+" and add "-lxml2" to the list.
Still more.
In the same tab, scroll down to the "Search Paths" section. Under your project's column in the "Framework Search Paths" row add "/usr/lib/libxml2.dylib".
In the "Header Search Paths" AND the "User Header Search Paths" row add "$(SDKROOT)/usr/include/libxml2". In those last two cases make sure that path is entered in Debug AND Release.
Then. Under the "Product" Menu select "Clean".
This is working and for Xcode5 too! Thank you!
Don't put libxml2.dylib under frameworks folder put it under root just below the root(Top left blue icon )
Then Click on the Project (TOP Left blue icon) ,GO to Build Settings,in the search box type "Header Search Paths" and then add the this "$(SDKROOT)/usr/include/libxml2"
This code resolve my issue hope it will help you fix this
#Aqib Mumtaz - I got it working by following the instructions in Parris' note above entitled "Adding libxml2 in Xcode 4.3 / 5 / 6". The step in using a Framework Search Path does not work and the compiler complains. Big kudos to that fella anyway!
I am using Xcode 6.2b3
Regardless of the version of Xcode you are using, it is buggy. Don't always assume that compile errors are real. There are many times when it does not follow header search paths and includes clearly listed are not found. Worse, the errors that result tend to point you in different directions so you waste a lot of time dinking around with distractions. With that said...
Recommend baby steps by starting with this exactly...:
create a single window Mac OS X Cocoa project called "Bench Test"
add XpathQuery into your project source directory directly in the Finder
click on the tiny folder icon under the project window's red close button
drag XpathQuery (folder if you contained it) into the project assets on the left of the project window's display
drag /Applications/Xcode/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/usr/lib/libxml2.2.dylib into your project assets, at the bottom. This will add it into your "Build Phases" -> "Link Binary With Libraries" the easy way
click on the "Bench Test" project icon in the project assets column, top of the left
search for "Other Linker Flags" under "Build Settings"
add "-lxml2" (sans "") to "Other Linker Flags" under the "Bench Test" project icon column
search for "header search" under "Build Settings"
add "$(SDKROOT)/usr/include/libxml2" (sans "") to "Header Search Paths" under the "Bench Test" project icon column
add "$(SDKROOT)/usr/include/libxml2" (sans "") to "User Header Search Paths" under the "Bench Test" project icon column
Notes:
Mine would not work until I added that search path to both "Header Search Paths" and "User Header Search Paths".
To get to the libxml2.2.dylib in the finder, you will need to right click your Xcode icon and select "Show Package Contacts" (editorial: what a hack.. cramming all that garbage into the app)
Be prepared to change the linked libxml2.2.dylib. The one inside Xcode is intentionally used to ensure that Xcode gets something it knows about and in theory was tested. You may want to use the dylib in the system later (read up in this thread)
As I am using Xcode 6.2b3, I may have a newer libxml2.2.dylib. Yours could be named slightly different. Just search the folder for something that starts with "libxml" and ends with ".dylib" and that should be it. There may also be an alias like "libxml2.dylib". Don't use that right away as resolving an alias adds another variable into the Xcode "what could have bugs" equation.
For sanity sake, I make aliases of the external libraries, rename them to indicate which one they are, and keep them at the same level as the project file in the Finder. If they change location, change name, etc, the alias will have in it's Get Info, the original file's full path for later detective work to get the project compiling and linking again. (symlinks break too easy and are not natural to the Mac)
Last thing, and very important, see http://www.cocoawithlove.com/2008/10/using-libxml2-for-parsing-and-xpath.html where you can download XpathQuery and get some more goodness.
hope this helps.
If you happen to be developing something for Veterans, oh say an iPhone / iPad or Mac app, and are working against something called "MDWS" or "VIA" which are SOAP based interfaces to the medical record system... please contact me
I solved in three steps inXcode 8.0 + Unit Test Project
1) I added libxml file to the Build Phases.follow this link
2) I added Other Linker Flags as -ObjC (For both project and test project targets)
3) I setup Header Search Path as "${SDKROOT}/usr/include/libxml2" (Don't forget double quote in either side) (For both project and test project targets)
One of them will solve the issue. If not you have to apply all three of the above.
i tought i added wrongly, then i realize the problem is it not support arc, so check the support one here, life saver -> http://www.michaelbabiy.com/arc-compliant-gdataxml-library/