run test classes using xctest framework from a class - ios

i am facing a problem. I want to do a ui test for my application.
i have written six test class where my test cases are included.
I can run my test cases using
"command U" or the clicking in the run button.
But i don't want to do like this.
I want to run my test classes using code i mean using
xctest because after complete my execution, i have to do another task.
can anyone please give an example how i can run my test classes using xctest codding from a class file.
i dont want to use shell script or jenkins, i want it by using xctest framework.

Running tests with xcodebuild
The xcodebuild command-line tool drives tests just like the Xcode IDE. Run xcodebuild with the action test and specify different destinations with the -destination argument. For example, to test MyApp on the local OS X “My Mac 64 Bit,” specify that destination and architecture with this command:
> xcodebuild test -project MyAppProject.xcodeproj -scheme MyApp -destination 'platform=OS X,arch=x86_64'
If you have development-enabled devices plugged in, you can call them out by name or id. For example, if you have an iPod touch named “Development iPod touch” connected that you want to test your code on, you use the following command:
> xcodebuild test -project MyAppProject.xcodeproj -scheme MyApp -destination 'platform=iOS,name=Development iPod touch'
Tests can run in Simulator, too. Use the simulator to target different form factors, operating systems, and OS versions easily. Simulator destinations can be specified by name or id. For example:
> xcodebuild test -project MyAppProject.xcodeproj -scheme MyApp -destination 'platform=Simulator,name=iPhone,OS=8.1'
Source

Edit your Xcode scheme.
Click the disclosure triangle next to "Test" to reveal more options.
Select "Post-actions"
Click '+' to add any actions to run after tests are complete.
As you can see, your choices are to run a script, or send a simple email. For more information on configuring actions, see https://michele.io/the-project-file-part-2/

Related

Can I run several iOS UI Tests in Parallel?

I'm working on building a UI Test suite for my iOS app. I need to test my app's functionality on several different devices, but right now I have to select the simulator I want, run the tests, and then repeat.
Now that Xcode supports multiple simulators running in parallel, is there a way to run the UI tests across several different device simulators at the same time?
Run the following command in the same directory as your project to run your tests in parallel from the command line:
xcodebuild test -scheme "YourSchemeName" -destination 'platform=iOS Simulator,OS=11.2,name=iPhone 8' -destination 'platform=iOS Simulator,OS=11.2,name=iPhone 6s' -configuration "Debug" ENABLE_TESTABILITY=YES SWIFT_VERSION=4.0 ONLY_ACTIVE_ARCH=YES
You can add -destination 'platform=iOS Simulator,OS=11.2,name=iPhone 8' for a different destination for as many different destinations as you would like.
For a list of simulator names and OSs that are available, run the command:
instruments -s devices
Bear in mind that if you are running tests in the simulator, you will not see the simulators on your screen when running tests through the command line.
In Xcode:
Select your target scheme in Xcode, and "Edit Scheme..."
Find the settings for "Test", and press on the "Info" tab
You'll see a list of your Unit and UI tests, press on the associated
"Options..." button
Select "Execute in parallel on Simulator"
Optionally select "Randomize execution order"
Commandline:
Look at this answer here

Is there a way to run XCTest(UI) against an archived build(.ipa)?

I am investigating how to use XCTest to close our automation test gap.
From the developer document, I only see something like this:
xcodebuild test -project MyAppProject.xcodeproj -scheme MyApp
https://developer.apple.com/library/tvos/documentation/DeveloperTools/Conceptual/testing_with_xcode/chapters/08-automation.html#//apple_ref/doc/uid/TP40014132-CH7-SW1
I would like to know if there is a way to run XCTest(UI test) against an archived build (.ipa)?
or
Can we just seperate the build and test so that we can build first and test against that build later?
Thanks
If you want to separate the build and test stages, you can use the xcodebuild flags build-for-testing (which builds a xctestrun file) and test-without-building (which runs it). You may want to build-without-testing to a generic device if you want the build-for-testing to be used on multiple devices.
Check out https://medium.com/xcblog/speed-up-ios-ci-using-test-without-building-xctestrun-and-fastlane-a982b0060676 for more details

Can you run a specific Xcuitest from the command line?

Playing with iOS 9.0, Xcode GM and the new Xcuitesting framework. How can I run a specific test from the command line instead of having to click play in the ide? I know there's a shortcut to run all the tests but I want to run a single test.
Thanks!
You should be able to do that with xctool command line tool for running specific test class,
xctool -workspace YourWorkspace.xcworkspace -scheme YourScheme test -only myUITestTarget:UITestClass
You can also use xcodebuild to run the entire test suite as explained here,
https://krausefx.com/blog/run-xcode-7-ui-tests-from-the-command-line

Relaunch iPhone Simulator for each test from Suite

Is it possible to configure Xcode in such a way so when I run all of my test each one will be performed on a "new iPhone Simulator". I'm testing UI related stuff and I want each test to start on a newly opened application.
Currently, application is starting only once and all of the tests are starting one after another and sometimes the ending state after one test is not good for the next test to start.
Solved
I used XCTool (https://github.com/facebook/xctool)
I was able to run one particulatr test by invoking command:
xctool -workspace WorkspaceName.xcworkspace -scheme Workspace -sdk iphonesimulator run-tests -only TestTarget:TestClass/test_Some_Method
Then I simply wrote a script to run all of the tests I had. This way every test triggers simulator to run application once again from scratch.
You could, before the next test, delete the app as you would do it in a real iPhone.

Don't launch simulator when running unittests

Some background:
I have iOS application with a target configured to run unitTests. And I am running build automation tool jenkins on my MacBook which automatically builds this application and run all tests (using command line xcodebuild tool).
Everything worked fine with Xcode 4. This build automation tool was running under different user and was running all these tests.
I switched to Xcode 5 recently and it started to fail, because it can't launch Simulator.
The problem
I have a scheme UnitTests which is configured to run tests (logic tests). A I run these test using one of two methods:
Command U in Xcode
Or command line "/usr/bin/xcodebuild -scheme UnitTests -sdk iphonesimulator -configuration Release clean build TEST_AFTER_BUILD=YES "
In both cases, it tries to start simulator. However, per my understand it doesn't need it. Anyway it runs on top x86 and it doesn't look like any apps are installed on Simulator.
Is there a way to get rid of this pesky simulator start (because it breaks my build automation)?
Update 1
Seems to find very similar question, but can't get it working:
Run logic tests in Xcode 4 without launching the simulator
Update 2
I found VERY relevant and interesting question/answer: Apple CI / Xcode Service and Jenkins
Using xCode 7 and xCtool.
xctool is capable of executing unit tests without the simulator.
To get this working,
1 . Update target settings to run without a host app.
Select your project --> then test target --> Set the host application to none.
2. Install xctool , if you don't have it.
brew install xctool
3. Run the tests using terminal with xctool.
xctool -workspace yourWorkspace.xcworkspace -scheme yourScheme run-tests -sdk iphonesimulator
You can create a Mac OSX Unit Test instead of an iOS unit test. This requires that you not include any iOS specific libraries in the unit tests though. You can do this via the following:
Select the project -> the target drop down -> "Add Target..."
Select "Mac OSX" -> "Other" -> "Cocoa Unit Testing Bundle"
Create the testing bundle as you would a normal project
You can now add sources to the unit test and run it like an iOS test without launching the simulator.
I've asked the same question to apple engineers. Unfortunately it doesn't seem you can accomplish this and stay with iOS at the same time. There are some tricks you can do to check if testing. You could put this code snippet in your AppDelegate.h or some other global class to say not load a root viewcontroller and prevent any wierdo ui stuff from corrupting your unit tests:
static BOOL isTesting() {
BOOL isTesting = !isEmpty([[[NSProcessInfo processInfo] environment] objectForKey:#"XCInjectBundle"]);
return isTesting;
}
I've also had an apple engineer verify this is a legitimate check. And to give credit where credit is due, this is from: Programmatically determine current target (run or test) in iOS project
EDIT:
I've also had success with this and it's a little more straight forward:
static BOOL isTesting() {
return [[[NSProcessInfo processInfo] processName] isEqualToString:#"xctest"];
}
A osx test target can become a huge hassle because you have to manage yourself which source file to include. Putting #testable import YourAppName on top of your XCTest files is way more convenient. So just prevent your app from launching in case of a XCTest run.
In your AppDelegate put: (Swift 3 solution)
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
{
if ProcessInfo.processInfo.environment["XCInjectBundleInto"] != nil {
return false
}
...
This wont prevent the simulator from launching, but will save you a lot of time.
Workaround:
App will still launch but you can #if to define what you don't want to run.
Approach:
Create a custom build configuration called Test by duplicating Debug (Project > Info > create new configuration)
In Build Settings > Active Compilation Conditions for Test add TESTING
Edit Scheme > Info > Build Configuration, set build configuration as Test
Use #if !TESTING #endif around the code you don't want to execute when testing.
Frameworks:
If you have embedded frameworks, create the same build configuration in the framework, so that the framework binary is properly linked.
You can run unit and ui tests in headless mode with Xcode 9 and a command from your terminal. For reference sample commands:
Building and testing a workspace
xcrun xcodebuild -workspace "YOUR_WORKSPACE_NAME.xcworkspace" -scheme "YOUR_SCHEME" -sdk "iphonesimulator12.0" -destination "OS=12.0,name=iPhone X" -configuration Debug -enableCodeCoverage YES clean build test
For project
xcrun xcodebuild -project "YOUR_PROJECT_NAME.xcodeproj" -scheme "YOUR_SCHEME" -sdk "iphonesimulator12.0" -destination "OS=12.0,name=iPhone X" -configuration Debug -enableCodeCoverage YES clean build test

Resources