最近看了点TestNG,做个入门笔记
0.Maven + TestNG
0a. 创建Maven 项目,pom中添加依赖(可能还需要安装TestNG插件
<dependencies> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>6.8.21</version> </dependency> <!--报告--> <dependency> <groupId>org.uncommons</groupId> <artifactId>reportng</artifactId> <version>1.1.4</version> <scope>test</scope> <exclusions> <exclusion> <groupId>org.testng</groupId> <artifactId>testng</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.google.inject</groupId> <artifactId>guice</artifactId> <version>3.0</version> <scope>test</scope> </dependency> </dependencies>
0b.项目结构
文件路径是在test目录下
1. 一个简单的case
package com.lernenTestNG; //1. 常用的import的包 import org.testng.annotations.*; import org.testng.Assert; //1. 实际的testcase为类中被@Test注释的方法 public class Lecktion1 { @BeforeTest public void y_beforeTest(){ System.out.println("**** BeforeTest ****"); } @BeforeMethod public void y_beforeMethod(){ System.out.println("**** BeforeMethod ****"); } @Test public void y_Test1() { int a = 1; int b = 2; int res = a + b; System.out.println("y_Test1()"); System.out.println("result=" + res); Assert.assertEquals(res,3); } @Test public void y_Test2() { int a = 2; int b = 3; int res = a + b; System.out.println("y_Test2()"); System.out.println("result=" + res); // 3. Assert.XXX只能在testcase中使用 Assert.assertEquals(res,5); } @Test public void y_Test3() { int a = 2; int b = 4; int res = a + b; System.out.println("y_Test3()"); System.out.println("result=" + res); Assert.assertEquals(res,5); } @AfterMethod public void y_AfterMethod(){ System.out.println("**** AfterMethod ****"); } @AfterTest public void y_AfterTest(){ System.out.println("**** AfterTest ****"); } }
2.常用注解
2a. @Test ——测试执行主体,作用域为Public
@Test 的可选参数
// 多个参数可以如下使用 // @Test(dataProvider = "",dependsOnGroups = "",groups = {"",""}) @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD, ElementType.TYPE, ElementType.CONSTRUCTOR}) public @interface Test { String[] groups() default {}; boolean enabled() default true; /** @deprecated */ @Deprecated String[] parameters() default {}; String[] dependsOnGroups() default {}; String[] dependsOnMethods() default {}; long timeOut() default 0L; long invocationTimeOut() default 0L; int invocationCount() default 1; int threadPoolSize() default 0; int successPercentage() default 100; String dataProvider() default ""; Class<?> dataProviderClass() default Object.class; boolean alwaysRun() default false; String description() default ""; Class[] expectedExceptions() default {}; String expectedExceptionsMessageRegExp() default ".*"; String suiteName() default ""; String testName() default ""; /** @deprecated */ boolean sequential() default false; boolean singleThreaded() default false; Class retryAnalyzer() default Class.class; boolean skipFailedInvocations() default false; boolean ignoreMissingDependencies() default false; int priority() default 0; }
2b. @BeforeTest、@BeforeClass、@BeforeMethod
2c. @AfterTest、@AfterClass、@AfterMethod
——测试前后setup,teardown,具体执行顺序如名称所示
2d. @DetaProvider ——数据驱动
public class Lecktion2 { //注意大小写 @Test(dataProvider = "dp") public void y_test_dp(int a, int b, int res){ System.out.println("a is "+ a +", b is " + b + ", res is " + res); Assert.assertEquals(a+b,res); } //@DataProvider只支持修饰方法,源码就是这么写的。。。这边可以改成读取文件、或是其他地方得到数据 @DataProvider public Object[][] dp(){ return new Object[][]{ {1,2,3}, {4,5,6}, {1,1,2}}; } //调用三次y_test_dp,所以y_AfterMethod也会执行三次 @AfterMethod public void y_AfterMethod(){ System.out.println("**** AfterMethod ****"); } }
2e. @Pataneters ——接受配置文件testng.xml中同名参数到测试用例中使用
引用某个触的文档:http://www.cnblogs.com/TankXiao/p/3888070.html
package TankLearn2.Learn; import org.testng.annotations.Parameters; import org.testng.annotations.Test; public class ParameterizedTest1 { @Test @Parameters("test1") public void ParaTest(String test1){ System.out.println("This is " + test1); } }
testng.xml
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > <suite name="Suite1"> <parameter name="test1" value="Tank" /> <parameter name="test1" value="Xiao" /> <test name="test12"> <classes> <class name="TankLearn2.Learn.ParameterizedTest1" /> </classes> </test> </suite>
3.testng.xml
码ing