ADDING TESTS TO PROJECT WITH MODULAR ARCHITECTURE

In my company, we tend to use modular architecture. We separate features in separate projects and have them all open in one workspace. This brings some joys, but on the other hand, it also brings some problems. For instance with adding tests.

As I haven’t found any complex answer for that, I wrote down an easy step-by-step tutorial, which some of you may find useful.

OUR PROJECT STRUCTURE

Since it’s always easier to imagine on direct example, imagine a scenario described in Img. 1. We have one workspace which contains three projects. One for Pods, one for the main app called MyProject with product name MyApp and an embedded framework product from the second project called MyFramework. MyFramework project has two targets, one for the framework itself, another one for tests.

Img. 1: Our project structure

ADDING TEST TARGET

Now imagine a scenario, a test target is not yet added. First tap on the project for which you want to create the test target. Then tap on the little plus icon marked in red in Img. 2.

Img. 2: Adding new target

Now, under the Test section, you will find Unit Testing Bundle(Img. 3). Fill in mandatory information and you will end up with something looking like in Img. 4. Notice how it has no hosting app. And that is the root of the problem. You need a hosting app on which the tests are being executed. Check the dropdown and you will see there are no options. Luckily that is just another peril from Apple, which is easily defeated as in the following step.

Img. 3: New target type
Img. 4: Result after Test bundle has been added

ADDING HOST APP FOR TESTS

Below stated Stackoverflow thread helped me a lot with running tests under my app executable.

Open your test target and navigate to Build Settings. Search for Test Host(Img. 5) and add a value for it in the following form:

$(BUILT_PRODUCTS_DIR)/{YOUR PRODUCT NAME}.app/{YOUR PRODUCT NAME}

So in my case, it is:

$(BUILT_PRODUCTS_DIR)/MyApp.app/MyApp
Img. 5: Setting host

But our work is not done yet. As it is stated in the accepted answer for the thread, you also need to specify from where the bundle for executable will be loaded. Just search for another term Bundle Loader still under test target build settings and set it to

$(TEST_HOST)

as shown in Img. 6.

Img. 6: Setting the bundle of the host app

TELLING THE APP TO RUN YOUR FRAMEWORK TESTS

Now, this is the easiest part. Just edit your app scheme. Under the Test tab and then Info tab, tap plus sign (Img. 7). And then lookup for your test target in the list (Img. 8).

Img. 7: Linking your tests to your app scheme
Img. 8: Adding test target to hosting app

Now you are all set, you can run tests using CMD+U.

Img. 9: Successfully executed default tests

iOS developer at Cleverlance

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store