Shared unit tests for new multiplatform template in Xcode 12

Viewed 604

How do you test the shared portion of code when you have a multiplatform project? I see tests for iOS and tests for macOS, but nothing for the shared pieces. I want to add unit tests for the platform-independent portion of my app.

2 Answers

Required

The most important part is to import the module to the test target:

Code

The @testable word means that this test can access the internal stuff of the imported module.

Also, note that the MultiplatformAPP is the name of my project. You should look for the name of your project, instead.


✅ Now you can access to the shared section and test it as you like. But there are some extra optional works you can do:


Optionally

  1. You can build a custom bundle for your shared test:

Template

Note that is doesn't matter what template group you are choosing this template from. It's a template! you can change it to anything else anytime. So we started with tvOS ‍♂️

  1. Then you can nake it independent to any of the targets or depends on any of them you prefer to test with:

Settings

Note that I have named it Tests shared to match the pattern of the default tests naming convention of the multiplatform app.

Think we managed to figure this out.

If you go to File > New > Target ...

In the Multiplatform section there isn't many options, but in iOS there is a Testing bundle named "Unit Testing Bundle"

enter image description here

If you add that and name it "Tests Shared"

You can then unit test against files that are part of the Shared target

Related