基本注解
TestNG注解说明
Before(@BeforeSuite…)
After(@AfterSuite…)
方法属性说明
@Test属性说明
注解方法的执行顺序
如图所示:注解方法的执行顺序。
示例
下面开始演示各注解执行顺序示例:
1、创建Annotation类(添加@Test等注解)
脚本代码:
package com.demo.annotation;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class Annotation {
@Test
public void testCase1() {
System.out.println("testCase1");
}
@Test
public void testCase2() {
System.out.println("testCase2");
}
@BeforeMethod
public void beforeMethod() {
System.out.println("beforeMethod");
}
@AfterMethod
public void afterMethod() {
System.out.println("afterMethod");
}
@BeforeClass
public void beforeClass() {
System.out.println("beforeClass");
}
@AfterClass
public void afterClass() {
System.out.println("afterClass");
}
@BeforeTest
public void beforeTest(){
System.out.println("beforeTest");
}
@AfterTest
public void afterTest(){
System.out.println("afterTest");
}
@BeforeSuite
public void beforeSuite() {
System.out.println("beforeSuite");
}
@AfterSuite
public void afterSuite() {
System.out.println("afterSuite");
}
}
2、创建testng_annotation.xml文件
文件内容:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="test">
<test name="Test1">
<classes>
<class name="com.demo.annotation.Annotation" />
</classes>
</test>
<test name="Test2">
<classes>
<class name="com.demo.annotation.Annotation" />
</classes>
</test>
</suite>
3、执行脚本(testng_annotation.xml鼠标右键Run As--->TestNG Suite)。
4、执行结果:
控制台打印结果信息:
TestNG结果报告:
本文分享自 AllTests软件测试 微信公众号,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文参与 腾讯云自媒体同步曝光计划 ,欢迎热爱写作的你一起参与!