将目标导入单元测试与在目标成员中包含该文件有什么区别? [英] What's the difference between importing a target into unit testing and including that file among the target Membership?

查看:21
本文介绍了将目标导入单元测试与在目标成员中包含该文件有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我编写测试时,我可以做到:

When I'm writing tests I can do:

@testable import TestProduct

以上与将测试文件添加到特定目标有何不同?

How is the above different from adding the Test File to a specific Target?

最终我的问题是:如果我将我的 unitTests 作为我所有文件的目标......它是否等同于:

Ultimately my question is: Had I made my unitTests a target to all my files...would it be equivalent to doing:

@testable import TestProduct

如果不是,那有什么区别?

If not, then what is the difference?

推荐答案

来自 Swift 文档,位于单元测试目标的访问级别(已添加重点)

From the Swift docs under the heading Access Levels for Unit Test Targets (emphasis added)

当您编写具有单元测试目标的应用时,您的应用中的代码需要可供该模块使用才能进行测试.默认情况下,只有标记为 open 或 public 的实体才能被其他模块访问.但是,如果您使用@testable 属性标记产品模块的导入声明并在启用测试的情况下编译该产品模块,则单元测试目标可以访问任何内部实体.

When you write an app with a unit test target, the code in your app needs to be made available to that module in order to be tested. By default, only entities marked as open or public are accessible to other modules. However, a unit test target can access any internal entity, if you mark the import declaration for a product module with the @testable attribute and compile that product module with testing enabled.

这些文档说@testable 属性为您的单元测试目标提供对标记为@testable 的模块的所有内部的访问.从而让您不必手动将该模块中的单个文件添加到单元测试目标.

These docs say that the @testable attribute provides your Unit Test target access to all internals of the module marked as @testable. Thus letting you avoid having to manually add individual files in that module to the Unit Test target.

@testable 导入通常是在大型代码库上完成的,这些代码库没有有任何测试......因为这种方法要慢得多而不是将要测试的文件添加到测试目标.

The @testable import is typically done on large codebases that did not have any tests yet... because this approach is significantly slower than adding the files you want to test to the test target.

这篇关于将目标导入单元测试与在目标成员中包含该文件有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆