简体   繁体   English

如何测试Xcode Source Editor Extension?

[英]How to test Xcode Source Editor Extension?

I am writing XCode Source Editor Extension and I would like to add some tests so I can develop faster. 我正在编写XCode Source Editor Extension,并且我想添加一些测试,以便可以更快地进行开发。

My setup is 我的设置是

MyContainerApplication
  - SourceEditorExtension
    - SourceEditorCommand.swift
    - MyClass.swift

I would like to test MyClass.swift. 我想测试MyClass.swift。 MyContainerApplication has target dependency to SourceEditorExtension. MyContainerApplication对SourceEditorExtension具有目标依赖关系。

So I create Unit test with MyContainerApplication set as a host application. 因此,我将MyContainerApplication设置为主机应用程序来创建单元测试。

In my test I can then call: 在测试中,我可以致电:

@testable import MyContainerApplication

class Test: XCTestCase {

    func testSomething() {

         let c = MyClass()
    }
}

But XCode says MyClass can't be found, which makes sense because it is not included in the HostApplication but in the SourceEditorExtension. 但是XCode表示无法找到MyClass,这是有道理的,因为它不在HostApplication中,而是在SourceEditorExtension中。 I also can't call 我也不能打电话

@testable import SourceEditorExtension

because it's not a module. 因为它不是模块。

And I can't add MyClass.swift directly to my test target or host application target because it uses XcodeKit framework. 而且我不能将MyClass.swift直接添加到测试目标或宿主应用程序目标中,因为它使用XcodeKit框架。 I will get an error: 我会得到一个错误:

XcodeKit module not found

It looks like XcodeKit can't be imported anywhere else than in the SourceEditorExtension, not even in tests. 看起来XcodeKit不能导入到SourceEditorExtension以外的任何地方,甚至不能在测试中导入。

So my question is - is there some another way to test the Source Editor Extension? 所以我的问题是-还有其他方法可以测试Source Editor Extension吗?

You can check how Unit Tests done here SwiftInitializerGenerator . 您可以在此处检查SwiftInitializerGenerator如何完成单元测试。 Testable code is shared between two targets. 可测试的代码在两个目标之间共享。 And no @testable inside tests. 而且没有@testable内部测试。 Try to get rid of any dependence of XcodeKit inside your class. 尝试摆脱类中对XcodeKit的任何依赖。 Probably now it is the one way for extension tests 现在可能是扩展测试的一种方法

确保将MyClass.swift设置为SourceEditorExtensionMyContainerApplicationTests

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

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