Running unit test without simulator

Is it possible to run business logic (e.g. non-UI) unit tests without triggering the simulator?
Someone said if I add all the tests into a framework, then it won't trigger the simulator. As a Swift beginner, not sure what that means. A framework in Swift is like a library in Java/.net?

UI tests involve simulators. Unit tests do not. You need to add a unit test target to your project. That's where you put your non-UI tests.

My unit tests do have a test target; when I created a new project in xcode, I choose "include test" option and it automatically created two test targets; one for unit test and one for UI tests. But even when I am running the unit tests, it still loads the simulator.

Whatever kind of target you have, running tests on iOS, unit or UI, necessarily involves running the simulator. If you want to make your framework cross platform and support macOS, you can run on that platform instead, but that's usually not a viable approach to the problem.

3 Likes

Thanks for the reply.
Two questions:

  1. how to make it work? e.g. how to run framework tests without the simulator? I tried to create a framework and run its tests; but it still triggered simulator.
  2. You mentioned this usually is not a viable approach, why?

You mentioned this usually is not a viable approach, why?

Because all code is built for a platform, with macOS, iOS, and the iOS simulator being different platforms. The only way to run iOS code on the Mac [1] is to build it for the iOS simulator platform, and the iOS simulator platform can only run within a simulator.

The easiest fix here is to do what Jon_Shier suggested, that is, allow your framework to build for the Mac. That’s usually pretty straightforward for non-UI frameworks.

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple

[1] Well, that’s not quite true. Apple silicon Macs can run iOS apps via iOS Apps on Mac, but this isn’t the direction you want to go.

The easiest fix here is to do what Jon_Shier suggested, that is, allow your framework to build for the Mac. That’s usually pretty straightforward for non-UI frameworks.

If I'm building for Mac, shouldn't I be able to run logic tests without a host ( mac) app even without a framework and just including the source files in the test target?
I

That’s exactly what happens with you run ‘swift test’ on a library package - the swift testing framework provides code for a simple process that hosts the tests and invokes them. There’s still a process there, even if it’s not an app.

I’m not 100% confident, but i believe this is functionally the same pattern, but with a different code path, that Xcode uses when you run tests from an App and specify hosting app: None in the configuration of the test target.

In any case, there’s always a test process - it’s either provided by you in the form of your app, or it’s provided by the testing framework.