XCTest的@testable幕后发生了什么? [英] What's happening behind the scenes in XCTest's @testable?

查看:116
本文介绍了XCTest的@testable幕后发生了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道

@testable import MyModule

具有从测试"(由"testTarget"构建)模块 MyModuleTests 探究 MyModule 的非公开成员的能力.

gives ability to explore non-public members of MyModule from a "test" (built with "testTarget") module MyModuleTests.

我的非测试"模块中需要相同的功能.不在生产中,仅在调试模式下.

I need the same functionality in my "non-test" module. Not in production, just in debug mode.

我的问题是:你知道怎么做吗?

My question is: do you know how to do this?

与之相关的(我认为,这是一个比较棘手的问题): @testable 背后实际上发生了什么魔术?

And related (I think, harder question): what magic is actually happening behind @testable?

推荐答案

要回答您的问题,出于调试目的,您可以实际使用它.假设您有一个工作区 MyAwesomeWkspace 和一个在 MyAwesomeProject 中的项目.

To answer your question, for debugging purposes, you can actually use this. Let's say you have a workspace MyAwesomeWkspace and a project inside MyAwesomeProject.

现在,创建一个名为 MyAwesomeModule 的新 framework 又名 module .在该模块内部,创建一个名为 Person 的非公共类.

Now, create a new framework aka module called MyAwesomeModule. Inside that module create a non-public class called Person.

如果您尝试通过执行 import MyAwesomeModule 然后在 let p = Person(),您将遇到一个错误.

If you try to use the class Person inside MyAwesomeProject by doing import MyAwesomeModule and then something like let p = Person() you will have an error.

但是,如果您执行 @testable import MyAwesomeModule ,魔术就发生了,您现在可以使用该类了.

But if you do @testable import MyAwesomeModule, the magic happens and you can now use the class.

基本上 @testable 允许您测试未声明为公共的内容.注释仅适用于 import ,因为您可以看到

Basically @testable allows you to test things that you didn't declare public. The annotation only works with import as you can see it here.

因此,为了工作,目标通过 -enable-testing 进行编译,以便您可以访问非公共成员.至少基于此处

So in order to work, the target is compiled with -enable-testing so that you can have access to non-public members. At least based on what's here

因为默认情况下,debug 构建配置是使用 -enable-testing 编译的,所以我向您展示的示例将起作用.但是,如果将构建配置更改为 release ,则会看到一条错误消息,指出自 release 配置以来,未编译 Module ..进行测试.不是用该标志构建的.

Because, by default, the debug build configuration is compiled with -enable-testing, the example I showed you will work. But if you change the build config to release, you'll see an error saying Module .. was not compiled for testing since the release config is not built with the flag.

Swift访问控制模型,如访问控制中所述Swift编程语言(Swift 4)部分,外部实体访问应用程序中声明为内部的任何内容或框架.默认情况下,要能够从您的网站访问这些项目测试代码,则需要至少将其访问级别提升到公开,从而降低了Swift的类型安全性带来的好处.

The Swift access control model, as described in the Access Control section of The Swift Programming Language (Swift 4), prevents an external entity from accessing anything declared as internal in an app or framework. By default, to be able to access these items from your test code, you would need to elevate their access level to at least public, reducing the benefits of Swift’s type safety.

Xcode为该问题提供了两部分的解决方案:

Xcode provides a two-part solution to this problem:

当您将启用可测试性"构建设置设置为是"时,对于新项目中的测试版本,默认情况下为true,Xcode包含-enable-testing标志在编译期间.这使得在已编译模块中声明的Swift实体有资格获得更高级别的访问权限.当您将@testable属性添加到启用测试编译的模块,您可以激活提升的访问权限在那个范围内的那个模块.班级和班级成员标记为内部或公共行为就像它们被标记为打开一样.其他实体标记为内部行为,就好像它们被宣布为公共行为一样.

When you set the Enable Testability build setting to Yes, which is true by default for test builds in new projects, Xcode includes the -enable-testing flag during compilation. This makes the Swift entities declared in the compiled module eligible for a higher level of access. When you add the @testable attribute to an import statement for a module compiled with testing enabled, you activate the elevated access for that module in that scope. Classes and class members marked as internal or public behave as if they were marked open. Other entities marked as internal act as if they were declared public.

更多此处

最新swift最酷的地方之一就是开源.因此,如果您想深入了解魔术",请查看一下: https://github.com/apple/迅速

Late edit: One of the cool parts of swift is that is open source. So if you want to dive deep into the "magic", check it out: https://github.com/apple/swift

这篇关于XCTest的@testable幕后发生了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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