簡體   English   中英

TestNG-如何通過testng.xml設置運行@Test方法的順序?

[英]TestNG - How can I set up the order of running @Test methods through testng.xml?

我的@Test類中有30個@Test方法和2個Java方法。 我需要在具體的@Test方法之后運行這2個Java方法,例如TestMethod5() 我怎樣才能做到這一點 ?

例如:

@Test
public void TestMethod5() {

/* compiled code */

}

public void Method1(){/* compiled code */};
public void Method2{/* compiled code */};

需要2種方法:

1)與testng.xml
2)使用Intellij IDEA

注意: @BeforeMethod@AfterMethod僅與extends classname命令一起使用。 這兩個Java方法使用assertTrue()方法檢查按鈕和標簽的呈現,所以我不想將它們與我的基類弄混。


是的,這些方法是測試的一部分(以及將來的類似測試),但是我不能只復制它們的內容並粘貼到我想要的每個方法中...然后@Test方法將被弄亂(大代碼)。只需使用以下方法檢查標簽和按鈕的渲染即可:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM