问题
Identical to this: How to run some tests only in azure devops CI env but not locally, but for NUnit, rather than xUnit.
I want some of my test to get run by my Azure DevOps pipeline, but not by my local (VisualStudio + R#) IDE.
Linked question solves this with a custom xUnit Attribute, but my project uses NUnit so answers to that question won't help.
回答1:
Extend
NUnitAttribute
andIApplyToTest(Test test)
to create a custom attribute that will allow you to control whether or not a particular test is run.In your implementation of
ApplyToTest
, usenew IgnoreAttribute("Blah").ApplyToTest(Test test)
to ignore the test (if applicable).IgnoreAttribute.ApplyToTest()
is not virtual so you can't safely derive fromIgnoreAttribute
andoverride
the method. Safer to use this composition instead.
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
)- 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
- I found it convenient to check for any of the Environment Variables from
来源:https://stackoverflow.com/questions/62388204/how-to-run-some-nunit-tests-only-in-azure-devops-ci-env-but-not-locally