简体   繁体   中英

Shared unit tests for new multiplatform template in Xcode 12

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.

Required

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

代码

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:

模板

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:

设置

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"

在此处输入图像描述

If you add that and name it "Tests Shared"

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM