Disable simulator in SwiftUI unit test

557 Views Asked by At

I am new to SwiftUI and XCode and trying to learn it via a TDD book. So far all the tests are non-UI tests; they're just logic unit tests. Is there any way to disable the simulator when running non-UI unit tests? It's going to run slightly faster without simulator I guess and it's slightly annoying to run an unit test with the simulator popping up every time. I tried to edit my test scheme which only includes the unit tests not the UI tests; still it pops up the simulator every time. I am using XCode 12.5.1.

2

There are 2 best solutions below

5
matt On BEST ANSWER

Is there any way to disable the simulator when running non-UI unit tests?

The way I do that is to put all my business logic code and its unit tests into a framework. Xcode tests a framework without loading the app target, so the Simulator is not involved.

0
Patrick On

This doesn't achieve your goal of "Disable simulator" however if your intention is to not execute any code beyond App then it achieves it.

@main
struct MyApp: App {
    var body: some Scene {
        WindowGroup {
            let isUnitTest = ProcessInfo.processInfo.environment["XCTestBundlePath"] != nil
            if isUnitTest {
                Text("Unit Test")
            } else {
                ContentView()
            }
        }
    }
}