How to use classes from another target for tests in XCTest - ios

I use Xcode 6.1 to create a new project, and it comes with a Tests target. I am trying to use the classes defined in another demo target in the same project for tests. But how do I accomplish this (besides setting the class's target membership to both demo and tests targets)?

Go to the source file in question and press option+command+1 to see the file inspector, and then select the appropriate targets in the "Target membership" section. Or, go to the target setting, click on "Build Phases" tab and add it to the "Compile Sources" section.

Related

Migration of OCUnit to XCTest leads to linker error

I've tried to migrate our OCUnit to XCTest. So I have 100% compilable project but I see next linker error:
Error:Undefined symbol '_OBJC_CLASS_$_XCTestCase' referenced from:...
My "Framework Search Paths" looks like this:
$(SDKROOT)/Developer/Library/Frameworks (non-recursive)
$(inherited) (non-recursive)
$(DEVELOPER_FRAMEWORKS_DIR) (non-recursive)
I'm not familiar what else to do to satisfy linker
I am seeing the exact same error messages in Xcode 6.1 after migrating with
Pull Down Menu → Edit → Refactor → Convert to XCTest.
I got 2 solutions:
Solution 1 (recreate test project)
Backup all source files in your test project.
In Project Navigator, select the project.
This shows the project settings.
In the project settings, click Show project and targets list.
Select the test target, and press delete to delete it.
In Project Navigator, select the test target, and press delete to delete it.
In Test Navigator, click the + button on the bottom left, and choose New Test Target.
Add your original source files back into the new test target.
Now test your project as usual.
Solution 2 (hackishly modify test project)
In Project Navigator, select the project.
This shows the project settings.
In the project settings, select the test target (not the main target), then select Build Phases.
In Link Binary With Libraries, click the + button.
This shows the list of frameworks, but there is no XCTest.
Choose a framework that you don't use at all, say, Twitter.framework, and click Add.
Open your project file (something.pbxproj) in a text editor, and replace all occurrences of Twitter.framework with XCTest.framework.
Note: the pbxproj file is inside the .xcodeproj folder. In Finder, you have to right click on the .xcodeproj and select Show Package Contents.
Go back to Link Binary With Libraries in Xcode, and check that XCTest.framework is added.
If not, try restarting Xcode.
If it is added but is red, just ignore it. Xcode can't find the framework, but the linker can.
Now test your project as usual.

After deleting DerivedData folder, unittests fail to compile with 'Apple Mach-O Linker' error

I have tried every single suggestion in the links below, without any solution. (XCODE 5.0.1)
Solution 1
Solution 2
d: file not found: /Users/hooman/Library/Developer/Xcode/DerivedData/F11i-erlvxsqudsegbmckzxfxnvnxxumb/Build/Products/Debug-iphonesimulator/F11i.app/F11i
clang: error: linker command failed with exit code 1 (use -v to see invocation)
The strange part is, when I look at this path:
/Users/hooman/Library/Developer/Xcode/DerivedData/F11i-erlvxsqudsegbmckzxfxnvnxxumb/Build/Products/Debug-iphonesimulator/
I see two files with a different name of the project (which I believe was the former name):
F09.app
F09.app.dSYM
but XCODE seems to be looking after
F11i.app
F11i.app.dSYM
Where in XCODE can I set these names so that it compiles again?
(I checked git, and the project file is reverted, there is nothing else that could have affected this)
Many Thanks
If you want to build a pure Unit Test which just tests a Class independently of an application where it is used, you usually don't set a "Unit Test" target. In order to build the Unit Test bundle properly, you need to include the module you want to test, and possibly link to any other framework or library that will be needed. This kind of test will run independently of your app.
If you want to test several aspects of the application, you can set a "Unit Test target". This builds the application executable, and then the test bundle containing only unit test code. During the test, the "test code" will be "injected" into the application executable. Note that this kind of test will start your application.
You can set the "target" of a Unit Test bundle in the "General" tab of the target editor:
Select project in left hand navigation area, select the unit test target in the target editor, select "General" tab, select a target from the popup, or "None".
If you want to inject unit test code into a running application, you possibly need to adjust these settings as well (note: Xcode will setup this as default already):
Open the Scheme Editor: Select Project, then command Product -> Scheme -> "YourProject".
Then, select the "Build" scheme, check whether the corresponding Unit Test target is included in the "Targets" list. Check also that the check mark "Test" is set for the Unit Test's target.
In the target editor, select the Unit Test, select "Build Phases" tab. Check if you have added the Unit Test's target as a "target dependency".

Import XCTest in to existing project

I added a new target as a Cocoa Touch Unit Testing Bundle, named the directory "MyAppTests", and the actual framework is not active-- it is highlighted red
When I add the framework by the 'link binary with libraries' in the 'build phases' tab technique, a new Framework is added, instead of updating the one that was created with adding the target.
When I go to run the code, I then run in to all sorts of troubles, ranging from linking errors to other frameworks not being recognized.
How can I activate the XCTest.framework that was generated when I added target? Please help, thank you!
XCTest.framework is the bundle that Xcode uses to enable
#import <XCTest/XCTest.h>
To add test cases to an existing project select the target, right click > file > new > objective c test case class. Then in your build phases you can add the XCTest.Framework. Make sure you check the add to target boxes when creating the test case.

XCode: Can I delete test target?

What would happen if I delete the test target for my app? Would this affect my other target? How would I go about properly deleting the target?
Select '.xcodeproj' file from project navigator, then select 'Show project and targets list' icon, select 'target' to delete and then click '-' at bottom
If by test target, you mean a unit test target, then deleting the test target would mean you couldn't unit test the code in your project. Deleting the test target wouldn't affect the other targets in your project.
To delete a target select your project from the project navigator to open the project editor. Select the target you want to delete from the left side of the project editor and press the Delete key.

Unit Testing in With A Static Library

I have an XCode workspace with a user interface project (UI) and a core logic project (Core). I want OCUnit unit tests in the UI project, so I have added a new target for testing, as is commonly done.
I am able to run tests just fine until I put in import statements for classes in the main UI target which in turn reference the Core project.
The error I get is "Lexical or Preprocessor Issue 'xxx.h' file not found". I do not get this message when I build the main UI target directly.
It's as if the main UI target knows about Core when it is built, but when it is referenced from the test target it seems to know nothing about Core.
I took the step of adding a reference to the core project using the "Link Binaries with Libraries" The item in the list remains red. A clue? Maybe, but the red reference in the Link list doesn't keep the UI target from building and using core classes. I also made the main target a dependency of the test target.
Make sure you check out the Apple sample code "Unit Tests":
https://developer.apple.com/library/ios/#samplecode/UnitTests/Introduction/Intro.html#//apple_ref/doc/uid/DTS40011742
Make sure your library project is set as a Dependancy in your OCUnit test target's build phases, AND it's linked as a library.
Open up your project in Xcode. In the File menu, go to Project Settings... (or Workspace Settings... if you are using a workspace). Click Advanced... and make sure Unique is checked. Clean and rebuild.
Check your BUILD_PRODUCTS_DIR to see if the headers for your library show up in there. If they don't, first check the build phases in your library target to make sure the headers you need are in the Public section (the Project section may work as well, but try Public and see if that solves your issue).
That covers the most common problems people seem to run into in your situation. When in doubt, check the target settings in the UnitTests sample against yours. Good luck!
In addition to Jon Reid's answer, I had to do the following as well:
In your test target, go to Build Settings. Set "Always Search User Paths" to YES
In your test target, go to Build Settings. Add the path to your static library headers to Header Search Paths.

Resources