suit -> class -> method,包含before和after两种形式,恰好对应各阶段的初始化(setup)和清理(teardown)java
另外test 和 groups能够定义不一样的组合,好比指定某个功能模块(package),或者以提测版本号将测试类/方法分组web
import org.testng.Assert; import org.testng.annotations.*; public class App { private void log(String action) { final String FORMATTER = "===== %-20s ====="; System.out.println(String.format(FORMATTER, action)); } @BeforeSuite public void beforeSuit() { log("before suit"); } @AfterSuite public void afterSuit() { log("after suit"); } @BeforeClass public void beforeClass() { log("before class"); } @AfterClass public void afterClass() { log("after class"); } @BeforeMethod public void beforeMethod() { log("before method"); } @AfterMethod public void afterMethod() { log("after method"); } @Test public void testAdd() { log("test add"); Assert.assertEquals(4, 1 + 3); } } --------------------------------------------------- ===== before suit ===== ===== before class ===== ===== before method ===== ===== test add ===== ===== after method ===== ===== after class ===== ===== after suit =====
另外,除了@Test注解能够出现屡次外,before/after + suit/test/class/method等也能够出现屡次,不过貌似没多大意义oracle
测试方法是否生效,默认为true;若是不但愿执行,能够置为falseapp
@Test(enabled = false)
测试方法的上游依赖,若是依赖的对象执行失败,则该测试方法不执行(skip)框架
public class App { private void log(String action) { final String FORMATTER = "===== %-20s ====="; System.out.println(String.format(FORMATTER, action)); } @Test(dependsOnMethods = {"upMethod"}) public void downMethod() { log("run down method"); Assert.assertEquals(2, 1+1); } @Test public void upMethod() { log("run up method"); Assert.assertEquals(3, 5/3); } } ------------------------------------------------ =============================================== Default Suite Total tests run: 2, Failures: 1, Skips: 1 ==============================================
Assert没法实现异常的校验,但没法避免异常的发生,或者但愿人为地抛出异常,所以须要借助此参数curl
public class App { @Test(expectedExceptions = {java.lang.ArithmeticException.class}) public void testExcption() { int a = 5 / 0; } }
方法引用的测试数据,避免同一个方法书写屡次,能够设计为数据驱动ide
public class App { @DataProvider(name = "data") public Object[][] data() { return new Object[][] { {1, 1}, {2, 4}, {3, 9}, }; } @Test(dataProvider = "data") public void testSquare(int num, int expected) { int result = num * num; Assert.assertEquals(result, expected); } }
测试方法的数据提供者,必须返回Object[][]
对象测试
包含2个参数:ui
测试方法查找指定的dataProvider时,默认是当前类及其父类url
若是数据由其余类提供,则需指定dataProviderClass,而且由@DataProvider注解的方法必须为static
public class App { @Test(dataProvider = "data", dataProviderClass = DataDriver.class) public void testSquare(int num, int expected) throws InterruptedException { int result = num * num; Assert.assertEquals(result, expected); } } class DataDriver { @DataProvider(name = "data", parallel = true) public static Object[][] data() { return new Object[][] { {1, 1}, {2, 4}, {3, 9}, }; } }
多个测试方法能够指定同一个DataProvider
DataProvider支持将Method做为第一个参数,根据不一样的方法读取不一样的数据文件,如Excel、Txt
public class App { @Test(dataProvider = "dp", dataProviderClass = DataDriver.class) public void test_1(String arg) { System.out.println("== run test-1"); Assert.assertTrue(arg != null); } @Test(dataProvider = "dp", dataProviderClass = DataDriver.class) public void test_2(String arg) { System.out.println("== run test-2"); Assert.assertTrue(arg != null); } } class DataDriver { @DataProvider(name = "dp") public static Object[][] data(Method method) { System.out.println(method.getName()); return new Object[][]{ {"java"}, }; } } ---------------------------------------------------- test_1 == run test-1 test_2 == run test-2
加载环境变量
好比在初始化时,根据提供的参数区分开发环境与测试环境
@Parameters(value = {"jdbcUrl"}) @BeforeSuite public void initSuit(String jdbcUrl) { String jdbcurl = jdbcUrl; }
那么parameter的值在哪设置呢? ——testng.xml
<suite name="Mysuite"> <parameter name="jdbcUrl" value="jdbc:oracle:thin:@oraHost:1522:orcl"/> <test name="Mytest"> ...
固然与jenkins结合会更妙
Parameters are scoped. In testng.xml, you can declare them either under a <suite> tag or under <test>. If two parameters have the same name, it's the one defined in <test> that has precedence. This is convenient if you need to specify a parameter applicable to all your tests and override its value only for certain tests.*test标签下的参数优先级高于suit
动态建立测试类
拿官网例子来说:编写一个方法实现屡次访问同一网页
// 官网使用了parameter,而后须要在testng.xml中配置多个参数值,实属麻烦 public class TestWebServer { @Test(parameters = { "number-of-times" }) public void accessPage(int numberOfTimes) { while (numberOfTimes-- > 0) { // access the web page } } }
那么使用@Factory将简化配置
public class WebTest { private int m_numberOfTimes; public WebTest(int numberOfTimes) { m_numberOfTimes = numberOfTimes; } @Test public void testServer() { for (int i = 0; i < m_numberOfTimes; i++) { // access the web page } } } ----------------------------------------------------- public class WebTestFactory { @Factory public Object[] createInstances() { Object[] result = new Object[10]; for (int i = 0; i < 10; i++) { result[i] = new WebTest(i * 10); } return result; } }
经过@Factory动态建立测试类,返回Object[]
对象 —— 包含一组测试类实例,框架会自动运行每一个实例下的测试方法
不过,虽然简化了配置,但并非指不用配置,须要在testng.xml中指定工厂类
<suite name="Suite1" verbose="1" > <test name="Test1"> <classes> <class name="WebTestFactory" /> <class name="WebTest" /> </classes> </test> </suite>
可是细想一下,测试方法的参数化能够经过@DataProvider实现
我理解的两者区别以下:
应用于测试类,忽略该类下全部的测试方法
至关于@Test(enabled=false),不过是批量的