bogen
bogen

Reputation: 10432

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

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
}

Upvotes: 133

Views: 57972

Answers (18)

Pace
Pace

Reputation: 1

Why not check processName that seems to always be xctest when running unit-tests.

var isRunningUnitTests: Bool {
    ProcessInfo.processInfo.processName == "xctest"
}

Upvotes: 0

jjrscott
jjrscott

Reputation: 1542

As of Xcode 13.4.1 and 14.0b6 the difference between running and testing is:

XCInjectBundleInto = unused
XCTestBundlePath = PlugIns/«Name of Your App»Tests.xctest
XCTestConfigurationFilePath = 
XCTestSessionIdentifier = 3B127C55-13E6-4F13-84BD-53FF9FBBC8B2

Obviously it's no guarantee of stability but being consistent across two major versions is pretty good.

Upvotes: 0

TheInkedEngineer
TheInkedEngineer

Reputation: 241

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

Upvotes: 14

zdravko zdravkin
zdravko zdravkin

Reputation: 2386

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
 } 

Upvotes: 15

anon
anon

Reputation:

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
}

Upvotes: 2

MZapatae
MZapatae

Reputation: 29

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

Upvotes: 2

Chuck H
Chuck H

Reputation: 8276

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
    }

Upvotes: 5

user3761851
user3761851

Reputation: 33

Worked for me:

Objective-C

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

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

Upvotes: 0

ODB
ODB

Reputation: 386

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.

Upvotes: 2

JosephH
JosephH

Reputation: 37505

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!)

Upvotes: 5

neoneye
neoneye

Reputation: 52221

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

Usage

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

Upvotes: 32

iCaramba
iCaramba

Reputation: 2640

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"];

Upvotes: 50

Michael McGuire
Michael McGuire

Reputation: 3910

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
}

Upvotes: 128

m8labs
m8labs

Reputation: 3721

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

Upvotes: 13

Jesse
Jesse

Reputation: 1697

I use this in application:didFinishLaunchingWithOptions:

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

Upvotes: 60

idStar
idStar

Reputation: 10804

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.

Upvotes: 24

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

Upvotes: 51

Fogmeister
Fogmeister

Reputation: 77651

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.

Upvotes: 3

Related Questions