The annotated method will be run before all tests in this suite have run.
@AfterSuite
The annotated method will be run after all tests in this suite have run.
@BeforeTest
The annotated method will be run before any test method belonging to the classes inside the tag is run.
@AfterTest
The annotated method will be run after all the test methods belonging to the classes inside the tag have run.
@BeforeGroups
The list of groups that this configuration method will run before. This method is guaranteed to run shortly before the first test method that belongs to any of these groups is invoked.
@AfterGroups
The list of groups that this configuration method will run after. This method is guaranteed to run shortly after the last test method that belongs to any of these groups is invoked.
@BeforeClass
The annotated method will be run before the first test method in the current class is invoked.
@AfterClass
The annotated method will be run after all the test methods in the current class have been run.
@BeforeMethod
The annotated method will be run before each test method.
@AfterMethod
The annotated method will be run after each test method.
TestNG测试类超类中的注解行为
The annotations above will also be honored (inherited) when placed on a superclass of a TestNG class. This is useful for example to centralize test setup for multiple test classes in a common superclass.
In that case, TestNG guarantees that the “@Before” methods are executed in inheritance order (highest superclass first, then going down the inheritance chain), and the “@After” methods in reverse order (going up the inheritance chain).
注解行为
描述
alwaysRun
1. For before methods (beforeSuite, beforeTest, beforeTestClass and beforeTestMethod, but not beforeGroups): If set to true, this configuration method will be run regardless of what groups it belongs to.2. For after methods (afterSuite, afterClass, …): If set to true, this configuration method will be run even if one or more methods invoked previously failed or was skipped.
dependsOnGroups
The list of groups this method depends on.
dependsOnMethods
The list of methods this method depends on.
enabled
Whether methods on this class/method are enabled.
groups
The list of groups this class/method belongs to.
inheritGroups
If true, this method will belong to groups specified in the @Test annotation at the class level.
onlyForGroups
Only for @BeforeMethod and @AfterMethod. If specified, then this setup/teardown method will only be invoked if the corresponding test method belongs to one of the listed groups.
@DataProvider
Marks a method as supplying data for a test method. The annotated method must return an Object[][] where each Object[] can be assigned the parameter list of the test method. The @Test method that wants to receive data from this DataProvider needs to use a dataProvider name equals to the name of this annotation.name:The name of this data provider. If it’s not supplied, the name of this data provider will automatically be set to the name of the method.parallel:If set to true, tests generated using this data provider are run in parallel. Default value is false.
@Factory
Marks a method as a factory that returns objects that will be used by TestNG as Test classes. The method must return Object[].
@Listeners
That is used to listen the whole process of excuting, If use default listener, do not neet to do anything, If use personal lisener, then you have to config.Defines listeners on a test class.value:The list of variables used to fill the parameters of this method.
@Test
Marks a class or a method as part of the test.alwaysRun:If set to true, this test method will always be run even if it depends on a method that failed.dataProvider:The name of the data provider for this test method.dataProviderClass:The class where to look for the data provider. If not specified, the data provider will be looked on the class of the current test method or one of its base classes. If this attribute is specified, the data provider method needs to be static on the specified class.dependsOnGroups:The list of groups this method depends on.dependsOnMethods:The list of methods this method depends on.description:The description for this method.enabled:Whether methods on this class/method are enabled.expectedExceptions:The list of exceptions that a test method is expected to throw. If no exception or a different than one on this list is thrown, this test will be marked a failure.groups:The list of groups this class/method belongs to.invocationCount:The number of times this method should be invoked.invocationTimeOut:The maximum number of milliseconds this test should take for the cumulated time of all the invocationcounts. This attribute will be ignored if invocationCount is not specified.priority:The priority for this test method. Lower priorities will be scheduled first.successPercentage:The percentage of success expected from this method.singleThreaded:If set to true, all the methods on this test class are guaranteed to run in the same thread, even if the tests are currently being run with parallel=“methods”. This attribute can only be used at the class level and it will be ignored if used at the method level. Note: this attribute used to be called sequential (now deprecated).timeOut:The maximum number of milliseconds this test should take.threadPoolSize:The size of the thread pool for this method. The method will be invoked from multiple threads as specified by invocationCount. Note: this attribute is ignored if invocationCount is not specified
代码实例
package org.example;import org.testng.annotations.BeforeMethod;import org.testng.annotations.BeforeClass;import org.testng.annotations.BeforeGroups;import org.testng.annotations.BeforeSuite;import org.testng.annotations.BeforeTest;import org.testng.annotations.AfterMethod;import org.testng.annotations.AfterClass;import org.testng.annotations.AfterGroups;import org.testng.annotations.AfterTest;import org.testng.annotations.AfterSuite;import org.testng.annotations.Test;publicclassFirstTest{@BeforeMethodpublicvoidbeforeMethod(){
System.out.println("This is BeforeMethod...");}@BeforeTestpublicvoidbeforeTest(){
System.out.println("This is BeforeTest...");}@BeforeSuitepublicvoidbeforeSuite(){
System.out.println("This is BeforeSuite...");}@BeforeGroups(groups ={"group1"})publicvoidbeforeGroups1(){
System.out.println("This is group1 of BeforeGroups...");}@BeforeGroups(groups ={"group2"})publicvoidbeforeGroups2(){
System.out.println("This is group2 of BeforeGroups...");}@BeforeClasspublicvoidbeforeClass(){
System.out.println("This is BeforeClass...");}@AfterMethodpublicvoidafterMethod(){
System.out.println("This is AfterMethod...");}@AfterClasspublicvoidafterClass(){
System.out.println("This is AfterClass...");}@AfterTestpublicvoidafterTest(){
System.out.println("This is AfterTest...");}@AfterGroups(groups ={"group1"})publicvoidafterGroups1(){
System.out.println("This is group1 of AfterGroups...");}@AfterGroups(groups ={"group2"})publicvoidafterGroups2(){
System.out.println("This is group2 of AfterGroups...");}@AfterSuitepublicvoidafterSuite(){
System.out.println("This is AfterSuite...");}@Test(groups ={"group1"})publicvoidtestCaseN01(){
System.out.println("Try testing something in group1");}@Test(groups ={"group2"})publicvoidtestCaseN02(){
System.out.println("Try testing something in group2");}@Test(groups ={"group2"})publicvoidtestCaseN03(){
System.out.println("Try testing something in group2");}@Test(groups ={"group1","group2"})publicvoidtestCaseN04(){
System.out.println("Try testing something in group1 and group2");}}
testng.xml配置
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" ><suitename="All Test Suite"><testverbose="2"preserve-order="true"name="D:/MavenTestNG"><classes><classname="org.example.FirstTest"><methods><includename="testCaseN01"/><includename="testCaseN02"/><includename="testCaseN03"/><includename="testCaseN04"/></methods></class></classes></test></suite>
基于TestNG6.14.3版本执行测试用例
This is BeforeSuite...
This is BeforeTest...
This is BeforeClass...
This is group1 of BeforeGroups...
This is BeforeMethod...
Try testing something in group1
This is AfterMethod...
This is group2 of BeforeGroups...
This is BeforeMethod...
Try testing something in group2
This is AfterMethod...
This is BeforeMethod...
Try testing something in group2
This is AfterMethod...
This is BeforeMethod...
Try testing something in group1 and group2
This is AfterMethod...
This is group2 of AfterGroups...
This is group1 of AfterGroups...
This is AfterClass...
This is AfterTest...
PASSED: testCaseN01
PASSED: testCaseN02
PASSED: testCaseN03
PASSED: testCaseN04
===============================================
src/test
Tests run: 4, Failures: 0, Skips: 0
===============================================
This is AfterSuite...
===============================================
All Test Suite
Total tests run: 4, Failures: 0, Skips: 0
===============================================
Process finished with exit code 0