1、TestNG按順序執行Case
1)優先級法:@Test(Priority=?)優先級設定的值越小,則越優先。
2)字母排序法:由@Test的方法名的首字母由a~~z。
3)在xml中設定辨別 preserve-order可以控制類和方法執行順序,其順序就是xml中指定的順序。
< test name ="test12" preserve-order ="true">
2、TestNG異常測試
@Test(expectedExceptions=ArithmeticException.class)
@Test(expectedExceptions = IllegalArgumentException.class)
預期該方法中會出現這樣的異常,一旦運作的時候出現異常則認為是測試通過,擷取到了該異常不會報失敗
3、TestNG測試分組
1) 在方法上的分組 package TankLearn2.Learn;
import org.testng.annotations.Test;
public class GroupTest {
@Test(groups = {"selenium-test"})
public void testLogin(){
System.out.println("this is test login");
}
@Test(groups = {"functiontest"})
public void testOpenPage(){
System.out.println("this is test Open Page");
}
}
然後在testng.xml中 按組執行測試用例 <? xml version="1.0" encoding="UTF-8" ?> <! DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > < suite name ="Suite1"> < test name ="test1"> < groups > < run > < include name =" selenium-test " /> </ run > </ groups >
<classes> <class name="TankLearn2.Learn.GroupTest" /> </classes> </ test > </ suite > 還有一種all的情況? 還有相容的情況 @Test(groups = {"functiontest"," selenium-test "}) 2) 在類上的分組
package com.yiibai;
import org.testng.annotations.Test;
@Test(groups = "selenium-test")
public class TestSelenium {
public void runSelenium() {
System.out.println("runSelenium()");
}
public void runSelenium1() {
System.out.println("runSelenium()1");
}
}
建立一個測試類: testng.xml ,其代碼如下所示 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > < suite name =" TestAll "> <!-- Run test method on group "selenium" only --> < test name =" selenium "> < groups > < run > < include name =" selenium-test " /> </ run > </ groups > < classes > < class name =" com.yiibai.TestSelenium " /> < class name =" com.yiibai.TestGroup " /> </ classes > </ test > </ suite > 總結: 分組測試其實可以控制哪部分方法和類執行測試,主要通過xxx.xml進行配置和控制 < groups > < run > < include name =" selenium-test " /> </ run > </ groups >
< classes > < class name =" com.yiibai.TestSelenium " /> < class name =" com.yiibai.TestGroup " /> </ classes > 代表類( com.yiibai.TestSelenium,com.yiibai.TestGroup )中帶有@Test(groups = " selenium-test " )所有類和方法都要執行。
4、 TestNG依賴測試 1) dependsOnMethods import org.testng.annotations.Test;
public class TestngDependencies { @Test public void testmethod(){ System.out.println("我是否為第一個執行"); } @Test public void serverstartok (){ System.err.println("method1 runs after me......"); } @Test(dependsOnMethods = {"serverstartok"} ) public void method1(){ System.out.println("depens on serverstartok."); } } 2)dependsOnGroups package com.yiibai;
import org.testng.Assert; import org.testng.annotations.Test;
public class DependencyTestUsingAnnotation { String message = "Manisha"; MessageUtil messageUtil = new MessageUtil(message);
@Test(groups = { "init1" }) public void testPrintMessage() { System.out.println("Inside testPrintMessage()"); message = "Manisha"; Assert.assertEquals(message, messageUtil.printMessage()); }
@Test(groups = { "init1" }, dependsOnGroups = { "init2" }, alwaysRun = true) //執行init1組的時候,要先執行init2組的東西(被依賴) public void testSalutationMessage() { System.out.println("Inside testSalutationMessage()"); message = "Hi!" + "Manisha"; Assert.assertEquals(message, messageUtil.salutationMessage()); }
@Test(groups = { "init2" }) public void initEnvironmentTest() { System.out.println("This is initEnvironmentTest"); } } ------------------------------------------------------------------------------------------------------- <test verbose="2" name="TestngDependOnGroups"> <groups> <run> <include name="init1" /> </run> </groups> ----------------- groups用來控制分組的,執行所有groups=init1----------------------- <classes> <class name="com.yiibai.DependencyTestUsingAnnotation"> </class> </classes> </test> ---------------- 強制依賴 :如果被依賴的某一個方法發生了 異常 ,那麼之後的方法都 不會被執行 (預設) 順序依賴 :無論被依賴的方法是否出現異常, 後續的方法都會被執行,通過alwaysRun=“true”來配置
預設是強制依賴,就是出現異常就終止。。。。程式跳出
- public class TestngDependOnGroups {
- @Test(groups = { "init" })
- public void serverStartedOk() {
- System.out.println("serverStartedOk.....");
- }
- @Test(groups = { "init2" })
- public void initEnvironment() {
- System.out.println("initEnvironment.....");
- throw new RuntimeException("unexpected fail......");
- }
- @Test(dependsOnGroups = { "init.*" })
- public void method1() {
- System.err.println("I am over here.....");
- }
- }
如果添加, alwaysRun = true則變為順序依賴,哪怕被依賴出現異常也繼續執行
- @Test(dependsOnGroups = { "init.*" }, alwaysRun = true)
- public void method1() {
- System.err.println("I am over here.....");
- }
<classes> <class name="com.yiibai.TestngDependOnGroups "> </class> </classes>
注意:單獨執行組的時候,dependsOnGrops @Test (dependsOnGroups = { "init.*" }) 的不會執行,隻有 @Test(groups = { "init1" }, dependsOnGroups = { "init2" }, alwaysRun = true)在執行組init1的時候才能執行。
5、TestNG忽略測試
有時候測試用例還沒準備好, 可以給測試用例加上 @Test(enable = false) , 來禁用此測試用例 package TankLearn2.Learn; import org.testng.annotations.Test; public class TesgNGIgnore { @Test(enabled = false ) public void testIgnore(){ System.out.println("This test case will ignore"); }}
6、TestNG參數化