Search code examples
xcodeswiftmacosswift2osx-elcapitan

How to run xcodebuild test command from swift?


I have an ios project where I have written my ios ui testcases.

Now I have created another osx(swift) command line tool and there from main file, I want to run my ios project ui tests.

For this, I use the below command:

xcodebuild test -project /Users/usernamer/Desktop/Xocde/ios-ui-automation-demo-master/ios-ui-automation-demo.xcodeproj -scheme ios-ui-automation-demo -destination 'platform=iOS Simulator,name=iPad Air'

If I run this command from the terminal, than ios project ui test run properly.

But if I run the command from my osx command line tool swift file(In a new OSX project), this shows an error. The code is

import Foundation

func shell(args: String...) -> Int32 {
let task = NSTask()
task.launchPath = "/usr/bin/xcodebuild"
task.arguments = args
task.launch()
task.waitUntilExit()
return task.terminationStatus
}

print("This is test version edit")

shell("xcodebuild test -project /Users/username/Desktop/Xocde/ios-ui-automation-demo-master/ios-ui-automation-demo.xcodeproj -scheme ios-ui-automation-demo -destination 'platform=iOS Simulator,name=iPad Air'")

This code shows the following error:

    xcodebuild: error: The directory /Users/username/Library/Developer/Xcode/DerivedData/automationProject-brxvyfplrimnekchsaaenmecydkp/Build/Products/Debug does not contain an Xcode project.

 Program ended with exit code: 9

Here automationProject is the osx project name from where I run my main file to run the ios ui test.

Please help me what is my error.


Solution

  • use the task.launchPath as below:

     task.launchPath = "/usr/bin/env"
    

    and finally use the function like below:

    shell("xcodebuild", "test","-project","/Users/username/Desktop/Xocde/ios-ui-automation-demo-master/ios-ui-automation-demo.xcodeproj","-scheme","ios-ui-automation-demo","-destination","platform=iOS Simulator,name=iPad Air")