1

I want only some subset of my test methods to run on a production environment. I annotate such test methods with @ProdAllowed annotation. I also wrote a small custom JUnit runner, which overrides the runChild method so it runs only for @ProdAllowed methods while on "PROD" environment:

public class ProdAwareRunner extends BlockJUnit4ClassRunner { public ProdAwareRunner(Class<?> klass) throws InitializationError { super(klass); } @Override protected void runChild(FrameworkMethod method, RunNotifier notifier) { ProdAllowed annotation = method.getAnnotation(ProdAllowed.class); String env = CreditCheckSuite.getEnv().trim(); if (annotation != null || "DEV".equalsIgnoreCase(env) || "UAT".equalsIgnoreCase(env)) { super.runChild(method, notifier); } else { notifier.fireTestIgnored(null); // this probably needs to be changed } } } 

This works quite well, but I want a little more - to have this skipped test methods to be marked in Eclipse as ignored (right now they are marked as not run, which is not exactly what I want)

1
  • 1
    A built-in mechanism within junit is using categories. Commented Nov 27, 2014 at 15:02

2 Answers 2

2

You could write a rule by extending TestWatcher

public class DoNotRunOnProd extends TestWatcher { protected void starting(Description description) { { ProdAllowed annotation = description.getAnnotation(ProdAllowed.class); String env = CreditCheckSuite.getEnv().trim(); if ((annotation == null) && !"DEV".equalsIgnoreCase(env) && !"UAT".equalsIgnoreCase(env)) { throw new AssumptionViolatedException("Must not run on production.") } } } 

and add it to your test

public class Test { @Rule public final TestRule doNotRunOnProd = new DoNotRunOnProd(); ... } 
Sign up to request clarification or add additional context in comments.

2 Comments

It seems that these days rules are replacing runners, no need to write a custom runner here. +1
My main question is in the last sentence of original post - I would like to have not annotated tests marked as ignored while on PROD. With your approach, Eclipse marks not annotated tests as passed, which is not really what I want to see.
0

This is already implemented (and actively used) in TestNG Groups:

public class Test1 { @Test(groups = { "dev", "uat" }) public void testMethod1() { } @Test(groups = {"uat", "prod"} ) public void testMethod2() { } @Test(groups = { "prod" }) public void testMethod3() { } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.