testng 中的执行顺序逻辑

it2026-04-18  6

testng 执行顺序目录

Annotation类执行顺序Case执行顺序

Annotation类执行顺序

method -> class -> test -> suite

@BeforeSuite/@AfterSuite 在testng定义的xml根元素里面的所有执行之前/后运行@BeforeTest/@AfterTest 在一个元素定义的所有里面所有测试方法执行之前/后运行@BeforeClass/@AfterClass 在当前测试类的第一个测试方法执行之前/最后一个测试方法执行之后运行@BeforeMethod/@AfterMethod 在当前测试类的每一个测试方法执行之前/后运行@BeforeGroup/@AfterGroup 在同个组的第一个测试方法之前/最后一个测试方法之后运行

Case执行顺序

testng中添加属性preserve-order, true按顺序执行 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="tests" parallel="true"> <test name="tests" preserve-order="true"> <!-- preserve-order为true,表示<tests>下所有<classes>顺序执行 --> <classes> <class name="test.001"/> <class name="test.002"/> </classes> </test> </suite>

2.在@Test中添加priority属性

priority小,先执行; @Test(priority = 1) public void test01() throws Exception { }

testng和priority同时配置时,priority的优先级高

最新回复(0)