Unit test class reference issue in Objective C - ios

I am working on multi target ios project in objective C. Some swift classes are also there by Bridging-header. There is a Unit test target for main target as Host application. I have to do unit test for another particular target [Say, SecondaryTarget]. I have added the "Unit test target" by Edit Scheme > Test to enable unit test.
I am trying to import a existing class and do the unit test. But I am always getting reference issue [file not found] in test classes, while importing the classes.

I'm pretty sure you can't reuse the same test target against different apps. Instead, create a test target for each app. Share whatever test code you want across each test target.

Related

Why do my Unit tests fail because my project has realm objects?

My iOS app uses Realm. It has Realm subclasses like MyRealmClass.
When I try to run any unit test in my workspace, I get the following error:
"RLMObject subclasses with the same name cannot be included twice in the same target. Please make sure 'MyRealmClass' is only linked once to your current target."
What I tried:
* I tried removing 'MyRealmClass' from the Test target. That does not work. When I do that the code won't compile anymore because the code is dependent on 'MyRealmClass'

Adding unit tests for an existing objective c + swift project

I have an existing iOs project which I would like to add a unit test target to. The classes I'd like to test are both in objective c and swift.
I've managed to create a test target which allows me to test swift only code by adding the implementation swift files to the test target.
However, as soon as I import or use a class that imports objective c code, I run into Linker issues when building the test target:
...
Symbols not found for architecture x86_64
I've tried adding the objectivec mm files to my target which gets my passed the linker error, but I then get an unresolved identifier error for the class I'm importing.
I'm using xcode 9 and swift 3.
edit: I think this may have something to do with the fact the swift bridging header is not available in the test target, however I'm not sure how to add it.
Your test project is a separate target and should have all files it relies upon to be tested linked separately. So first of all click one of the .m files it's missing and check if the test project is also included in the targets. If this is the case there might be a problem with the bridging header your test project uses. Figure out which one it uses from the build settings of your target and see if it includes the same files as your main project.

How to import XCTest suite

I've created my project without XCTest support.
But now after long time development i want to cover my project with unit tests. And i want do it with XCTest.
How to import XCTest suite into existing project previously created without XCTest support ?
You can add new Unit/UI test case classes through "file-> new-> file -> UI test case class or Unit test class". Make sure that the target membership is set for only the test target and not the regular targets (you can see that in the file inspector). If you currently have no test target, you can add that through "file -> new -> target -> iOS UI testing bundle or iOS Unit Testing Bundle" and add relevant files to the this target.
Adding XCTest target to an existing project is simple you can refer
https://automationwithaditya.wordpress.com/2017/08/21/getting-started-to-xctest-automation-framework/
or follow the steps
1. Click on project
2. Add the target choose the one which you want.
Follow this image

How to unit test an app extension on Xcode 6

Does anyone know how to perform unit testing on app extension target, especially keyboard extension target?
What have I tried (in the unit test target):
In the "General" tap, set it's target to the extension target instead of the container app.
Set the "Bundle Loader" to the path of the binary of the extension target, which looks like $(BUILT_PRODUCTS_DIR)/com.mycompany.keyboard.appex/com.mycompany.keyboard
Set the "Test Host" to $(BUNDLE_LOADER).
In the "Build Phases" tap, set the "Target Dependencies" to both the container app and the extension.
After these things done, I can build it successfully but always get "Test Failed" with an log Test target SogouInputTests encountered an error (Test session exited(1). without checking in. If you believe this error represents a bug, please attach the log file at /tmp/TestStatus-UXfvxw.log).
I'm using Xcode 6 beta 3.
I have reported the bug to Apple. And sadly, the answer is that the keyboard extension is not support unit test now. The answer comes from Apple:
It's not currently supported to run unit tests inside an app extension
Instead, factor the code you want to test into a framework and test the code there
Link the framework into your extension
Just ran into similar issues trying to unit test an extension. Independently did exactly the same thing the author tried with Bundle Loader pointing to .appx path with no success of course.
I really did not like the idea of creating a separate framework just for testing so I ended up of adding testable source into the extension test target. It is really simple if you don't have too many source files in your extension:
Select you extension test target in Project settings
Go to Build Phases
Expand Compile Sources
Click +
Add source files with your testable code.
Build for Testing
Why it works:
Once you add extension sources into your extension test target, XCode is going to double reference each of them and compile in both the normal extension build and the test build thus eliminating linking issues.
Are there any drawbacks?
You will have to manually synchronize the source file list in the extension test target. Whenever you add/remove files in the extension target, you may need to do the same in its test target.
What I did which was easier than the other suggestions (no framework creation, no weird settings on build settings, phases, etc) was adding the file that I wanted to test (from the extension target) into the Target Membership of the test target:
The only "drawback" would be that your test target will also include files from your extension (rather than using #testable import like with the main app), but since you are not shipping your test target I would say there is no drawback :)
Tested on Xcode 10.
Note: Only works with Swift code, with ObjC due the BridgingHeader is not possible to follow this approach.

XCTest for existing project

I have a big(about 700 modules) iOS project. Now I need to make unit tests for existing code(before we didn't use it). I've added new XCTest test target for my target and started to write my first test. But after compilation I've got some link errors, because modules from my project weren't be included to test target. Have I include all my modules to test target? Or there is easier way to make test target?
Application files DO NOT need to be included within XCTest targets. Only test files should be included within the 'Compile Sources' list for your XCTest target.
Follow the Apple instructions to add XCTest to your project.
Within the Application target, make sure the compiler option "Symbols hidden by default" is set to NO.
Here is a blog post with screenshots if you get stuck!

Resources