TestNG初探
用例包准备?
import org.testng.Assert;
import org.testng.annotations.Test;
前后置用例有哪些?
package com.wj;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.AfterSuite;
public class TestngAnnotation {
// test case1
@Test
public void testCase1(){
System.out.println(" --in test case 1--");
}
// test case 2
@Test
public void testCase2(){
System.out.println(" --in test case 2--");
}
@BeforeMethod
public void beforeMethod(){
System.out.println(" 1 in beforeMethod");
}
@AfterMethod
public void afterMethod(){
System.out.println(" 1 in afterMethod");
}
@BeforeClass
public void beforeClass() {
System.out.println(" 2 in beforeClass");
}
@AfterClass
public void afterClass() {
System.out.println(" 2 in afterClass");
}
@BeforeTest
public void beforeTest() {
System.out.println(" 3 in beforeTest");
}
@AfterTest
public void afterTest() {
System.out.println(" 3 in afterTest");
}
@BeforeSuite
public void beforeSuite() {
System.out.println("4 in beforeSuite");
}
@AfterSuite
public void afterSuite() {
System.out.println("4 in afterSuite");
}
}
用例间依赖如何写?
@Test(dependsOnMethods = {"successTest"})
// 如果依赖的用例为真,才执行这个用例,否则直接跳过该用例。
@Test(enabled = false)
@Test(groups = { "init" })
public void testPrintMessage() {
System.out.println("Inside testPrintMessage()");
message = "Manisha";
Assert.assertEquals(message, messageUtil.printMessage());
}
@Test(dependsOnGroups = { "init.*" })
public void testSalutationMessage() {
System.out.println("Inside testSalutationMessage()");
message = "Hi!" + "Manisha";
Assert.assertEquals(message, messageUtil.salutationMessage());
}
// testSalutationMessage在执行前,先去执行所有的init.*的用例。
用例执行顺序?
用例的执行顺序在任何没有其他约束下,就按照用例名称才进行升序排序进行执行。
如果有约束条件,就会先执行依赖的用例,再执行当前用例。
用例参数化?
关键词:the @DataProvider passes Integer and Boolean as parameter.
1.这里的参数是一个基本类型
package com.wj.test;
import com.wj.dependency.PrimeNumberChecker;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.testng.annotations.DataProvider;
public class ParamTestWithDataProvider1 {
private PrimeNumberChecker primeNumberChecker;
@BeforeMethod
public void initalize() {
primeNumberChecker = new PrimeNumberChecker();
}
@DataProvider(name = "test1")
public static Object[][] primeNumbers() {
return new Object[][] {
{2, true}, {6, false}, {19, true}, {22, false}, {23, true}
};
}
@Test(dataProvider = "test1")
public void testPrimeNumberChecker(Integer inputNumber, Boolean expectedResult) {
System.out.println(inputNumber + " " + expectedResult);
Assert.assertEquals(expectedResult, primeNumberChecker.validate(inputNumber));
}
}
- 如果参数是一个对象又是怎样的呢?
// 定义一个对象
package com.wj.dependency;
public class Bean {
private String val;
private int i;
public Bean(String val, int i) {
this.val = val;
this.i = i;
}
public String getVal() {
return val;
}
public void setVal(String val) {
this.val = val;
}
public int getI() {
return i;
}
public void setI(int i) {
this.i = i;
}
}
// 去测试这个对象的内容
package com.wj.test;
import com.wj.dependency.Bean;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class ParamTestWithDataProvider2 {
@DataProvider(name = "test1")
public static Object[][] primeNumbers() {
return new Object[][] {{ new Bean("hi I am the bean", 111)}};
}
@Test(dataProvider = "test1")
public void testMethod(Bean myBean) {
System.out.println(myBean.getVal() + " " + myBean.getI());
}
}
TestNG也很好的集成了Junit!
package com.wj.JUnitDemo;
import org.junit.Test;
import static org.testng.AssertJUnit.assertEquals;
public class TestJunit {
@Test
public void testAdd() {
String str = "Junit testing using TestNG";
assertEquals("Junit testing using TestNG", str);
}
}
Test监听器和报告?
待补充