目录
一、概述
二、@Test注解常用参数
三、测试中常用的断言(assert)
四、TestNG常用注解及使用
五、配置文件xml常用标签
六、参数传递
七、测试报告
一、概述
1、TestNG是一个开源自动化测试框架,其灵感来自JUnit和NUnit,TestNG还涵盖了整个核心的JUnit4功能,但引入了一些新的功能,使其功能更强大,使用更方便。
优势:支持依赖测试方法,并行测试,负载测试,局部故障;灵活的插件API;支持多线程测试;
2、Maven依赖
<dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>6.8.7</version> <scope>test</scope> </dependency>
注:Eclicpe上想要直接运行还需要安装testng插件
3、helloword
(1)被测试方法类HelloWrold:
package study.testng; public class HelloWorld { public String hello(){ return "hello world !"; } }
(2)测试类TestHelloWorld:
package study.testng; import org.testng.Assert; import org.testng.annotations.Test; public class TestHelloWorld { //测试返回结果不为空 @Test public void tester1(){ HelloWorld hello = new HelloWorld(); String helloworld = hello.hello(); Assert.assertNotNull(helloworld); } //测试返回结果为”hello world !“字符串 @Test public void tester2(){ HelloWorld hello = new HelloWorld(); String helloworld = hello.hello(); System.out.println(helloworld); Assert.assertEquals(helloworld, "hello world !"); } }
(3)运行测试
(4)测试结果
二、@Test注解常用参数
1、测试方法是否执行enable
默认是true,如果设置为false,则在运行时不会执行这个测试方法;
2、预期异常expectedExeption
@Test(expectedExceptions = ClassName.class)
如果ClassName类抛出了异常,测算测试通过,没有异常算测试不通过;
expectedExceptions的值也可以是一个数组:
@Test(expectedExceptions = {ClassName.class, ClassName2.class,... })
3、超时timeOut
单位为毫秒,如果测试方法运行时间超这个值算测试不通过;
4、分组groups
(1)把在一个<test>标签内的中所有类方法再进行组,在运行时,一个组的方法会一起运行,然后再运行下一个组的方法;
(2)分组的最小维度为方法,当把带分组的@Test(groups = ”groupName”)注解对类使用时,这个测试类中的所有方法都属于这同一个组;
(3)一个方法也可以同时属于多个组,@Test(groups = {“groupName1”, “groupName2”}),那么每组运行时这个方法都会被执行一次;
(4)同一个组里的方法类,如果分别属于两个不同的测试用例(<test>)内,那么它们其实可以算两个组,它们会在每个测试用例分别运行,而不会合在一起运行;
5、依赖方法dependsOnMethods
在被依赖的方法运行完成之后运行当前方法,如果依赖方法测试不通过,那么当前方法也不会继续运行了;依赖的方法可以有多个;
例:@Test(dependsOnMethods = { "methodName1" , “methodName2” })
6、依赖组,dependsOnGroups
和依赖方法类似,在被依赖组运行完成之后运行当前组,如果依赖组中的方法没有测试能过,那么当前的方法也不会继续运行了;依赖组可以有多个;
三、测试中常用的断言(assert)
1 assertEqual ([String message], expected value, actual value) 断言两个值相等。值可能是类型有 int, short, long, byte, char or java.lang.Object. 第一个参数是一个可选的字符串消息; 2 assertTrue([String message], boolean condition) 断言一个条件为真; 3 assertFalse([String message],boolean condition) 断言一个条件为假; 4 assertNotNull([String message], java.lang.Object object) 断言一个对象不为空(null); 5 assertNull([String message], java.lang.Object object) 断言一个对象为空(null); 6 assertSame([String message], java.lang.Object expected, java.lang.Object actual) 断言两个对象引用相同的对象; 7 assertNotSame([String message], java.lang.Object unexpected, java.lang.Object actual) 断言两个对象不是引用同一个对象; 8 assertArrayEquals([String message], expectedArray, resultArray) 断言预期数组和结果数组相等。数组的类型可能是 int, long, short, char, byte or java.lang.Object.;
四、TestNG常用注解及使用
@BeforeSuite 在该套件的所有测试都运行在注释的方法之前,仅运行一次(套件测试是一起运行的多个测试类)。 @AfterSuite 在该套件的所有测试都运行在注释方法之后,仅运行一次。 @BeforeClass 在调用当前类的第一个测试方法之前运行,注释方法仅运行一次。 @AfterClass 在调用当前类的第一个测试方法之后运行,注释方法仅运行一次 @BeforeTest 注释的方法将在属于<test>标签内的类的所有测试方法运行之前运行。 @AfterTest 注释的方法将在属于<test>标签内的类的所有测试方法运行之后运行。 @BeforeGroups 配置方法将在之前运行组列表。 此方法保证在调用属于这些组中的任何一个的第一个测试方法之前不久运行。 @AfterGroups 此配置方法将在之后运行组列表。该方法保证在调用属于任何这些组的最后一个测试方法之后不久运行。 @BeforeMethod 注释方法将在每个测试方法之前运行。 @AfterMethod 注释方法将在每个测试方法之后运行。 @Parameters 描述如何将参数传递给@Test方法。 @DataProvider 标记一种方法来提供测试方法的数据。 注释方法必须返回一个Object [] [],其中每个Object []可以被分配给测试方法的参数列表。 要从该DataProvider接收数据的@Test方法需要使用与此注释名称相等的dataProvider名称。 @Factory 将一个方法标记为工厂,返回TestNG将被用作测试类的对象。 该方法必须返回Object []。 @Listeners 定义测试类上的侦听器。 @Test 将类或方法标记为测试的一部分。
例:
1、增加一个测试类TestConfig
package study.testng; import org.testng.annotations.AfterGroups; import org.testng.annotations.AfterMethod; import org.testng.annotations.AfterSuite; import org.testng.annotations.AfterTest; 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 TestConfig { @BeforeSuite public void beforeSuite() { System.out.println("测试套件(当前xml中<suite>标签)之前运行@BeforeSuite--------------------"); } @AfterSuite public void afterSuite() { System.out.println("测试套件(当前xml中<suite>标签)之后运行@AfterSuite--------------------\n"); } @BeforeTest public void beforeTest() { System.out.println("测试用例(当前xml中<test>标签)之前运行@BeforeTest----------"); } @AfterTest public void afterTest() { System.out.println("测试用例(当前xml中<test>标签)之后运行@AfterTest----------\n"); } @BeforeMethod public void beforeMethod() { System.out.println("当前类每个测试方法(@Test)之前运行@BeforeMethod"); } @AfterMethod public void AfterMethod(){ System.out.println("当前类每个测试方法(@Test)之后运行@AfterMethod"); } @BeforeGroups(value="group1") public void beforeGroups(){ System.out.println("配置组配group1之前运行@BeforeGroups.................."); } @AfterGroups(value="group1") public void afterGroups(){ System.out.println("配置组配group1之前运行@AfterGroups.................."); } @Test public void test1(){ System.out.println("runnig TestConfig.test1()"); } @Test(groups = "group1") public void test2(){ System.out.println("runnig TestConfig.test2()"); } @Test(groups = "group1") public void test3(){ System.out.println("runnig TestConfig.test3()"); } }