TestNG入门——注解之@Test
来源:互联网 发布:300533冰川网络 编辑:程序博客网 时间:2024/06/07 05:35
TestNg提供的最基本的注解之一就是Test注解,作用在方法或者类上,此注解支持的属性有:
1、alwaysRun:提供一个false or true值,如果设置为true,则被标记的方法会永远被执行,即使被标记方法所依赖的方法执行失败了。
2、dataProvider:此属性的值为标记方法提供数据驱动的数据源
3、dataProviderClass:此属性指出提供数据驱动方法的所在类
4、dependsOnGroups:此属性指出标记方法所依赖的组
5、dependsOnMethods:此属性支持标记方法所依赖的方法
6、description:标记方法的描述信息
7、enabled:标记方法是否要执行,默认为true执行
8、expectedExceptions:指定标记方法返回的异常信息列表
9、groups:指定标记方法归属于哪个组
10、timeOut:指定标记方法超时时长 (in millisecs)
11、invocationCount:被标记的方法会执行多次
12、threadPoolSize:启用多个线程执行被标记的方法,一般会与invocationCount一起使用
下面举几个常用的例子
1、enabled的使用例子,因方法testMethodTwo中enabled=false所以此方法在执行的时候会被忽略
package test;import org.testng.annotations.Test;public class DisableTestClass {@Test(enabled=true)public void testMethodOne(){System.out.println("Test method one.");}@Test(enabled=false)public void testMethodTwo(){System.out.println("Test method two.");}@Testpublic void testMethodThree(){System.out.println("Test method three.");}}
2、expectedExceptions, 方法exceptionTestTwo 返回的异常类与期望的异常类不一致,方法exceptionTestThree返回的异常信息与期望的异常信息不一致所有这两个方法会执行报错
package test;import java.io.IOException;import org.testng.annotations.Test;public class ExceptionTest {@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();}@Test(expectedExceptions={IOException.class}, expectedExceptionsMessageRegExp="Pass Message test")public void exceptionTestThree() throws Exception{throw new IOException("Pass Message test1");}@Test(expectedExceptions={IOException.class},expectedExceptionsMessageRegExp=".*")public void exceptionTestFoure() throws Exception{throw new IOException("Pass Message test1");}}
3、timeOut:首先在testng.xml中配置超时时间为500毫秒,也可直接在java代码中配置超时时间@Test(timeOut=500),xml中的配置信息如下:
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" ><suite name="Time Test Suite" time-out="500" verbose="1"><test name="Time Test"><classes><class name="test.TimeSuite" /></classes></test></suite>
然后在TimeSuite.java文件中添加如下代码,其中 timeTestOne因未在配置时间内完成,所以会执行不通过
package test;import org.testng.annotations.Test;public class TimeSuite {@Testpublic void timeTestOne() throws InterruptedException{Thread.sleep(1000);System.out.println("Time test method one");}@Testpublic void timeTestTwo() throws InterruptedException{Thread.sleep(400); ;}}
groups, dependsOnMethods, dependsOnGroups:
package test;import org.testng.annotations.Test;public class DependencyTest {//方法依赖@Test(dependsOnMethods={"testTwoMethod"})public void testOneMethod(){System.out.println("Test One Method");}//组依赖@Test(groups={"group-one"},dependsOnGroups={"group-two"})public void testTwoMethod(){System.out.println("Test Two Method");}@Test(groups={"group-two"})public void testThreeMethod(){System.out.println("Test Three Method");}//依赖组支持正则匹配@Test(dependsOnGroups={".*up-th.*"})public void testFourMethod(){System.out.println("Test Four Method");}@Test(groups={"group-three"})public void testFiveMethod(){System.out.println("Test Five Method");}}依赖除了在代码中利用属性字段dependsOnGroups来标注外,也可以在testng.xml中配置如下:
<suite name="Regexpxmldependency Suite" verbose="1"><test name="Regexp xml dependency Test"><groups><dependencies><group name="test" depends-on="starts-with.*" /></dependencies><run><include name="test" /></run></groups><classes><class name="test.depends.xml.RegularExpressionXmlTest" /></classes></test></suite>
invocationCount, threadPoolSize的使用方法的举例
package test;import org.testng.annotations.Test;public class ThreadPool {@Test(invocationCount = 100, threadPoolSize=100)public void ThreadTest() throws InterruptedException{System.out.println("Thread.currentThread() is:" + Thread.currentThread().getId());Thread.sleep(1000);}}
- TestNG入门——注解之@Test
- TestNG入门——注解之Before/After
- TestNG入门——@DataProvider
- 自动化测试框架selenium+java+TestNG——TestNG注解、执行、测试结果和测试报告 之二
- testng翻译之二--注解
- Jtester之TestNG常用注解
- Jtester之TestNG:@Factory注解
- Jtester之TestNG:@Listener注解
- TestNG入门指导——安装篇
- appium(java)——TestNG的安装与注解
- TestNG入门笔记[4]: testng.xml 执行case —— 参数的传递
- TestNG入门笔记[2] : 简介 & 基本注解 & 举例
- testng翻译之五--Test methods, Test classes and Test groups
- TestNG入门指导——include/exclude中的正则表达式
- TestNG入门
- TestNG 入门
- TestNG 入门
- test notation from testNG
- java.lang.ClassCastException: oracle.sql.CLOB cannot be cast to oracle.sql.CLOB
- Lambda表达式
- MySQL数据库多表查询
- 优化UITableViewCell高度计算的那些事
- Keil代码整体偏移和查找功能
- TestNG入门——注解之@Test
- LeetCode 6. ZigZag Conversion(Python)兼翻译
- oracle导入空间数据
- SELinux/SEAndroid 实例简述(一) 基础概念
- Direct3D---纹理映射
- nodejs+bigpipe+angularjs技术实现
- hdu 2008 数值统计
- 金额验证,小数点和数字可以输入,其它不能操作
- 学会如何调试程序bug