iOS - How can I run unit/ui tests on Release scheme - ios

I wonder if there is a way to run iOS AppTests and AppUITests on Release build. I found SO post here where OP mentioned,
However, the tests cannot run on the release build (because release
doesn't build for testing).
Seems he was unable to run tests in Release build. I am also getting some linking errors in Release build while testing but tests run fine on Debug build.

As mentioned in that answer, the code should be the same in release or debug mode. So unit tests should not require running in a release build, specifically.
For the UI tests, if this means your code has conditional compilation based on Release or Debug target, then you might be forced to test some end-to-end tests?
Option (ref)
Warning: Apple does not recommend doing this in the xcode release notes:
The Enable Testability build setting should be used only in your Debug configuration,
because it prohibits optimizations that depend on not exporting
internal symbols from the app or framework
In the project settings: Build Settings > Enable Testability
This will allow the #testable keyword to import the app in your test code.
Then edit the Test scheme to build the Release target

Related

Jenkins build failed with Xcode if project have test cases

'Module was not compiled for testing' when using #testable does not resolve this post issue.
I am integrating Xcode project with CI/CD by using jenkins and fastlane. My Xcode project build success if project do not have test cases. If I added my test cases then xcode build failed in jenkins with error:
error: module 'JenkinsFastlaneExample' was not compiled for testing
ScreenShot of error
In one post I found answer to set scheme flag, I made same like answer but didn't work for me.
My test cases are also successful in Xcode 11.x , iOS 13.x. tested in iPhone XR
I unable to find out why jenkins build is failed.
Demo Repo (if needed) : https://github.com/thedahiyaboy/ios_jenkins_example
Edit:
I also tried to disable my test cases from Xcode so that I can run my cases from mac system only and with hope jenkins will skip test bundles, but getting the same issue.
In jenkins, configuration was Release (by default) which was unnoticed by me, after replacing it to Debug my build is succeeded.
Reference Image:
Reference Doc:
https://plugins.jenkins.io/xcode-plugin/
Special thanks to Scriptable for giving time and efforts for solving the issue.

Is there any way to auto run all my Unit Tests before archiving release build in iOS

How do I automatically perform unit tests before archiving build in iOS.
You can setup the continuous integration on locally on your computer adding by its steps. There are variety options are available. fastlane or bitrise (I use bitrise to build easy steps). For bitrise, more information could be found this link (https://app.bitrise.io/cli) By doing this you can achieve the steps that you want to do with your build process.

How does Xcode server run tests in release mode without Enable Testability?

When using Xcode server to execute UI Tests, the bot has a setting to 'override' the configuration to Release mode.
This was handy as tests would be executed on release build.
Now when I am trying to run tests on TeamCity or CLI and pass the -configuration Release I get an error saying Module 'App name' was not compiled for testing
This can be solved by toggling Enable testability flag for Release configuration in build settings but that will have to be changed back each time before releasing the app.
Creating another build configuration is also not a desirable option as I have a lot of targets and maintaining an extra config for all would be a pain.
So how does Xcode server do this? Is there a CLI param or something I am missing? Or does it just change the setting via a prebuild script?

Does Test Target required to go on production in iOS

Is it required to take Test case target in iOS application to production or it can be kept locally
No, Test Target does not get included in production builds. it does not even get included in debug builds.
In your build scheme, you can see, Test target is build only for Test.

How to dynamically change target for unit tests in Xcode 7?

I have a project that has multiple different targets/schemes (~38 of them as of writing this question) and I am trying to get unit testing working properly across all of the different targets. I got things working with one target, and tried adding my testing target to all of the different schemes, but it looks like when tests run for each scheme they actually run on the same, original target.
Looking in the project file I see that there's a specific Host Application associated with my testing target, and in the build settings the Bundle Loader and Test Host point to that same Host Application.
Is there any way to override those values for each scheme to run the tests against the current scheme's build target? Or some other way to set up a single test target to run across multiple build targets?
If you run the tests from the command line, or from an CI tool, like Jenkins, you can instruct xcodebuild to use the build settings that you provide. A simple usage example would be:
xcodebuild -scheme SomeScheme test TEST_HOST=target
You can control almost (if not any) build setting from Xcode, like code coverage generation, build directory, derived data directory, code sign identity, etc.
You can select the scheme when you run tests with Xcode server.
Look at WWDC 2014 continues integration talk for a walk through on how to set it up
https://developer.apple.com/videos/play/wwdc2014-415/
It's using Xcode 6 but it's very similar process to Xcode 7
Also check this CI(continues integration) guideline from apple
https://developer.apple.com/library/ios/documentation/IDEs/Conceptual/xcode_guide-continuous_integration/adopt_continuous_integration.html
If anyone is wondering how to do that with UI tests (maybe it is working with unit tests, too), this is what I came up with:
First, we need to build the application which we are going to use to host our UI tests:
xcodebuild -scheme "<appSchemeName>" build -destination "<yourDestination>"
More info about destination parameter: https://mokacoding.com/blog/xcodebuild-destination-options/
Then, we need to run tests on our newly built application:
xcodebuild -scheme "<uiTestsSchemeName>" -destination "<yourDestination>" test TEST_TARGET_NAME="<yourNewlyBuiltAppTargetName>"
Destinations should match, since after the build .app is generated in your DerivedData folder, which will be used for UI tests hosting application

Resources