如何在Intellij中按优先级运行TestNG测试?

huangapple go评论84阅读模式
英文:

How can I run testng tests by priority in Intellij?

问题

如何仅运行优先级为2的测试
我有数百个测试但是 enabled=false 对我不起作用谢谢

@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() {
}
英文:

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(){
}

答案1

得分: 1

这可以通过使用 TestNG 的拦截器机制来实现。实际上,一个完整的示例可以在这里找到

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;
  }
}

然后可以在 testng.xml 文件中配置拦截器:

<!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>

或者通过命令行传递,就像官方文档示例中一样:

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

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&lt;IMethodInstance&gt; intercept(List&lt;IMethodInstance&gt; methods, ITestContext context) {
    List&lt;IMethodInstance&gt; result = new ArrayList&lt;&gt;();
    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:

&lt;!DOCTYPE suite SYSTEM &quot;http://testng.org/testng-1.0.dtd&quot; &gt;
&lt;suite name=&quot;Suite1&quot; verbose=&quot;1&quot;&gt;
	&lt;listeners&gt;
		&lt;listener class-name=&quot;com.easy.PriorityInterceptor&quot; /&gt;
	&lt;/listeners&gt;
	&lt;test name=&quot;Regression Test Suite&quot;&gt;
		&lt;classes&gt;
			&lt;class name=&quot;com.easy.TestA&quot; /&gt;
			&lt;class name=&quot;com.easy.TestB&quot; /&gt;
		&lt;/classes&gt;
	&lt;/test&gt;
&lt;/suite&gt;

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

答案2

得分: 0

根据我的假设,您可能在使用Junit作为测试框架。Junit4引入了一个称为Categories的东西。然而,这可能不完全是您要寻找的,Categories可帮助您为测试方法创建一个类别,这将帮助您仅运行特定类别的测试,您可以在maven插件或gradle插件中提及这一点。

最后,您可以在项目的插件中对它们进行配置。请查阅wiki。

> 通常情况下,您无法指定单独单元测试运行的顺序(尽管您可以在TestNG中指定优先级,并为每个测试指定不同的优先级)。然而,单元测试应该能够独立运行,因此测试的顺序不应该影响结果。这是一种不良实践。如果您需要测试以特定顺序运行,您应该重新考虑您的设计。如果您具体说明为什么需要这个顺序,我们肯定可以提供建议。

英文:

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.

huangapple
  • 本文由 发表于 2020年10月21日 00:03:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/64449005.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定