10

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.

PointOfNilReturn
  • 416
  • 4
  • 13
  • 1
    Hey @PointOfNilReturn curious to learn if you had any success with this? We are currently having the same problem in both beta 4 and beta 5. – kelvin Aug 19 '20 at 16:24

2 Answers2

8

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.

neoneye
  • 50,398
  • 25
  • 166
  • 151
Mojtaba Hosseini
  • 95,414
  • 31
  • 268
  • 278
  • I tried what you suggested with the new target. I had been using testable already. I keep getting an "Undefined symbol:" error. Do I need to select the new test target as a target in the files I wish to test?? – Harry Day May 17 '21 at 16:20
3

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

Matthew Wilson
  • 2,045
  • 14
  • 27