Are there “Dynamic Playlists” of unit tests in Visual Studio?

回眸只為那壹抹淺笑 提交于 2019-12-23 22:22:14

问题


When working with unit tests in Visual Studio, it's possible to compile playlists that group the tests. This is useful, for instance, when developing a new feature for which we want to run only the related tests and not all the existing ones.

However, it seems to me the playlists can only be maintained "manually", by explicitly adding/removing unit tests.

It's better than nothing, but it would be great if I could some how "tag" my unit tests depending on some criterion (e.g. feature, test duration, etc.) and generate the playlists dynamically.

Is there any way to do something similar in Visual Studio with the default testing framework?


回答1:


You can run certain tests contextually in relation to the current cursor position by right clicking and then "Run Tests", or hitting CTRL+R then T.

  • If the cursor is within the body of a test, only that test will be run.
  • If the cursor is within the body of a test class, only the tests in that class will be run.
  • If the cursor is within a namespace, all tests in that namespace will be run.

See MSDN documentation on the subject.

Alternatively you can filter the test explorer in various ways. I often use the Project flag to exclude certain projects for instance.




回答2:


In VS2017, you can group tests by Namespace, Class, Duration, Outcome, Traits or Project. Probably was there before, but I can't check first hand right now.

To do so, in the Test Explorer window, click on the second icon then whichever choice you want. By default, they're grouped by Outcome.

If you want to use Traits, add the Trait name/value attribute to your individual test cases:

[Trait("Foo", "Value1")]
[Trait("Foo", "Value2")]
[Trait("Bar", "Value")]
public void MyClass_MyMethod_WhenThing_Outcome()
{
    // ...
}


来源:https://stackoverflow.com/questions/23512181/are-there-dynamic-playlists-of-unit-tests-in-visual-studio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!