6

I have created a ProdIgnoreAttribute which extends from IgnoreAttribute. And I have assigned this attribute to certain tests which I want to run in DEV/QA but not in PROD.

ApplyToTest(Test test) method is not being called in this case. How to resolve this?

Any help is appreciated.

public class ProdIgnoreAttribute : IgnoreAttribute
{
    private string IgnoreReason { get; }

    public ProdIgnoreAttribute(string reason) : base(reason) 
    {
        IgnoreReason = reason;
    }

    public new void ApplyToTest(Test test)
    {
        if (test.RunState == RunState.NotRunnable)
            return;

        if (StaticInfoHelper.VrCurrentEnv == (int)RunEnv.DEMO)
        {
            test.RunState = RunState.Ignored;
            test.Properties.Set("_SKIPREASON", (object)IgnoreReason);
        }
        else
        {
            base.ApplyToTest(test);
        }
    }
}
  • Couple of thoughts... You can use category instead. Tests can be run by category. Look into the TestSettings file. I don't know of a way to inject an attributeType at run time into a class based on CurrentEnv. – John Peters Jun 20 '17 at 20:19
  • Thanks for the response. I have modified the code in my question. But facing another problem. Any idea on this ? – ranp Jun 21 '17 at 13:26
1

I would go another way.

  1. Set test categories for the given tests (e.g. "QA", "DEV", "PROD")
  2. Create a CI job for NUnit test execution for given category on a specific environment. 1 category - 1 job.
  3. If needed - create a master job which will trigger the child (environment-specific jobs) and this will allow you execute the tests against your environments at once.

I think the code itself should be environment agnostic and condition logic depending on environment variables will reduce the readability and increase maintenance effort. Creating the jobs on the CI is not a silver bullet as well, but there is always a some point of a trade-off.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.