testng笔记2

1  各类@before,@after注解的例子:
    package test.beforeafter;

import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterGroups;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
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 TestClass {
/**
* Before suite method which gets executed before
* starting of any of the test in the suite.
*/
@BeforeSuite
public void beforeSuite(){
System.out.println("Before Suite method");
}

/**
* After suite method which gets executed after
* execution of all the tests in a suite.
*/
@AfterSuite
public void afterSuite(){
System.out.println("After Suite method");
}

/**
* Before Test method which gets executed before
* each test mentioned inside the 'test' tag inside a test suite.
*/
@BeforeTest
public void beforeTest(){
System.out.println("Before Test method");
}

/**
* After Test method which gets executed after
* each test mentioned inside the 'test' tag inside a test suite.
*/
@AfterTest
public void afterTest(){
System.out.println("After Test method");
}

/**
* Before Class method which gets executed before
* any of the test-methods inside a class.
*/
@BeforeClass
public void beforeClass(){
System.out.println("Before Class method");
}

/**
* After Class method which gets executed after
* all of the test-methods inside a class gets executed.
*/
@AfterClass
public void afterClass(){
System.out.println("After Class method");
}

/**
* Before group method gets executed before executing the tests
* belonging to the group as mentioned in the 'groups' attribute.
* The following method gets executed before execution of the test-method belonging to group "testOne".
*/
@BeforeGroups(groups={"testOne"})
public void beforeGroupOne(){
System.out.println("Before Group Test One method");
}

/**
* After group method gets executed after executing the tests
* belonging to the group as mentioned in the 'groups' attribute.
* The following method gets executed after execution of the test-methods belonging to group "testOne".
*/
@AfterGroups(groups={"testOne"})
public void afterGroupOne(){
System.out.println("After Group Test One method");
}

/**
* Before group method gets executed before executing the tests
* belonging to the group as mentioned in the 'groups' attribute.
* The following method gets executed before execution of the test-method belonging to group "testTwo".
*/
@BeforeGroups(groups={"testTwo"})
public void beforeGroupTwo(){
System.out.println("Before Group Test two method");
}

/**
* After group method gets executed after executing the tests
* belonging to the group as mentioned in the 'groups' attribute.
* The following method gets executed after execution of the test-methods belonging to group "testTwo".
*/
@AfterGroups(groups={"testTwo"})
public void afterGroupTwo(){
System.out.println("After Group Test two method");
}

/**
* Before method which gets executed before each test-method.
*/
@BeforeMethod
public void beforeMethod(){
System.out.println("Before Method");
}

/**
* After method which gets executed after each test-method.
*/
@AfterMethod
public void afterMethod(){
System.out.println("After Method");
}

/**
* Test-method which belongs to group "testOne".
*/
@Test(groups={"testOne"})
public void testOneMethod(){
System.out.println("Test one method");
}

/**
* Test-method which belongs to group "testTwo".
*/
@Test(groups={"testTwo"})
public void testTwoMethod(){
System.out.println("Test two method");
}
}

输出:
   
Before Suite method
Before Test method
Before Class method
Before Group Test One method
Before Method
Test one method
After Method
After Group Test One method
After Class method
After Test method
Before Test method
Before Class method
Before Group Test two method
Before Method
Test two method
After Method
After Group Test two method
After Class method
After Test method
After Suite method


2 对于注解,也是可以继承的,比如:
一个基类:
   public class BaseClass {
@BeforeClass
public void beforeBaseClass(){
System.out.println("Parent Before Class method");
}

@AfterClass
public void afterBaseClass(){
System.out.println("Parent After Class method");
}

@BeforeMethod
public void beforeBaseMethod(){
System.out.println("Parent Before method");
}

@AfterMethod
public void afterBaseMethod(){
System.out.println("Parent After method");
}



}

   一个子类继承:
       public class TestClass extends BaseClass{
@BeforeClass
public void beforeChildClass(){
System.out.println("Child Before Class method");
}

@AfterClass
public void afterChildClass(){
System.out.println("Child After Class method");
}

@BeforeMethod
public void beforeChildMethod(){
System.out.println("Child Before method");
}

@AfterMethod
public void afterChildMethod(){
System.out.println("Child After method");
}

@Test
public void testMethod(){
System.out.println("Test method under TestClass");
}
}
    输出:  E:\java资料2\TestNG Beginner's Guide\Codes-6009OS - Reviewed\Chapter 3\BeforeAfterExtend\testng.xml

Parent Before Class method
Child Before Class method
Parent Before method
Child Before method
Test method under TestClass
Child After method
Parent After method
Child After Class method
Parent After Class method


3  private等修饰的方法是不纳入测试的,即使所在类加了@Test

4 禁用某个测试
   
@Test(enabled=false)
public void testMethodTwo(){
System.out.println("Test method two.");
}

5 测试异常
   @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();
}


测试异常信息
     public class ExceptionMessageTest {
/**
* Verifies the exception message based on the exact error message thrown.
* @throws Exception
*/
@Test(expectedExceptions={IOException.class},expectedExceptionsMessageRegExp="Pass Message test")
public void exceptionMsgTestOne() throws Exception{
throw new IOException("Pass Message test");
}

/**
* Verifies the exception message using the regular exception.
* This test verifies that the exception message contains a test "Message" in it.
* @throws Exception
*/
@Test(expectedExceptions={IOException.class},expectedExceptionsMessageRegExp=".* Message .*")
public void exceptionMsgTestTwo() throws Exception{
throw new IOException("Pass Message test");
}

/**
* Verifies the exception message based on the exact error message thrown.
* This is to show that TestNg fails a test when the exception message does not match.S
* @throws Exception
*/
@Test(expectedExceptions={IOException.class},expectedExceptionsMessageRegExp="Pass Message test")
public void exceptionMsgTestThree() throws Exception{
throw new IOException("Fail Message test");
}
  输出:PASSED: exceptionMsgTestOne
PASSED: exceptionMsgTestTwo
FAILED: exceptionMsgTestThree

6 测试超时
   public class TimeSuite {
@Test
public void timeTestOne() throws InterruptedException{
Thread.sleep(1000);
System.out.println("Time test method one");
}

@Test
public void timeTestTwo() throws InterruptedException{
Thread.sleep(400);
System.out.println("Time test method two");
}
如果在testng.xml中可以定义整个suite中的方法不能超过 time-out指定的500微秒,如:
   <suite name="Time test Suite" time-out="500" verbose="1" >
  <test name="Timed Test" >
    <classes>
    <class name="test.timetest.TimeSuite" />
    </classes>
  </test>
</suite>

  也可以指定某个方法的限制时间,比如:
      @Test(timeOut=500)
public void timeTestOne() throws InterruptedException{
Thread.sleep(1000);
System.out.println("Time test method one");
}

7  输入参数
    @Parameters({ "suite-param", "test-three-param" })
@Test
public void prameterTestThree(String param, String paramTwo) {
System.out.println("Test three suite param is: " + param);
System.out.println("Test three test level param is: " + paramTwo);
}
     <test name="Parameter Test three">
<parameter name="suite-param" value="overiding suite parameter" />
<parameter name="test-three-param" value="test three parameter" />
<classes>
<class name="test.parameter.ParameterTest">
<methods>
<include name="prameterTestThree" />
</methods>
</class>
</classes>
</test>
8  可选参数
      public class OptionalTest {
@Parameters({"optional-value"})
@Test
public void optionTest(@Optional("optional value")String value){
System.out.println("This is: "+value);
}

<test name="Optional Test one">
<classes>
<class name="test.parameter.OptionalTest" />
</classes>
</test>
<test name="Optional Test two">
<parameter name="optional-value" value="passed from xml" />
<classes>
<class name="test.parameter.OptionalTest" />
</classes>
</test>
    由于在Optional Test one中,没在xml中找到parameter,所以使用@Optional中的参数了
     ,输出:
     This is: optional value
This is: passed from xml



9 dataprovider
    public class SameClassDataProvider {
@DataProvider(name = "data-provider")
public Object[][] dataProviderMethod() {
return new Object[][] { { "data one" }, { "data two" } };
}

@Test(dataProvider = "data-provider")
public void testMethod(String data) {
System.out.println("Data is: " + data);
}

  输出;Data is: data one
Data is: data two
    当然,dataprovider的类也是可以单独的,如:
        @Test(dataProvider = "data-provider",dataProviderClass=DataProviderClass.class)
public void testMethod(String data) {
System.out.println("Data is: " + data);
}
    

你可能感兴趣的:(TestNG)