Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在Intellij中按优先级运行testng测试?_Java_Intellij Idea_Testng - Fatal编程技术网

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

Java 如何在Intellij中按优先级运行testng测试?,java,intellij-idea,testng,Java,Intellij Idea,Testng,如何仅运行优先级为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

如何仅运行优先级为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(){
}

基于我的假设,您可能正在使用
Junit
作为测试框架
Junit4
引入了一个名为
Categories
的东西。然而,这可能不是你想要的,帮助你为你的测试方法创建一个类别,这将帮助你只运行一个你可以在maven插件或grade插件中提到的特定类别

最后,您可以在项目的插件中配置它们。看看维基

通常,不能指定单独单元测试的运行顺序 在中(尽管您可以在TestNG中指定优先级,并使用不同的 每个测试的优先级)。但是,单元测试应该能够运行 因此,测试的顺序应该无关紧要。这是一个 坏习惯。如果您需要按照特定顺序进行测试,您可以 应该重新考虑你的设计。如果你发布了你为什么要这样做的细节 需要订单,我相信我们可以提供建议


这可以通过使用TestNG的拦截器机制来实现。事实上,一个完整的例子是:

公共类优先级拦截器实现IMethodInterceptor{
@凌驾
公共列表截取(列表方法、ITestContext上下文){
列表结果=新建ArrayList();
对于(i方法:方法){
TestTestMethod=method.getMethod().getConstructorOrMethod().getMethod().getAnnotation(Test.class);
if(testMethod.priority()==2){
结果:添加(方法);
}
}
返回结果;
}
}
然后可以在testng.xml文件中配置拦截器:


或如官方文件示例中所示:

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


@木瓜:问题是TestNG,也被贴上了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;
  }
}
<!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>