![](/img/trans.png)
[英]Can I call @Test method programmatically when TestNG.xml has list of classes only not methods?
[英]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.