testNG之组测试
@Test(groups = {""})
在執行測試用例的時候,往往一個功能依賴多個測試用例,比如流程的測試,那么這個時候就可以用到組測試,把流程涉及到測試用例都分到同一組里,按組執行即可。
testNG的組通過@Test的groups屬性來指定的,一個方法可以屬于一個組(@Test(groups = {"checkintest"})),也可以屬于多個組(@Test(groups = {"functest","checkintest"}))。
假設現在有3個java代碼,common.java、functionA.java、functionB.java,測試流程涉及到common.java中 login() 和?quit() 方法、functionA.java中的?testMethod1() 方法、functionB.java中的?testMethod3() 方法,那么可以將這4個方法分到同一組里functest,代碼如下:
?
common.java:
import org.testng.annotations.Test;public class common {@Test(groups = {"functest","checkintest"})public void login() {System.out.println("login");}@Test(groups = {"functest","checkintest"})public void quit() {System.out.println("quit");}@Test(groups = {"checkintest"})public void init() {System.out.println("init");} }?
functionA.java:
import org.testng.annotations.Test;public class functionA {@Test(groups = {"functest"})public void testMethod1() {System.out.println("this is testMethod1"); } @Test(groups = {"functest2"})public void testMethod2() {System.out.println("this is testMethod2"); } }?
functionB.java:
import org.testng.annotations.Test;public class functionB {@Test(groups = {"functest"})public void testMethod3() {System.out.println("this is testMethod3"); } @Test(groups = {"functest2"})public void testMethod4() {System.out.println("this is testMethod4"); } }?
testng.xml:
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" ><suite name="Suite1" verbose="1" ><test name="Regression1" preserve-order="true"><groups><run><include name = "functest" /></run> </groups><classes><class name="common"></class><class name="functionA"></class><class name="functionB"></class></classes></test> </suite>注意:testng.xml需要指定要測試的組(<groups>……</groups>)和組所在的class(<classes>……</classes>)
?
運行testng.xml,執行結果如下:
login quit this is testMethod1 this is testMethod3
@Test(priority = )
一般quit都是在流程的最后才執行,如何控制組里方法執行的順序呢?可以通過@Test的priority屬性,testNG按照priority從小到大的順序執行
修改common.java,在@Test中添加屬性priority = 1和priority = 4:
import org.testng.annotations.Test;public class common {@Test(groups = {"functest","checkintest"},priority = 1)public void login() {System.out.println("login");}@Test(groups = {"functest","checkintest"},priority = 4)public void quit() {System.out.println("quit");}@Test(groups = {"checkintest"})public void init() {System.out.println("init");} }?
修改functionA.java,在@Test中添加屬性priority = 2:
import org.testng.annotations.Test;public class functionA {@Test(groups = {"functest"},priority = 2)public void testMethod1() {System.out.println("this is testMethod1"); } @Test(groups = {"functest2"})public void testMethod2() {System.out.println("this is testMethod2"); } }?
修改functionB.java,在@Test中添加屬性priority = 3:
import org.testng.annotations.Test;public class functionB {@Test(groups = {"functest"}, priority = 3)public void testMethod3() {System.out.println("this is testMethod3"); } @Test(groups = {"functest2"})public void testMethod4() {System.out.println("this is testMethod4"); } }?
再次運行testng.xml,執行結果如下:
login this is testMethod1 this is testMethod3 quit?
如何只執行組里 testMethod1() 和 testMethod3() 方法,而不執行 login() 和 quit() 方法呢?
修改testng.xml,添加<exclude name="checkintest"></exclude>,排除在checkintest組里的方法
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" ><suite name="Suite1" verbose="1" ><test name="Regression1" preserve-order="true"><groups><run><include name = "functest" /><exclude name="checkintest"></exclude></run> </groups><classes><class name="common"></class><class name="functionA"></class><class name="functionB"></class></classes></test> </suite>?
再次運行testng.xml,執行結果如下:
this is testMethod1 this is testMethod3?
轉載于:https://www.cnblogs.com/yuan-yuan/p/4498134.html
總結
以上是生活随笔為你收集整理的testNG之组测试的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 智能家居 (8) ——智能家居项目整合(
- 下一篇: 【转】WEB前端调优