ibo
ibo

Reputation: 21

How can I run testng tests by priority in Intellij?

How can I run ONLY tests with priority = 2? I have hundreds tests and enabled=false does not work for me, thanks!

 @Test(priority = 1)
    public  void test_1_1(){
}

 @Test(priority = 1)
    public  void test_1_2(){
}

 @Test(priority = 2)
    public  void test_2_1(){
}
 @Test(priority = 2)
    public  void test_2_2(){
}
 @Test(priority = 2)
    public  void test_2_3(){
}
 @Test(priority = 3)
    public  void test_3_1(){
}
 @Test(priority = 3)
    public  void test_3_2(){
}

Upvotes: 0

Views: 607

Answers (2)

Magnilex
Magnilex

Reputation: 11958

This can be achieved by using the interceptor mechanism of TestNG. In fact, a complete example can be found here:

public class PriorityInterceptor implements IMethodInterceptor {

  @Override
  public List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext context) {
    List<IMethodInstance> result = new ArrayList<>();
    for (IMethodInstance method : methods) {
      Test testMethod = method.getMethod().getConstructorOrMethod().getMethod().getAnnotation(Test.class);
      if (testMethod.priority() == 2) {
        result.add(method);
      }
    }
    return result;
  }
}

The interceptor can then be configured in the testng.xml file:

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite1" verbose="1">
    <listeners>
        <listener class-name="com.easy.PriorityInterceptor" />
    </listeners>
    <test name="Regression Test Suite">
        <classes>
            <class name="com.easy.TestA" />
            <class name="com.easy.TestB" />
        </classes>
    </test>
</suite>

Or passed via the command line as in the official documentation example:

java -classpath "testng-jdk15.jar:test/build" org.testng.TestNG -listener test.methodinterceptors.NullMethodInterceptor -testclass test.methodinterceptors.FooTest

Upvotes: 1

papaya
papaya

Reputation: 1535

Based on my assumption, you might be using Junit as a testing framework. Junit4 has introduced a thing called Categories. However this may not be exactly what you are looking for, Categories help you create a Category for your test method and this will help you run only a specific category which you could mention under your maven plugin or grade plugins

Finally you could configure them in your plugin for your project. Have a look at the wiki.

In general, you can't specify the order that separate unit tests run in (though you could specify priorities in TestNG and have a different priority for each test). However, unit tests should be able to be run in isolation, so the order of the tests should not matter. This is a bad practice. If you need the tests to be in a specific order, you should be rethinking your design. If you post specifics as to why you need the order, I'm sure we can offer suggestions.

Upvotes: 0

Related Questions