简体   繁体   English

Xcode 12 中新多平台模板的共享单元测试

[英]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.我看到了 iOS 的测试和 macOS 的测试,但没有看到共享的部分。 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. @testable这个词意味着这个测试可以访问导入模块的内部内容。

Also, note that the MultiplatformAPP is the name of my project.另外,请注意MultiplatformAPP是我的项目的名称。 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 ♂️所以我们从 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.请注意,我已将其命名为Tests shared以匹配多平台应用程序的默认测试命名约定的模式。

Think we managed to figure this out.认为我们设法解决了这个问题。

If you go to File > New > Target...如果你 go 到 File > New > Target...

In the Multiplatform section there isn't many options, but in iOS there is a Testing bundle named "Unit Testing Bundle"在 Multiplatform 部分没有太多选项,但在 iOS 中有一个名为“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然后,您可以对属于 Shared 目标的文件进行单元测试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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