问题
When working in Xcode, it's often nice to pull up unit test cases in an assistant editor. Currently, I've been selecting the tests file manually, but I see the assistant editor has an option for Test Classes
.
I've tried to get this option to pull up my tests file automatically, but I can't seem to make it work. Is there some sort of configuration required for it? Does it just not work for swift projects?
回答1:
The assistant editor can automatically open the companion file in many cases. However, afaik, there is no automatic setting for opening the unit test.
To get the behavior you want you can fuzzy search with open quickly ⌘+⇧+o then open the result in the assistant editor with ⌥+⏎.
You might also find use in open quickly ⌘+⇧+o then ⇧+⏎.
You can see more about "Working Efficiently with Xcode" here.
回答2:
The shortcuts mentioned in other answers work great, but they don't make the assistant editor automatically work with test classes. Turns out it didn't work with swift code previously, but Apple just added support for this to Xcode 9:
A file with a base name of FooTest or FooTests is now considered a counterpart of a file with a base name of Foo for navigation and the Assistant Editor. (28981734)
So, with Xcode 9, the test target filename needs to have the same name as the main code file + either 'Test' or 'Tests' and the Xcode test classes assistant editor will work automatically.
回答3:
It seems that on Xcode 8 it automatically setup the suitable test class in the assistant editor after you import the original class inside the test class.
@testable import YOUR_MODULE_NAME
class MyViewControllerTests: XCTestCase {
var myVC : MyViewController!
}
After that you will find MyViewControllerTests on the assistant editor under "Tests Classes".
来源:https://stackoverflow.com/questions/34340997/use-xcodes-test-classes-assistant-editor-with-swift-classes