How to test Xcode Source Editor Extension?

382 views Asked by At

I am writing XCode Source Editor Extension and I would like to add some tests so I can develop faster.

My setup is

MyContainerApplication
  - SourceEditorExtension
    - SourceEditorCommand.swift
    - MyClass.swift

I would like to test MyClass.swift. MyContainerApplication has target dependency to SourceEditorExtension.

So I create Unit test with MyContainerApplication set as a host application.

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. 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. 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.

So my question is - is there some another way to test the Source Editor Extension?

2

There are 2 answers

0
Igor Palaguta On

You can check how Unit Tests done here SwiftInitializerGenerator. Testable code is shared between two targets. And no @testable inside tests. Try to get rid of any dependence of XcodeKit inside your class. Probably now it is the one way for extension tests

0
DàChún On

Make sure your MyClass.swift is set to both SourceEditorExtension and MyContainerApplicationTests.