Jonathan Chen
Jonathan Chen

Reputation: 716

XCUITests - Passing in environment variables via command line

I'm attempting to pass a value to my XCUITest via command line. I'm aware that it's possible to do this for an XCTest (non-UI) in the following way:

xcodebuild -verbose test -workspace MyWorkspace.xcworkspace -derivedDataPath derivedData -scheme "MyScheme" -configuration Debug SYMROOT="$(pwd)"/build -destination platform="iOS Simulator",name="iPad Air",OS=10.2 -only-testing:UITests/UITests -resultBundlePath logfiles MY_SETTING="setting_value" MY_OTHER_SETTING="setting_value_2"

This, however, doesn't seem to work in my XCUITest, as the code does not enter the #ifdef MY_SETTING block. Is there another way I can pass values to my XCUITest via command line, or is it not possible?

Upvotes: 3

Views: 7915

Answers (4)

Jonathan Chen
Jonathan Chen

Reputation: 716

Thanks for your answers below, I found this answer which eventually worked for me. The great people at Applitools shed some light on this issue for me, detailed steps are as follows:

The most important step is to make Xcode know about the variables we are going to pass from command line.

Lets call our variable TEST_VAR. To do this follow these steps please:

1) Open to Xcode, select TEST TARGET(not target of application) https://www.evernote.com/l/AR0BF8Te8_hFGYON8jKqIkv3grnwFky16tc

2) Select “Build Settings” and add in the “Preprocessor Macros” section TEST_VAR=$(TEST_VAR)

https://www.evernote.com/l/AR1b4QRysBlDRro3sbKH1hSy6s_2s_Qkxnw

3) Add new user-define setting by tapping “+” button and selecting “Add User-Defined Setting” https://www.evernote.com/l/AR0qflFqN2lIEoKVVzUUYRArM49Qb1d2TUE

4) Set a default value for your new custom setting https://www.evernote.com/l/AR32JBfPDhFIN76ZlJjq1eV7v8bry9WImfE

Now variable TEST_VAR is ready to use.

This is an example how I pass it to the test. My test project is inside a workspace. So I open Terminal and go to the destination folder of my workspace.

xcodebuild -workspace workspace_name\ workspace.xcworkspace/ -scheme YourSchemeName -sdk iphonesimulator -destination 'platform=iOS Simulator,name=iPhone 6,OS=10.2' -only-testing:TestTargetName/TestCaseName/testName TEST_VAR=1988 test

Inside the test I can get the value of TEST_VAR variable somehow like this: NSLog(@"TEST_VAR %li", (long)TEST_VAR);

However, I also needed to define another preprocessor macro to unwrap the string value of the value I passed in, as described here: Accessing the value of a Preprocessor Macro definition.

So to retrieve the string value, I did something like:

NSLog(@"TEST_VAR %s", MACRO_VALUE(TEST_VAR))

Upvotes: 5

Maetschl
Maetschl

Reputation: 1339

If you need pass the environment variables from your schema to XCUITes, modify the XCTestCase -> app.launchEnvironment object, on each test class of this way:

Swift 3

override func setUp(){
    app.launchEnvironment = ProcessInfo.processInfo.environment
}

Upvotes: 5

atgrubb
atgrubb

Reputation: 1203

This can be done, yes, you are missing two steps though.

  1. Set up your Scheme to have an environment variable that has MY_SETTING set as it's value.
  2. if let mySetting = ProcessInfo.processInfo.environment['MY_SETTING'] { // do whatever you want because it is defined }

Kind of a dupe of Accessing user defined variables passed in from xcodebuild command line

Upvotes: 2

tedrothrock
tedrothrock

Reputation: 368

There is no native method for passing values to UI tests via xcodebuild. You could use a perl one-liner to replace a value in your project before running UI tests:

perl -pi -e 's/valueToReplace/replacementValue/g' <path to file to modify>

I've used this method in the past to replace the base URL in a project in order to run UI tests against a mock server.

Upvotes: 0

Related Questions