简体   繁体   English

resharper可以跳转到包含单元测试的文件吗?

[英]Can resharper jump to the file that contains the unit tests?

Is it possible to somehow link or use some convention so I can jump between my unit tests for a given class? 是否有可能以某种方式链接或使用某些约定,以便我可以在给定类的单元测试之间跳转?

Also, creating short cuts for jumping between the interface, the implementations? 另外,在界面和实现之间创建快捷方式?

(keyboard shortcuts) (键盘快捷键)

Example: 例:

IUserService UserService UserServiceTests IUserService UserService UserServiceTests

It would be great if I can somehow link these together so I can jump to any of these files while in any one of them currently. 如果我可以以某种方式将这些链接在一起,那将是很好的,所以我可以跳转到任何这些文件,而在其中任何一个目前。

I just implemented that feature in TestLinker , which is a ReSharper 2016.1 extension. 我刚刚在TestLinker中实现了该功能,这是一个ReSharper 2016.1扩展。 It is available to install from the ReSharper Gallery. 它可以从ReSharper Gallery安装。

演示

As has already been mentioned you can do this using the TestCop ReSharper plugin ( gallery link ). 正如已经提到的,您可以使用TestCop ReSharper插件( 图库链接 )来完成此操作。

It ties the class under test to the test fixture by using regular expressions to identify class names and namespaces. 它通过使用正则表达式来识别类名和命名空间,从而将被测试的类与测试夹具联系起来。 You can customise these to fit your needs, but I found there to be a fair amount of trial and error to get this right on existing code. 您可以自定义这些以满足您的需求,但我发现有相当多的试验和错误,以便在现有代码上实现这一点。

Once it's all set up you can go back and forth with a keyboard shortcut. 一旦完成设置,您就可以使用键盘快捷键来回切换。 It can also do things like create the TestFixture or the class for you. 它也可以为您创建TestFixture或类。

Is it possible to somehow link or use some convention so I can jump between my unit tests for a given class? 是否有可能以某种方式链接或使用某些约定,以便我可以在给定类的单元测试之间跳转?

To jump between unit tests for a given class, launch ReSharper's Find Usages on the class name, and as soon as you have results in the Find Results tool window, group them in a way that helps focus on usages in a particular part of your code base - for example, by project and type. 要在给定类的单元测试之间跳转,请在类名称上启动ReSharper的Find Usages ,并在“ 查找结果”工具窗口中找到结果后立即对它们进行分组,以便有助于关注代码的特定部分中的用法base - 例如,按项目和类型。 This will let detect usages in your test project. 这将检测您的测试项目中的用法。 From there, you can quickly jump from Find Results to actual usages in code. 从那里,您可以快速从查找结果跳转到代码中的实际用法。 As an alternative, you can use ReSharper's Go to Usages of Symbol that works in a similar way but displays search results in a pop-up menu instead of flushing them to Find Results . 作为替代方案,您可以使用ReSharper的Go to Usages of Symbol以类似的方式工作,但在弹出菜单中显示搜索结果,而不是将它们刷新到查找结果

If your test classes contain metadata showing which business logic they're covering, this would help even better in differentiating the usages you need. 如果您的测试类包含显示他们所涵盖的业务逻辑的元数据,这将有助于更好地区分您需要的用法。 For example, if you're using MSpec, test classes are marked with the Subject attribute: [Subject(typeof (MyCoveredClass))] This is handy because usages within this attribute are very visible, and navigating to them leads you directly to declarations of your test classes: 例如,如果您正在使用MSpec,则测试类将使用Subject属性进行标记: [Subject(typeof (MyCoveredClass))]这很方便,因为此属性中的用法非常明显,导航到它们会直接导致您声明你的考试班: 查找涵盖特定类的MSpec测试类

With NUnit and MSTest, this is a bit more complicated as their attributes take strings as parameters, like this: [TestProperty("TestKind", "MyCoveredClass")] . 使用NUnit和MSTest,这有点复杂,因为它们的属性将字符串作为参数,如下所示: [TestProperty("TestKind", "MyCoveredClass")] In order to find such a usage of MyCoveredClass , you'll have to use ReSharper's Find Usages Advanced and turn on the Textual occurrences option. 为了找到MyCoveredClass这种用法,你必须使用ReSharper的Find Usages Advanced并打开Textual occurrence选项。

Also, creating short cuts for jumping between the interface, the implementations? 另外,在界面和实现之间创建快捷方式?

As to jumping within an inheritance chain, ReSharper provides multiple options to do that, including Type Hierarchy (ReSharper > Inspect > Type Hierarchy) and Go to Implementation (ReSharper > Navigate > Go to Implementation): 至于在继承链中跳转,ReSharper提供了多种选项来实现,包括类型层次结构 (ReSharper> Inspect> Type Hierarchy)和Go to Implementation (ReSharper> Navigate> Go to Implementation):

使用Go to Implementation和/或Type Hierarchy在整个继承链中导航

ReSharper doesn't have a specific Goto Test/Code feature other than navigating through the list of usages. 除了浏览用法列表之外,ReSharper没有特定的Goto测试/代码功能。

However, TestDriven.NET has this feature which uses naming conventions to find the Test/Code peer such that you can flip back and forth. 但是, TestDriven.NET具有此功能该功能使用命名约定来查找测试/代码对等,以便您可以来回切换。

Also, creating short cuts for jumping between the interface, the implementations? 另外,在界面和实现之间创建快捷方式?

ReSharper has this feature. ReSharper具有此功能。 Using the Visual Studio scheme: 使用Visual Studio方案:

  • Alt + Home navigates to the class' base, in case there are more than one a context menu will list them Alt + Home导航到类的基础,如果有多个上下文菜单将列出它们
  • Alt + End navigates down the inheritance hierarchy and behaves like Alt + Home Alt + End在继承层次结构中向下导航,其行为类似于Alt + Home

Ctrl + U and Ctrl + Alt + B respectively is the equivalent for the ReSharper 2.x / IDEA scheme. Ctrl + U和Ctrl + Alt + B分别与ReSharper 2.x / IDEA方案等效。

You can use the ReSharper Extension TestCop 您可以使用ReSharper Extension TestCop

This plugin is designed for use with mstest & nunit but should work with any other unittest framework that requires you to assign a test Attribute. 此插件设计用于mstest和nunit,但应与任何其他要求您分配测试属性的unittest框架一起使用。

With ReSharper and NUnit, to jump from test to subject, you can use the TestOf property of the TestFixture attribute. 使用ReSharper和NUnit,要从测试跳转到主题,可以使用TestFixture属性的TestOf属性。 Just Ctrl + Click on MyClass in the test file: 只需按住Ctrl键并单击测试文件中的MyClass

[TestFixture(TestOf = typeof(MyClass))]
public class MyClassTest

To jump from subject to test, use the ReSharper Find usage command. 要从主题跳转到测试,请使用ReSharper Find usage命令。

i don't think this is going to be possible with just resharper. 我不认为只用resharper就可以做到这一点。 as far as resharper is concerned, your unit test is just another usage of UserService. 就resharper而言,您的单元测试只是UserService的另一种用法。

also, all of the different unit testing frameworks specify things differently, so it'd be tough to know. 此外,所有不同的单元测试框架都以不同的方式指定事物,因此很难知道。 for instance, doing bdd would yield test class names almost entirely unrelated to the class(es) being tested. 例如,执行bdd将产生几乎完全与被测试的类无关的测试类名。

you might be able to write an extension to do this, maybe using attributes or something? 你可以写一个扩展来做这个,也许使用属性或什么? not sure. 不确定。

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

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