TestNG按順序執行case
package com.testngDemo; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; public class DemoTestng { @BeforeClass public void setup() { System.out.println("begin test"); } @Test public void test1() { System.out.println("at test1"); } @Test public void test2() { System.out.println("at test2"); } @Test public void test3() { System.out.println("at test3"); } @AfterClass public void teardown() { System.out.println("end test"); } }
在xml文件中配置:
執行一下:
TestNG參數化執行case
我們在測試中經常會遇到參數化執行case的情況, TestNG為我們提供了兩種參數化的方式:
1.在xml文件中傳遞參數
2.使用@DataProvider傳遞參數
第一種方式:
<suite>和<test>標簽定義了suite和test兩種測試范圍:一個test可以包含一系列的測試方法,一個suite可以包含多個獨立的test。這兩種測試范圍有什么區別呢?一個test的所有測試方法都是針對同一測試對象,測試方法之間可以相互影響。而一個suite的每個test都是針對一個單獨測試對象,兩個test中的測試方法不會相互影響。
在這兩種測試范圍定義的參數,滿足如下規律:
1)在Suite范圍內定義某個參數的值,對所有的Test都有效。
2)在Test范圍內定義某個參數的值,只是針對該Test有效。
3)如果同時在Suite和Test中定義某個參數,Test范圍的值會屏蔽Suite的值。
示例代碼如下:
xml文件:
package com.testngDemo; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Parameters; import org.testng.annotations.Test; public class DemoTestng { @BeforeClass public void setup() { System.out.println("begin test"); } @Test @Parameters({"name"}) public void test1(String test1) { System.out.println("測試1的名字是"+test1); } @AfterClass public void teardown() { System.out.println("end test"); } }
package com.testngDemo; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Parameters; import org.testng.annotations.Test; public class DemoTestng2 { @BeforeClass public void setup() { } @Test @Parameters("name") public void test(String name) { System.out.println("測試2的名字是"+name); } @AfterClass public void teardown() { } }
執行完成的結果是:
第二種方式:
執行結果是:
TestNG忽略性測試
測試結果:
TestNG依賴性測試
在@Test后面添加dependsOnMethods={"testName"},則這個case依賴於testName這個case,如下圖,我忽略了test1測試,那么dependTest也沒法執行。
TesNG多線程並發執行
執行結果: