- 浏览: 7943724 次
- 性别:
- 来自: 广州
文章分类
- 全部博客 (2425)
- 软件工程 (75)
- JAVA相关 (662)
- ajax/web相关 (351)
- 数据库相关/oracle (218)
- PHP (147)
- UNIX/LINUX/FREEBSD/solaris (118)
- 音乐探讨 (1)
- 闲话 (11)
- 网络安全等 (21)
- .NET (153)
- ROR和GOG (10)
- [网站分类]4.其他技术区 (181)
- 算法等 (7)
- [随笔分类]SOA (8)
- 收藏区 (71)
- 金融证券 (4)
- [网站分类]5.企业信息化 (3)
- c&c++学习 (1)
- 读书区 (11)
- 其它 (10)
- 收藏夹 (1)
- 设计模式 (1)
- FLEX (14)
- Android (98)
- 软件工程心理学系列 (4)
- HTML5 (6)
- C/C++ (0)
- 数据结构 (0)
- 书评 (3)
- python (17)
- NOSQL (10)
- MYSQL (85)
- java之各类测试 (18)
- nodejs (1)
- JAVA (1)
- neo4j (3)
- VUE (4)
- docker相关 (1)
最新评论
-
xiaobadi:
jacky~~~~~~~~~
推荐两个不错的mybatis GUI生成工具 -
masuweng:
(转)JAVA获得机器码的实现 -
albert0707:
有些扩展名为null
java 7中可以判断文件的contenttype了 -
albert0707:
非常感谢!!!!!!!!!
java 7中可以判断文件的contenttype了 -
zhangle:
https://zhuban.me竹板共享 - 高效便捷的文档 ...
一个不错的网络白板工具
1 各类@before,@after注解的例子:
package test.beforeafter;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterGroups;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class TestClass {
/**
* Before suite method which gets executed before
* starting of any of the test in the suite.
*/
@BeforeSuite
public void beforeSuite(){
System.out.println("Before Suite method");
}
/**
* After suite method which gets executed after
* execution of all the tests in a suite.
*/
@AfterSuite
public void afterSuite(){
System.out.println("After Suite method");
}
/**
* Before Test method which gets executed before
* each test mentioned inside the 'test' tag inside a test suite.
*/
@BeforeTest
public void beforeTest(){
System.out.println("Before Test method");
}
/**
* After Test method which gets executed after
* each test mentioned inside the 'test' tag inside a test suite.
*/
@AfterTest
public void afterTest(){
System.out.println("After Test method");
}
/**
* Before Class method which gets executed before
* any of the test-methods inside a class.
*/
@BeforeClass
public void beforeClass(){
System.out.println("Before Class method");
}
/**
* After Class method which gets executed after
* all of the test-methods inside a class gets executed.
*/
@AfterClass
public void afterClass(){
System.out.println("After Class method");
}
/**
* Before group method gets executed before executing the tests
* belonging to the group as mentioned in the 'groups' attribute.
* The following method gets executed before execution of the test-method belonging to group "testOne".
*/
@BeforeGroups(groups={"testOne"})
public void beforeGroupOne(){
System.out.println("Before Group Test One method");
}
/**
* After group method gets executed after executing the tests
* belonging to the group as mentioned in the 'groups' attribute.
* The following method gets executed after execution of the test-methods belonging to group "testOne".
*/
@AfterGroups(groups={"testOne"})
public void afterGroupOne(){
System.out.println("After Group Test One method");
}
/**
* Before group method gets executed before executing the tests
* belonging to the group as mentioned in the 'groups' attribute.
* The following method gets executed before execution of the test-method belonging to group "testTwo".
*/
@BeforeGroups(groups={"testTwo"})
public void beforeGroupTwo(){
System.out.println("Before Group Test two method");
}
/**
* After group method gets executed after executing the tests
* belonging to the group as mentioned in the 'groups' attribute.
* The following method gets executed after execution of the test-methods belonging to group "testTwo".
*/
@AfterGroups(groups={"testTwo"})
public void afterGroupTwo(){
System.out.println("After Group Test two method");
}
/**
* Before method which gets executed before each test-method.
*/
@BeforeMethod
public void beforeMethod(){
System.out.println("Before Method");
}
/**
* After method which gets executed after each test-method.
*/
@AfterMethod
public void afterMethod(){
System.out.println("After Method");
}
/**
* Test-method which belongs to group "testOne".
*/
@Test(groups={"testOne"})
public void testOneMethod(){
System.out.println("Test one method");
}
/**
* Test-method which belongs to group "testTwo".
*/
@Test(groups={"testTwo"})
public void testTwoMethod(){
System.out.println("Test two method");
}
}
输出:
Before Suite method
Before Test method
Before Class method
Before Group Test One method
Before Method
Test one method
After Method
After Group Test One method
After Class method
After Test method
Before Test method
Before Class method
Before Group Test two method
Before Method
Test two method
After Method
After Group Test two method
After Class method
After Test method
After Suite method
2 对于注解,也是可以继承的,比如:
一个基类:
public class BaseClass {
@BeforeClass
public void beforeBaseClass(){
System.out.println("Parent Before Class method");
}
@AfterClass
public void afterBaseClass(){
System.out.println("Parent After Class method");
}
@BeforeMethod
public void beforeBaseMethod(){
System.out.println("Parent Before method");
}
@AfterMethod
public void afterBaseMethod(){
System.out.println("Parent After method");
}
}
一个子类继承:
public class TestClass extends BaseClass{
@BeforeClass
public void beforeChildClass(){
System.out.println("Child Before Class method");
}
@AfterClass
public void afterChildClass(){
System.out.println("Child After Class method");
}
@BeforeMethod
public void beforeChildMethod(){
System.out.println("Child Before method");
}
@AfterMethod
public void afterChildMethod(){
System.out.println("Child After method");
}
@Test
public void testMethod(){
System.out.println("Test method under TestClass");
}
}
输出: E:\java资料2\TestNG Beginner's Guide\Codes-6009OS - Reviewed\Chapter 3\BeforeAfterExtend\testng.xml
Parent Before Class method
Child Before Class method
Parent Before method
Child Before method
Test method under TestClass
Child After method
Parent After method
Child After Class method
Parent After Class method
3 private等修饰的方法是不纳入测试的,即使所在类加了@Test
4 禁用某个测试
@Test(enabled=false)
public void testMethodTwo(){
System.out.println("Test method two.");
}
5 测试异常
@Test(expectedExceptions={IOException.class})
public void exceptionTestOne() throws Exception{
throw new IOException();
}
@Test(expectedExceptions={IOException.class,NullPointerException.class})
public void exceptionTestTwo() throws Exception{
throw new Exception();
}
测试异常信息
public class ExceptionMessageTest {
/**
* Verifies the exception message based on the exact error message thrown.
* @throws Exception
*/
@Test(expectedExceptions={IOException.class},expectedExceptionsMessageRegExp="Pass Message test")
public void exceptionMsgTestOne() throws Exception{
throw new IOException("Pass Message test");
}
/**
* Verifies the exception message using the regular exception.
* This test verifies that the exception message contains a test "Message" in it.
* @throws Exception
*/
@Test(expectedExceptions={IOException.class},expectedExceptionsMessageRegExp=".* Message .*")
public void exceptionMsgTestTwo() throws Exception{
throw new IOException("Pass Message test");
}
/**
* Verifies the exception message based on the exact error message thrown.
* This is to show that TestNg fails a test when the exception message does not match.S
* @throws Exception
*/
@Test(expectedExceptions={IOException.class},expectedExceptionsMessageRegExp="Pass Message test")
public void exceptionMsgTestThree() throws Exception{
throw new IOException("Fail Message test");
}
输出:PASSED: exceptionMsgTestOne
PASSED: exceptionMsgTestTwo
FAILED: exceptionMsgTestThree
6 测试超时
public class TimeSuite {
@Test
public void timeTestOne() throws InterruptedException{
Thread.sleep(1000);
System.out.println("Time test method one");
}
@Test
public void timeTestTwo() throws InterruptedException{
Thread.sleep(400);
System.out.println("Time test method two");
}
如果在testng.xml中可以定义整个suite中的方法不能超过 time-out指定的500微秒,如:
<suite name="Time test Suite" time-out="500" verbose="1" >
<test name="Timed Test" >
<classes>
<class name="test.timetest.TimeSuite" />
</classes>
</test>
</suite>
也可以指定某个方法的限制时间,比如:
@Test(timeOut=500)
public void timeTestOne() throws InterruptedException{
Thread.sleep(1000);
System.out.println("Time test method one");
}
7 输入参数
@Parameters({ "suite-param", "test-three-param" })
@Test
public void prameterTestThree(String param, String paramTwo) {
System.out.println("Test three suite param is: " + param);
System.out.println("Test three test level param is: " + paramTwo);
}
<test name="Parameter Test three">
<parameter name="suite-param" value="overiding suite parameter" />
<parameter name="test-three-param" value="test three parameter" />
<classes>
<class name="test.parameter.ParameterTest">
<methods>
<include name="prameterTestThree" />
</methods>
</class>
</classes>
</test>
8 可选参数
public class OptionalTest {
@Parameters({"optional-value"})
@Test
public void optionTest(@Optional("optional value")String value){
System.out.println("This is: "+value);
}
<test name="Optional Test one">
<classes>
<class name="test.parameter.OptionalTest" />
</classes>
</test>
<test name="Optional Test two">
<parameter name="optional-value" value="passed from xml" />
<classes>
<class name="test.parameter.OptionalTest" />
</classes>
</test>
由于在Optional Test one中,没在xml中找到parameter,所以使用@Optional中的参数了
,输出:
This is: optional value
This is: passed from xml
9 dataprovider
public class SameClassDataProvider {
@DataProvider(name = "data-provider")
public Object[][] dataProviderMethod() {
return new Object[][] { { "data one" }, { "data two" } };
}
@Test(dataProvider = "data-provider")
public void testMethod(String data) {
System.out.println("Data is: " + data);
}
输出;Data is: data one
Data is: data two
当然,dataprovider的类也是可以单独的,如:
@Test(dataProvider = "data-provider",dataProviderClass=DataProviderClass.class)
public void testMethod(String data) {
System.out.println("Data is: " + data);
}
package test.beforeafter;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterGroups;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class TestClass {
/**
* Before suite method which gets executed before
* starting of any of the test in the suite.
*/
@BeforeSuite
public void beforeSuite(){
System.out.println("Before Suite method");
}
/**
* After suite method which gets executed after
* execution of all the tests in a suite.
*/
@AfterSuite
public void afterSuite(){
System.out.println("After Suite method");
}
/**
* Before Test method which gets executed before
* each test mentioned inside the 'test' tag inside a test suite.
*/
@BeforeTest
public void beforeTest(){
System.out.println("Before Test method");
}
/**
* After Test method which gets executed after
* each test mentioned inside the 'test' tag inside a test suite.
*/
@AfterTest
public void afterTest(){
System.out.println("After Test method");
}
/**
* Before Class method which gets executed before
* any of the test-methods inside a class.
*/
@BeforeClass
public void beforeClass(){
System.out.println("Before Class method");
}
/**
* After Class method which gets executed after
* all of the test-methods inside a class gets executed.
*/
@AfterClass
public void afterClass(){
System.out.println("After Class method");
}
/**
* Before group method gets executed before executing the tests
* belonging to the group as mentioned in the 'groups' attribute.
* The following method gets executed before execution of the test-method belonging to group "testOne".
*/
@BeforeGroups(groups={"testOne"})
public void beforeGroupOne(){
System.out.println("Before Group Test One method");
}
/**
* After group method gets executed after executing the tests
* belonging to the group as mentioned in the 'groups' attribute.
* The following method gets executed after execution of the test-methods belonging to group "testOne".
*/
@AfterGroups(groups={"testOne"})
public void afterGroupOne(){
System.out.println("After Group Test One method");
}
/**
* Before group method gets executed before executing the tests
* belonging to the group as mentioned in the 'groups' attribute.
* The following method gets executed before execution of the test-method belonging to group "testTwo".
*/
@BeforeGroups(groups={"testTwo"})
public void beforeGroupTwo(){
System.out.println("Before Group Test two method");
}
/**
* After group method gets executed after executing the tests
* belonging to the group as mentioned in the 'groups' attribute.
* The following method gets executed after execution of the test-methods belonging to group "testTwo".
*/
@AfterGroups(groups={"testTwo"})
public void afterGroupTwo(){
System.out.println("After Group Test two method");
}
/**
* Before method which gets executed before each test-method.
*/
@BeforeMethod
public void beforeMethod(){
System.out.println("Before Method");
}
/**
* After method which gets executed after each test-method.
*/
@AfterMethod
public void afterMethod(){
System.out.println("After Method");
}
/**
* Test-method which belongs to group "testOne".
*/
@Test(groups={"testOne"})
public void testOneMethod(){
System.out.println("Test one method");
}
/**
* Test-method which belongs to group "testTwo".
*/
@Test(groups={"testTwo"})
public void testTwoMethod(){
System.out.println("Test two method");
}
}
输出:
Before Suite method
Before Test method
Before Class method
Before Group Test One method
Before Method
Test one method
After Method
After Group Test One method
After Class method
After Test method
Before Test method
Before Class method
Before Group Test two method
Before Method
Test two method
After Method
After Group Test two method
After Class method
After Test method
After Suite method
2 对于注解,也是可以继承的,比如:
一个基类:
public class BaseClass {
@BeforeClass
public void beforeBaseClass(){
System.out.println("Parent Before Class method");
}
@AfterClass
public void afterBaseClass(){
System.out.println("Parent After Class method");
}
@BeforeMethod
public void beforeBaseMethod(){
System.out.println("Parent Before method");
}
@AfterMethod
public void afterBaseMethod(){
System.out.println("Parent After method");
}
}
一个子类继承:
public class TestClass extends BaseClass{
@BeforeClass
public void beforeChildClass(){
System.out.println("Child Before Class method");
}
@AfterClass
public void afterChildClass(){
System.out.println("Child After Class method");
}
@BeforeMethod
public void beforeChildMethod(){
System.out.println("Child Before method");
}
@AfterMethod
public void afterChildMethod(){
System.out.println("Child After method");
}
@Test
public void testMethod(){
System.out.println("Test method under TestClass");
}
}
输出: E:\java资料2\TestNG Beginner's Guide\Codes-6009OS - Reviewed\Chapter 3\BeforeAfterExtend\testng.xml
Parent Before Class method
Child Before Class method
Parent Before method
Child Before method
Test method under TestClass
Child After method
Parent After method
Child After Class method
Parent After Class method
3 private等修饰的方法是不纳入测试的,即使所在类加了@Test
4 禁用某个测试
@Test(enabled=false)
public void testMethodTwo(){
System.out.println("Test method two.");
}
5 测试异常
@Test(expectedExceptions={IOException.class})
public void exceptionTestOne() throws Exception{
throw new IOException();
}
@Test(expectedExceptions={IOException.class,NullPointerException.class})
public void exceptionTestTwo() throws Exception{
throw new Exception();
}
测试异常信息
public class ExceptionMessageTest {
/**
* Verifies the exception message based on the exact error message thrown.
* @throws Exception
*/
@Test(expectedExceptions={IOException.class},expectedExceptionsMessageRegExp="Pass Message test")
public void exceptionMsgTestOne() throws Exception{
throw new IOException("Pass Message test");
}
/**
* Verifies the exception message using the regular exception.
* This test verifies that the exception message contains a test "Message" in it.
* @throws Exception
*/
@Test(expectedExceptions={IOException.class},expectedExceptionsMessageRegExp=".* Message .*")
public void exceptionMsgTestTwo() throws Exception{
throw new IOException("Pass Message test");
}
/**
* Verifies the exception message based on the exact error message thrown.
* This is to show that TestNg fails a test when the exception message does not match.S
* @throws Exception
*/
@Test(expectedExceptions={IOException.class},expectedExceptionsMessageRegExp="Pass Message test")
public void exceptionMsgTestThree() throws Exception{
throw new IOException("Fail Message test");
}
输出:PASSED: exceptionMsgTestOne
PASSED: exceptionMsgTestTwo
FAILED: exceptionMsgTestThree
6 测试超时
public class TimeSuite {
@Test
public void timeTestOne() throws InterruptedException{
Thread.sleep(1000);
System.out.println("Time test method one");
}
@Test
public void timeTestTwo() throws InterruptedException{
Thread.sleep(400);
System.out.println("Time test method two");
}
如果在testng.xml中可以定义整个suite中的方法不能超过 time-out指定的500微秒,如:
<suite name="Time test Suite" time-out="500" verbose="1" >
<test name="Timed Test" >
<classes>
<class name="test.timetest.TimeSuite" />
</classes>
</test>
</suite>
也可以指定某个方法的限制时间,比如:
@Test(timeOut=500)
public void timeTestOne() throws InterruptedException{
Thread.sleep(1000);
System.out.println("Time test method one");
}
7 输入参数
@Parameters({ "suite-param", "test-three-param" })
@Test
public void prameterTestThree(String param, String paramTwo) {
System.out.println("Test three suite param is: " + param);
System.out.println("Test three test level param is: " + paramTwo);
}
<test name="Parameter Test three">
<parameter name="suite-param" value="overiding suite parameter" />
<parameter name="test-three-param" value="test three parameter" />
<classes>
<class name="test.parameter.ParameterTest">
<methods>
<include name="prameterTestThree" />
</methods>
</class>
</classes>
</test>
8 可选参数
public class OptionalTest {
@Parameters({"optional-value"})
@Test
public void optionTest(@Optional("optional value")String value){
System.out.println("This is: "+value);
}
<test name="Optional Test one">
<classes>
<class name="test.parameter.OptionalTest" />
</classes>
</test>
<test name="Optional Test two">
<parameter name="optional-value" value="passed from xml" />
<classes>
<class name="test.parameter.OptionalTest" />
</classes>
</test>
由于在Optional Test one中,没在xml中找到parameter,所以使用@Optional中的参数了
,输出:
This is: optional value
This is: passed from xml
9 dataprovider
public class SameClassDataProvider {
@DataProvider(name = "data-provider")
public Object[][] dataProviderMethod() {
return new Object[][] { { "data one" }, { "data two" } };
}
@Test(dataProvider = "data-provider")
public void testMethod(String data) {
System.out.println("Data is: " + data);
}
输出;Data is: data one
Data is: data two
当然,dataprovider的类也是可以单独的,如:
@Test(dataProvider = "data-provider",dataProviderClass=DataProviderClass.class)
public void testMethod(String data) {
System.out.println("Data is: " + data);
}
发表评论
-
复习:强迫线程顺序执行方式
2019-01-03 23:42 1579方法1: 三个线程,t1,t2,t3,如果一定要按顺序执行, ... -
(转)不错的前后端处理异常的方法
2019-01-02 23:16 2019前言 在 Web 开发中, 我们经常会需要处理各种异常, 这是 ... -
info q的极客时间大咖说等资料下载
2018-08-15 08:40 3472info q的极客时间大咖说等资料下载,还有不少思维导图 链 ... -
CXF 客户端超时时间设置(非Spring配置方式)
2018-07-03 22:38 2236import org.apache.cxf.endpoint. ... -
(转)synchronized关键字画像:正确打开方式
2018-06-14 09:25 490https://mp.weixin.qq.com/s/b3Sx ... -
CountDownLatch的例子
2018-06-13 14:10 691public class StatsDemo { ... -
两道面试题,带你解析Java类加载机制
2018-06-12 16:29 611https://mp.weixin.qq.com/s/YTa0 ... -
Spring中获取request的几种方法,及其线程安全性分析
2018-06-11 09:03 671https://mp.weixin.qq.com/s/KeFJ ... -
内部类小结
2018-06-06 10:25 438https://mp.weixin.qq.com/s/hErv ... -
JVM虚拟机小结1
2018-06-04 20:43 5441 jps -l //列出详细的类名和进程ID 2)jps ... -
windows下自带命令行工具查看CPU资源情况等
2018-06-04 12:53 3101微软提供了不少命令行 ... -
(收藏)深入分析Java的序列化与反序列化
2018-05-30 15:21 617https://mp.weixin.qq.com/s/T2Bn ... -
apache common包中的序列化工具
2018-05-30 09:10 1844什么是序列化 我们的 ... -
JAVA8 JVM的变化: 元空间(Metaspace)
2018-05-24 22:30 967本文将会分享至今为至我收集的关于永久代(Permanent G ... -
(转)服务器性能指标(一)——负载(Load)分析及问题排查
2018-05-21 21:03 1363原创: Hollis Hollis 负载 ... -
(转)对象复用
2018-05-20 15:27 863public class Student { priv ... -
mapreduce中入门中要注意的几点
2018-05-06 08:59 674在 mapreduce中,比如有如下的词: I love b ... -
HDFS的基本操作
2018-05-02 21:47 941-mkdir 在HDFS创建目录 ... -
一个不错的开源工具类,专门用来解析日志头部的,好用
2018-05-02 20:00 772一个不错的开源工具类,专门用来解析日志头部的,好用。 http ... -
介绍个不错的RESTFUL MOCK的工具wiremock
2018-04-27 21:02 1907介绍个不错的RESTFUL MOCK的工具wiremock,地 ...
评论