如何仅在azure devops CI env中而不是在本地运行某些NUnit测试 [英] How to run some NUnit tests only in azure devops CI env but not locally
问题描述
Identical to this: How to run some tests only in azure devops CI env but not locally, but for NUnit, rather than xUnit.
我希望我的某些测试能够通过Azure DevOps管道运行,而不希望通过本地(VisualStudio + R#)IDE运行.
I want some of my test to get run by my Azure DevOps pipeline, but not by my local (VisualStudio + R#) IDE.
链接的问题使用自定义的xUnit属性解决了此问题,但是我的项目使用NUnit,因此该问题的答案无济于事.
Linked question solves this with a custom xUnit Attribute, but my project uses NUnit so answers to that question won't help.
推荐答案
-
扩展
NUnitAttribute
和IApplyToTest(Test test)
以创建自定义属性,该属性将允许您控制是否运行特定测试.Extend
NUnitAttribute
andIApplyToTest(Test test)
to create a custom attribute that will allow you to control whether or not a particular test is run.在
ApplyToTest
的实现中,使用new IgnoreAttribute("Blah").ApplyToTest(Test test)
忽略测试(如果适用).In your implementation of
ApplyToTest
, usenew IgnoreAttribute("Blah").ApplyToTest(Test test)
to ignore the test (if applicable).-
IgnoreAttribute.ApplyToTest()
不是虚拟的,因此您不能安全地从IgnoreAttribute
和override
派生该方法.改用这种成分更安全.
IgnoreAttribute.ApplyToTest()
is not virtual so you can't safely derive fromIgnoreAttribute
andoverride
the method. Safer to use this composition instead.
使用
Environment.GetEnvironmentVariable
通过检查是否定义了特定的环境变量(您自己定义的变量还是AzureDevOps定义的自动定义的变量之一)来确定是否跳过测试. c8>)Use
Environment.GetEnvironmentVariable
to determine whether or not to skip the test, by checking whether a particular Environment variable is defined (either one you define yourself, or one of the auto-defined variables that AzureDevOps defines, e.g.TF_BUILD
)- 我发现检查
.Machine
,.User
或.Process
中的任何环境变量都很方便.看到这里: https://stackoverflow.com/a/41410599/1662268
- I found it convenient to check for any of the Environment Variables from
.Machine
,.User
or.Process
. See here: https://stackoverflow.com/a/41410599/1662268
这篇关于如何仅在azure devops CI env中而不是在本地运行某些NUnit测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
-