package com.newcrud.testngTest;
import org.testng.annotations.*;
public class TestOne {
@Test
public void testOne(){
System.out.println("testOne");
}
@Test
public void testTwo(){
System.out.println("testTwo");
}
@Test(groups = "one")
public void testThree(){
System.out.println("testThree");
}
@Test(groups = "one")
public void testFour(){
System.out.println("testFour");
}
@Test(groups = "two")
public void testFive(){
System.out.println("testFive");
}
@Test(groups = "two")
public void testSix(){
System.out.println("testSix");
}
@BeforeSuite
public void testSeven(){
System.out.println("@BeforeSuite,对于套件测试,在此套件中的所有测试运行之前运行");
}
@AfterSuite
public void testEight(){
System.out.println("@AfterSuite,对于套件测试,在此套件中的所有测试运行之后运行。");
}
@BeforeClass
public void testNine(){
System.out.println("@BeforeClass,在当前类的第一个测试方法之前运行。");
}
@AfterClass
public void testTen(){
System.out.println("@AfterClass,运行当前类中的所有测试方法之后都运行。");
}
@BeforeGroups(groups = {"one","two"})
public void testEleven(){
System.out.println("@BeforeGroups:在调用属于该组的第一个测试方法之前运行.");
}
@AfterGroups(groups = {"one","two"})
public void testTwelve(){
System.out.println("@AfterGroups:在调用属于组的最后一个测试方法之后运行。");
}
@BeforeTest
public void testThirteen(){
System.out.println("@BeforeTest - 对于套件测试,在运行属于标签内的类的任何测试方法之前运行。");
}
@AfterTest
public void testFourteen(){
System.out.println("@AfterTest - 对于套件测试,在运行属于标签内的类的所有测试方法都已运行之后运行");
}
@BeforeMethod
public void testFifteen(){
System.out.println("@BeforeMethod - 在每个测试方法之前运行。");
}
@AfterMethod
public void testSixteen(){
System.out.println("@AfterMethod - 在每个测试方法之后运行。");
}
}
package com.newcrud.testngTest;
import org.testng.annotations.*;
public class TestTwo {
@Test
public void testOne(){
System.out.println("TestTwo的testOne");
}
}
package com.newcrud.testngTest;
import org.testng.annotations.Test;
public class TestThree {
@Test
public void testOne(){
System.out.println("TestThree的testOne");
}
}
执行的优先级
执行TestOne,查看结果,暂时先不看Suite和Test相关的注解,重点观察Groups的
@BeforeSuite,对于套件测试,在此套件中的所有测试运行之前运行 @BeforeTest - 对于套件测试,在运行属于标签内的类的任何测试方法之前运行。 @BeforeClass,在当前类的第一个测试方法之前运行。 @BeforeGroups:在调用属于该组的第一个测试方法之前运行. @BeforeMethod - 在每个测试方法之前运行。 testFive @AfterMethod - 在每个测试方法之后运行。 @BeforeGroups:在调用属于该组的第一个测试方法之前运行. @BeforeMethod - 在每个测试方法之前运行。 testFour @AfterMethod - 在每个测试方法之后运行。 @BeforeMethod - 在每个测试方法之前运行。 testOne @AfterMethod - 在每个测试方法之后运行。 @BeforeMethod - 在每个测试方法之前运行。 testSix @AfterMethod - 在每个测试方法之后运行。 @AfterGroups:在调用属于组的最后一个测试方法之后运行。 @BeforeMethod - 在每个测试方法之前运行。 testThree @AfterMethod - 在每个测试方法之后运行。 @AfterGroups:在调用属于组的最后一个测试方法之后运行。 @BeforeMethod - 在每个测试方法之前运行。 testTwo @AfterMethod - 在每个测试方法之后运行。 @AfterClass,运行当前类中的所有测试方法之后都运行。 @AfterTest - 对于套件测试,在运行属于 标签内的类的所有测试方法都已运行之后运行 @AfterSuite,对于套件测试,在此套件中的所有测试运行之后运行。
刚开始我学习的时候在这里甚是迷惑,明明one的第一个为Three第二个为Four,two的第一个为Five第二个为Six。那BeforeGroups应该在Three和Five前,AfterGroups应该在Four和Six后,但是结果确是BeforeGroups在Five和Four前,AfterGroupsThree和Six后。然后就钻牛角尖出不来了,直到后来发现整个带@Test的执行顺序都是按照one、two、three来的,是按照首字母的前后来排序的,好吧,通过搜索引擎也确认了这个点,困扰了我半个多小时。
Suite和Test


