TestNG –配置注释示例

测试配置

在TestNG中,我们可以使用以下注释为您的测试类进行配置,例如设置/清理数据库,准备虚拟数据,部署/关闭服务器等。

@BeforeSuite - For suite test, run before all tests in this suite have run. 
@AfterSuite -  For suite test, run after all tests in this suite have run.

@BeforeTest - For suite test, run before any test method belonging to the classes inside the <test> tag is run. 
@AfterTest - For suite test, run after all the test methods belonging to the classes inside the <test> tag have run. 

@BeforeGroups: Run before the first test method that belongs to the group is invoked. 
@AfterGroups: Run after the last test method that belongs to the groups is invoked. 

@BeforeClass - Run before the first test method in the current class is invoked. 
@AfterClass - Run after all the test methods in the current class have been run. 

@BeforeMethod - Run before each test method. 
@AfterMethod - Run after each test method.

PS Suite测试–一起运行多个测试类。

查看以下示例以查看执行顺序-首先调用哪个方法,然后调用哪个方法。

1.单班考试

运行一个测试用例,显示before / after groupclassmethod

TestConfiguration.java
package com.mkyong.testng.examples.configuration;

import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterGroups;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class TestConfiguration {
	
	@BeforeGroups("shopping")
	public void beforeGroups() {
		System.out.println("@BeforeGroups");
	}

	@AfterGroups("shopping")
	public void afterGroups() {
		System.out.println("@AfterGroups");
	}

	@BeforeClass
	public void beforeClass() {
		System.out.println("@BeforeClass");
	}

	@AfterClass
	public void afterClass() {
		System.out.println("@AfterClass");
	}

	@BeforeMethod
	public void beforeMethod() {
		System.out.println("@BeforeMethod");
	}

	@AfterMethod
	public void afterMethod() {
		System.out.println("@AfterMethod");
	}

	@Test(groups = "shopping")
	public void runTest1() {
		System.out.println("@Test - runTest1");
	}

	@Test
	public void runTest2() {
		System.out.println("@Test - runTest2");
	}
}

输出量

@BeforeClass 

@BeforeGroups 
@BeforeMethod 
@Test - runTest1
@AfterMethod  
@AfterGroups  

@BeforeMethod
@Test - runTest2
@AfterMethod

@AfterClass

PASSED: runTest1
PASSED: runTest2

===============================================
    Default test
    Tests run: 2, Failures: 0, Skips: 0
===============================================

2.套件测试类

再创建2个测试类,以显示before和after suitetest的用法。

DBConfig.java
package com.mkyong.testng.examples.configuration;

import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;

public class DBConfig {

	@BeforeSuite()
	public void beforeSuite() {
		System.out.println("@BeforeSuite");
	}

	@AfterSuite()
	public void afterSuite() {
		System.out.println("@AfterSuite");
	}

	@BeforeTest()
	public void beforeTest() {
		System.out.println("@BeforeTest");
	}

	@AfterTest()
	public void afterTest() {
		System.out.println("@AfterTest");
	}

}
TestDBConnection.java
package com.mkyong.testng.examples.configuration;

import org.testng.annotations.Test;

public class TestDBConnection {

	@Test
	public void runOtherTest1() {
		System.out.println("@Test - runOtherTest1");
	}

	@Test
	public void runOtherTest2() {
		System.out.println("@Test - runOtherTest2");
	}

}

创建一个XML文件以一起运行多个测试用例。 阅读XML注释以进行自我解释。

testng.xml
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<!-- @BeforeSuite -->
<suite name="TestAll">

	<!-- @BeforeTest -->
	<test name="case1">
	  <classes>
		<class name="com.mkyong.testng.examples.configuration.TestConfiguration" />
		<class name="com.mkyong.testng.examples.configuration.TestDBConnection" />
		<class name="com.mkyong.testng.examples.configuration.DBConfig" />
	  </classes>
	</test>
	<!-- @AfterTest -->
	
	<!-- @BeforeTest -->
	<test name="case2">
	  <classes>
		<class name="com.mkyong.testng.examples.configuration.TestDBConnection" />
		<class name="com.mkyong.testng.examples.configuration.DBConfig" />
	  </classes>
	</test>
	<!-- @AfterTest -->
	
</suite>
<!-- @AfterSuite -->

输出量

@BeforeSuite

@BeforeTest		//Start {case1}
@BeforeClass
@BeforeGroups
@BeforeMethod
@Test - runTest1
@AfterMethod
@AfterGroups
@BeforeMethod
@Test - runTest2
@AfterMethod
@AfterClass
@Test - runOtherTest1
@Test - runOtherTest2
@AfterTest		//End {case1}


@BeforeTest		//Start {case2}
@Test - runOtherTest1
@Test - runOtherTest2
@AfterTest		//End {case2}

@AfterSuite

===============================================
TestAll
Total tests run: 6, Failures: 0, Skips: 0
===============================================

做完了

参考文献

  1. TestNG文档
  2. TestNG –运行多个测试类(套房测试)

翻译自: https://mkyong.com/unittest/testng-tutorial-1-basic-usage/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值