Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode / iOS: Unit Tests, Schemes, and Configurations

Tags:

xcode

ios

My iOS project has five schemes: Local Development, Integration, QA, Demo, and Production. Each scheme uses a differing configuration to control things like network poll frequency, API endpoints, analytics, and so on.

Similarly, we have five corresponding targets: Local Development, Integration, QA, Demo, and Production. Each target has several User-Defined Build Settings, which contain API keys, numeric values for timing, etc.

Our application's Info.plist file uses application variables such as ${SOME_ENDPOINT_URL} to draw in the corresponding User-Defined Build Settings.

To retrieve the variables, I do something like the following:

[[[NSBundle mainBundle] infoDictionary] valueForKey:@"Some Endpoint URL"]

That would correspond to the User-Defined Build Setting, like this:

"Some Endpoint URL" = ${SOME_ENDPOINT_URL}

I am now looking at how to configure the project appropriately to perform unit and logic tests.

To build out the tests to determine if the environments are configured correctly, I'm not certain what the best practice is.

  • Is it correct to build out five additional Test-specific targets for each environment?
  • Or is it better to override the "Run action's arguments" setting for the test component for each scheme, and provide something like an argument to specify which scheme we are looking at?
  • Are there any existing references for configuring unit and logic tests for iOS projects with multiple environments? This project's complexity seems to exceed the scope of most documentation.
like image 633
fdsaas Avatar asked Mar 06 '13 19:03

fdsaas


People also ask

What are Xcode test plans?

And for that, we're introducing a new feature in Xcode 11 called test plans. So test plans allows running your test more than once with different settings. Using a test plan, you can define all your testing variance in one place. And then, you can share that between multiple schemes.

How do I write unit tests in Xcode?

Adding a unit test in Xcode These include both Unit Tests and UI Tests. If you already have a project, you can add a Unit Testing Bundle to it as well. Go to File > New > Target. Select iOS Unit Testing Bundle and then click Next.

What should I unit test iOS?

A unit test is a function you write that tests something about your app. A good unit test is small. It tests just one thing in isolation. For example, if your app adds up the total amount of time your user spent doing something, you might write a test to check if this total is correct.


1 Answers

The following are what I do.

Info.plist

  • Create a master Info.plist file
  • Write a run script (Shell Script) for each scheme to generate a environment-specific Info.plist by modifying settings in the master Info.plist (Use PlistBuddy -c)
  • Add the run script to Build Phases (above "Compile Sources")

.h file

  • Define configuration settings in a .h file (e.g. config.h)

    #if defined (CONFIG_FILE) 
    #import CONFIG_FILE
    #endif
    
  • Import config.h in your code

  • Use pre-processor macros for each scheme to select the target .h file.

    -DCONFIG_FILE=local-env-config.h
    
like image 103
Pattrawoot S. Avatar answered Nov 15 '22 11:11

Pattrawoot S.