How to let the app know if it's running Unit tests in a pure Swift project?

IosSwiftXcodeXctest

Ios Problem Overview


One annoying thing when running tests in Xcode 6.1 is that the entire app has to run and launch its storyboard and root view controller. In my app this runs some server calls that fetches API data. However, I don't want the app to do this when running its tests.

With preprocessor macros gone, what's the best for my project to be aware that it was launched running tests and not an ordinary launch? I run them normally with command + U and on a bot.

Pseudocode:

// Appdelegate.swift
if runningTests() {
   return
} else {
   // do ordinary api calls
}

Ios Solutions


Solution 1 - Ios

Elvind's answer isn't bad if you want to have what used to be called pure "Logic Tests". If you'd still like to run your containing host application yet conditionally execute or not execute code depending on whether tests are run, you can use the following to detect if a test bundle has been injected:

if NSProcessInfo.processInfo().environment["XCTestConfigurationFilePath"] != nil {
     // Code only executes when tests are running
}

I used a conditional compilation flag as described in this answer so that the runtime cost is only incurred in debug builds:

#if DEBUG
    if NSProcessInfo.processInfo().environment["XCTestConfigurationFilePath"] != nil {
        // Code only executes when tests are running
    }
#endif
Edit Swift 3.0
if ProcessInfo.processInfo.environment["XCTestConfigurationFilePath"] != nil {
    // Code only executes when tests are running
}

Solution 2 - Ios

I use this in application:didFinishLaunchingWithOptions:

// Return if this is a unit test
if let _ = NSClassFromString("XCTest") {
	return true
}

Solution 3 - Ios

Instead of checking if the tests are running to avoid side-effects, you could run the tests without the host app itself. Go to Project Settings -> select the test target -> General -> Testing -> Host Application -> select 'None'. Just remember to include all files you need to run the tests, as well as libraries normally included by the Host app target.

enter image description here

Solution 4 - Ios

Other, in my opinion simpler way:

You edit your scheme to pass a boolean value as launch argument to your app. Like this:

Set launch arguments in Xcode

All launch arguments are automatically added to your NSUserDefaults.

You can now get the BOOL like:

BOOL test = [[NSUserDefaults standardUserDefaults] boolForKey:@"isTest"];

Solution 5 - Ios

var isRunningTests: Bool {
	return ProcessInfo.processInfo.environment["XCTestConfigurationFilePath"] != nil
}

Usage

if isRunningTests {
	return "lena.bmp"
}
return "facebook_profile_photo.bmp"

Solution 6 - Ios

I believe it's completely legitimate to want to know if you're running inside a test or not. There are numerous reasons why that can be helpful. For example, in running tests, I return early from application-did/will-finish-launching methods in the App Delegate, making the tests start faster for code not germane to my unit test. Yet, I can't go pure "logic" test, for a host of other reasons.

I used to use the excellent technique described by @Michael McGuire above. However, I noticed that stopped working for me around Xcode 6.4/iOS8.4.1 (perhaps it broke sooner).

Namely, I don't see the XCInjectBundle anymore when running a test inside a test target for a framework of mine. That is, I'm running inside a test target that tests a framework.

So, utilizing the approach @Fogmeister suggests, each of my test schemes now sets an environment variable that I can check for.

enter image description here

Then, here's some code I have on a class called APPSTargetConfiguration that can answer this simple question for me.

static NSNumber *__isRunningTests;

+ (BOOL)isRunningTests;
{
    if (!__isRunningTests) {
        NSDictionary *environment = [[NSProcessInfo processInfo] environment];
        NSString *isRunningTestsValue = environment[@"APPS_IS_RUNNING_TEST"];
        __isRunningTests = @([isRunningTestsValue isEqualToString:@"YES"]);
    }

    return [__isRunningTests boolValue];
}

The one caveat with this approach is that if you run a test from your main app scheme, as XCTest will let you do, (that is, not selecting one of your test schemes), you won't get this environment variable set.

Solution 7 - Ios

UPDATE 2022

/// Returns true if the thread is running an `XCTest`.
var isRunningXCTest: Bool {
  threadDictionary.allKeys
    .contains {
      ($0 as? String)?
        .range(of: "XCTest", options: .caseInsensitive) != nil
    }
  }

ORIGINAL ANSWER

This is the swift way to do it.

extension Thread {
  var isRunningXCTest: Bool {
    for key in self.threadDictionary.allKeys {
      guard let keyAsString = key as? String else {
        continue
      }
    
      if keyAsString.split(separator: ".").contains("xctest") {
        return true
      }
    }
    return false
  }
}

And this is how you use it:

if Thread.current.isRunningXCTest {
  // test code goes here
} else {
  // other code goes here
}

Here is the full article: https://medium.com/@theinkedengineer/check-if-app-is-running-unit-tests-the-swift-way-b51fbfd07989

Solution 8 - Ios

Combined approach of @Jessy and @Michael McGuire

(As accepted answer will not help you while developing a framework)

So here is the code:

#if DEBUG
        if (NSClassFromString(@"XCTest") == nil) {
            // Your code that shouldn't run under tests
        }
#else
        // unconditional Release version
#endif

Solution 9 - Ios

You can use now

if ProcessInfo.processInfo.environment["XCTestConfigurationFilePath"] == nil {
            // Code only executes when tests are not running
}

The option is deprecated in Xcode 12.5

First add variable for testing:

enter image description here

and use that in your code:

 if ProcessInfo.processInfo.environment["IS_UNIT_TESTING"] == "1" {
                 // Code only executes when tests are running
 } 

Solution 10 - Ios

Here's a way I've been using in Swift 4 / Xcode 9 for our unit tests. It's based on Jesse's answer.

It's not easy to prevent the storyboard being loaded at all, but if you add this at the beginning of didFinishedLaunching then it makes it very clear to your developers what is going on:

func application(_ application: UIApplication,
                 didFinishLaunchingWithOptions launchOptions:
                 [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    #if DEBUG
    if let _ = NSClassFromString("XCTest") {
        // If we're running tests, don't launch the main storyboard as
        // it's confusing if that is running fetching content whilst the
        // tests are also doing so.
        let viewController = UIViewController()
        let label = UILabel()
        label.text = "Running tests..."
        label.frame = viewController.view.frame
        label.textAlignment = .center
        label.textColor = .white
        viewController.view.addSubview(label)
        self.window!.rootViewController = viewController
        return true
    }
    #endif

(you obviously shouldn't do anything like this for UI tests where you do want the app to startup as normal!)

Solution 11 - Ios

You can pass runtime arguments into the app depending on the scheme here...

enter image description here

But I'd question whether or not it is actually needed.

Solution 12 - Ios

The method I had been using stopped working in Xcode 12 beta 1. After trying all of the build based answers to this question, I was inspired by @ODB's answer. Here is a Swift version of a fairly simple solution that works for both Real Devices and Simulators. It should also be fairly "release proof".

Insert in Test setup:

let app = XCUIApplication()
app.launchEnvironment.updateValue("YES", forKey: "UITesting")
app.launch()

Insert in App:

let isTesting: Bool = (ProcessInfo.processInfo.environment["UITesting"] == "YES")

To use it:

    if isTesting {
        // Only if testing
    } else {
        // Only if not testing
    }

Solution 13 - Ios

Some of these approaches don't work with UITests and if you're basically testing with the app code itself (rather than adding specific code into a UITest target).

I ended up setting an environment variable in the test's setUp method:

XCUIApplication *testApp = [[XCUIApplication alloc] init];

// set launch environment variables
NSDictionary *customEnv = [[NSMutableDictionary alloc] init];
[customEnv setValue:@"YES" forKey:@"APPS_IS_RUNNING_TEST"];
testApp.launchEnvironment = customEnv;
[testApp launch];

Note that this is safe for my testing since I don't currently use any other launchEnvironment values; if you do, you would of course want to copy any existing values first.

Then in my app code, I look for this environment variable if/when I want to exclude some functionality during a test:

BOOL testing = false;
...
if (! testing) {
    NSDictionary *environment = [[NSProcessInfo processInfo] environment];
    NSString *isRunningTestsValue = environment[@"APPS_IS_RUNNING_TEST"];
    testing = [isRunningTestsValue isEqualToString:@"YES"];
}

Note - thanks for RishiG's comment that gave me this idea; I just expanded that to an example.

Solution 14 - Ios

Apparently in Xcode12 we need to search in the environment key XCTestBundlePath instead of XCTestConfigurationFilePath if you are using the new XCTestPlan

Solution 15 - Ios

I'm using this in my SwiftUI Scene.body (Xcode 12.5):

if UserDefaults.standard.value(forKey: "XCTIDEConnectionTimeout") == nil {
  // not unit testing
} else {
  // unit testing
}

Solution 16 - Ios

Worked for me:

Objective-C

[[NSProcessInfo processInfo].environment[@"DYLD_INSERT_LIBRARIES"] containsString:@"libXCTTargetBootstrapInject"]

Swift: ProcessInfo.processInfo.environment["DYLD_INSERT_LIBRARIES"]?.contains("libXCTTargetBootstrapInject") ?? false

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbogenView Question on Stackoverflow
Solution 1 - IosMichael McGuireView Answer on Stackoverflow
Solution 2 - IosJesseView Answer on Stackoverflow
Solution 3 - IosEivind Rannem BøhlerView Answer on Stackoverflow
Solution 4 - IosiCarambaView Answer on Stackoverflow
Solution 5 - IosneoneyeView Answer on Stackoverflow
Solution 6 - IosidStarView Answer on Stackoverflow
Solution 7 - IosFiras SafaView Answer on Stackoverflow
Solution 8 - Iosm8labsView Answer on Stackoverflow
Solution 9 - Ioszdravko zdravkinView Answer on Stackoverflow
Solution 10 - IosJosephHView Answer on Stackoverflow
Solution 11 - IosFogmeisterView Answer on Stackoverflow
Solution 12 - IosChuck HView Answer on Stackoverflow
Solution 13 - IosODBView Answer on Stackoverflow
Solution 14 - IosMZapataeView Answer on Stackoverflow
Solution 15 - IosMatthewView Answer on Stackoverflow
Solution 16 - Iosuser3761851View Answer on Stackoverflow