C# 忽略忽略属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10120087/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 12:29:50  来源:igfitidea点击:

Ignore IgnoreAttribute

c#mstestresharper-5.1

提问by brgerner

We have MSTest tests which automatically run in hourly production. One of these tests is marked with [Ignore]attribute because it is not yet ready to run it in our production environment.
Now I want to start that test (only) on my local environment because my local environment is ready for that test.
I try to run that test by clicking on ReSharper's icon on the left side of test code or by clicking Run Selected Testsicon in Unit Test Sessionswindow and nothing happens.

我们有 MSTest 测试,可以在每小时生产中自动运行。其中一个测试被标记为[Ignore]属性,因为它还没有准备好在我们的生产环境中运行它。
现在我想(仅)在我的本地环境中开始该测试,因为我的本地环境已准备好进行该测试。
我尝试通过单击测试代码左侧的 ReSharper 图标或单击单元测试会话”窗口中的“运行选定的测试”图标来运行测试,但没有任何反应。

I fix it currently by commenting out the [Ignore]line. But now I need to be aware to remove the comment characters (//) before checking-in the code.

我目前通过注释掉该[Ignore]行来修复它。但是现在我需要注意//在签入代码之前删除注释字符 ( )。

Is there another way to temporarly run an [Ignore]'d test ?

是否有另一种方法可以临时运行[Ignore]'d 测试?

采纳答案by Skonen

Recently when I have encountered problems such as this, I add a new Build Configurationto the visual studio project named something such as "Local Developer Debug" and use the settings from the existing Debug configuration. Then I go to "Project -> MyProjectName Properties -> Build", make sure "Local Developer Debug" is the selected configuration and add "LOCALDEVBUILD" to "Conditional compliation symbols". This allows for the use of preprocessor directives to 'toggle' code at compile time:

最近当我遇到这样的问题时,我将一个新的构建配置添加到名为“本地开发人员调试”之类的 Visual Studio 项目中,并使用现有调试配置中的设置。然后我转到“Project -> MyProjectName Properties -> Build”,确保“Local Developer Debug”是选定的配置,并将“LOCALDEVBUILD”添加到“Conditional compliation symbols”。这允许使用预处理器指令在编译时“切换”代码:

#if (!LOCALDEVBUILD)
    [Ignore]
#endif

Not sure if this is what you're looking for... but it allows you to run/utilize specific code depending on the intentions of the build (via the build configuration)... With this method you can leave the test ignored for more 'official' builds, but still execute it at your leisure if you so desire.

不确定这是否是您要查找的内容……但它允许您根据构建的意图(通过构建配置)运行/利用特定代码……使用这种方法,您可以忽略测试更多“官方”构建,但如果您愿意,仍然可以在闲暇时执行它。

回答by Davin Tryon

It looks like there are other ways to enable/disable tests using the testrunconfig file. However, if you use the IgnoreAttribute it compiles in with the code and therefore cannot be easily toggled.

看起来还有其他方法可以使用 testrunco​​nfig 文件启用/禁用测试。但是,如果您使用 IgnoreAttribute,它会与代码一起编译,因此无法轻松切换。

From the documentation:

文档

The Ignore attribute for a unit test resides in the source file of the unit test, together with the other attributes and source code of the test. This means that, if you disable a unit test in the Test Manager window, and later run the test by using the MSTest.exe command-line utility, the test still runs. But if you mark a unit test with the Ignore attribute, compile the assembly, and then run the test by using MSTest.exe, the test does not run. It does not run because the Ignore attribute has become part of the test.

单元测试的 Ignore 属性与测试的其他属性和源代码一起位于单元测试的源文件中。这意味着,如果您在“测试管理器”窗口中禁用单元测试,然后使用 MSTest.exe 命令行实用程序运行该测试,该测试仍会运行。但是,如果您使用 Ignore 属性标记单元测试,编译程序集,然后使用 MSTest.exe 运行测试,则测试不会运行。它不会运行,因为 Ignore 属性已成为测试的一部分。

回答by Lasse Normann

You could also use a test categoryto mark the test methods you do not want to be included in your automated test, e.g.

您还可以使用测试类别来标记您不希望包含在自动化测试中的测试方法,例如

[TestCategory("IgnoreOnBuild")]

Thus not using the [Ignore] attribute. And combine this with a filter in your build definition, under Basic -> Automated Test -> Test Source -> Test Case Filter:

因此不使用 [Ignore] 属性。并将其与构建定义中的过滤器结合起来,在 Basic -> Automated Test -> Test Source -> Test Case Filter 下:

TestCategory!=IgnoreOnBuild