简体   繁体   English

如何创建仅在手动指定时运行的单元测试?

[英]How to create unit tests which runs only when manually specified?

I remember something like ' explicit ', and google says that nunit has such attribute. 我记得像' 明确 '这样的东西,谷歌说nunit有这样的属性。 Does Microsoft.VisualStudio.TestTools.UnitTesting provide something like this? Microsoft.VisualStudio.TestTools.UnitTesting是否提供类似这样的内容?

The MSTest tools does not explicitly support this type of behavior at an attribute level. MSTest工具不在属性级别明确支持此类行为。 At the attribute level you can either enable a test via the TestMethod attribute or completely disable it with the Ignore attribute. 在属性级别,您可以通过TestMethod属性启用测试,也可以使用Ignore属性完全禁用它。 Once the Ignore attribute is added, mstest will not run the test until it is removed. 添加Ignore属性后,mstest将不会运行测试,直到删除它为止。 You cannot override this behavior via the UI. 您无法通过UI覆盖此行为。

What you can do though is disable the test via the property page. 你可以做的是通过属性页面禁用测试。 Open up the test list editor, select the test you want and hit F4 to bring up the property page. 打开测试列表编辑器,选择所需的测试,然后按F4打开属性页面。 Set the Test Enabled property to false. 将Test Enabled属性设置为false。 The test will now not run until you re-enable it through the property page. 在您通过属性页重新启用测试之前,测试现在不会运行。 It's not exactly what you're looking for but likely the closest equivalent. 这不是你想要的,但可能是最接近的等价物。

You can create a "Run Manually" category for your tests using the Category attribute, and then exclude that category from your tests in the GUI. 您可以使用Category属性为测试创建“手动运行”类别,然后从GUI中的测试中排除该类别。 These tests will be grayed out, and you can put them back in whenever you want. 这些测试将显示为灰色,您可以随时将它们放回原处。 I do this often for slow-running tests. 我经常这样做是为了慢速运行测试。

I haven't used it, and it looks pretty old (Mar 2008), but I see that TestListGenerator claims to auto-generate Test Lists based on attributes you set on your tests. 我没有使用它,它看起来很旧(2008年3月),但我发现TestListGenerator声称根据您在测试中设置的属性自动生成测试列表 If it works, this would effectively provide Categories for MS Test. 如果它有效,这将有效地提供MS测试的类别 While not the same as Explicit , it may let you achieve what you want. 虽然与Explicit不同 ,但它可以让你实现你想要的。

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

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