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 TestNG-如何设置通过TestNG.xml运行@Test方法的顺序?_Java_Ubuntu_Intellij Idea_Testng - Fatal编程技术网

Java TestNG-如何设置通过TestNG.xml运行@Test方法的顺序?

Java TestNG-如何设置通过TestNG.xml运行@Test方法的顺序?,java,ubuntu,intellij-idea,testng,Java,Ubuntu,Intellij Idea,Testng,我的@Test类中有30个@Test方法和2个Java方法。 例如,我需要在concrete@Test方法之后运行这两个Java方法, TestMethod5()。 我该怎么做 例如: @Test public void TestMethod5() { /* compiled code */ } public void Method1(){/* compiled code */}; public void Method2{/* compiled code */}; 需要两种方法: 1)

我的
@Test
类中有30个
@Test
方法和2个Java方法。 例如,我需要在concrete
@Test
方法之后运行这两个Java方法,
TestMethod5()
。 我该怎么做

例如:

@Test
public void TestMethod5() {

/* compiled code */

}

public void Method1(){/* compiled code */};
public void Method2{/* compiled code */};
需要两种方法:

1) 使用testng.xml
2) 用Intellij思想

注意:
@beforethod
@AfterMethod
仅适用于扩展 classname命令。这两个Java方法检查 按钮和标签使用
assertTrue()
方法,所以我不想弄乱我的 和他们在一起



是的,这些方法是测试的一部分(以及未来类似的测试),但我不能复制它们的内容并粘贴到我想要的每个方法中…然后@test methods将被弄乱(大代码)。我只需要检查这些方法中标签和按钮的呈现:

 public void method1_ButtonsTest() {

  assertTrue1();
  assertTrue2();

 }


 public void method2_LabelsTest() {

  assertTrue3();
  assertTrue4();
 }


@Test
public void Test1();

@Test
public void Test2();

@Test
public void Test3();

@Test
public void Test4();

@Test
public void Test5();

@Test
public void Test6();


@Test
public void Test7();

@Test
public void Test8();

你为什么不能直接调用这些方法呢

@Test public void testMethod5() {
    ...
    method1();
    method2();
}

如果您不想修改基类,也不想在测试中直接调用方法,那么您可以向测试类添加另一个@AfterMethod,并且只为您调用的测试用例运行方法,然后在基类中调用@AfterMethod,如下所示:

@AfterMethod(alwaysRun = true)
public void postTestCase(ITestResult _result) {
    if (_result.getMethod().getMethodName().equals("Test1")){
          System.out.println("Do something here...");
    }
    //Call the @AfterMethod in the base class
    super.postTestCase(_result);
}

我同意,如果这些方法是更高级别的断言,那么它们就是测试的一部分,就像更简单的assertXxxx一样。是的,这些方法是测试的一部分(以及未来类似的测试),但我不能只是复制它们的内容并粘贴到我想要的每个方法中……那么@test方法将被弄乱(大代码)。我只需要检查这些方法public void method1()中标签和buton的呈现情况{我不是说你应该复制它们的内容,只是它们是更高级的断言,因此应该从每个需要这些断言的测试中显式调用。它们可以被称为
assertLabelRendering()
assertButtonRendering()
。此外,如果它们不是在测试本身中调用的,而是在
@AfterMethod
或等效方法中调用的,如果它们失败了,您可能无法直接知道哪个测试失败了,只能知道外部断言在独立方法中失败了。