TestNG - 基本註解 - 變換器



在某些情況下,您可能希望根據在測試執行過程中動態評估的某些條件或標準來執行 TestNG 測試。例如:

  • 啟用或停用測試

  • 在執行時新增資料提供者

為了實現這一點,您需要使用註解變換器。註解變換器是一個實現以下介面的類:

  public interface IAnnotationTransformer {

  /**
   * This method will be invoked by TestNG to give you a chance
   * to modify a TestNG annotation read from your test classes.
   * You can change the values you need by calling any of the
   * setters on the ITest interface.
   *
   * Note that only one of the three parameters testClass,
   * testConstructor and testMethod will be non-null.
   *
   * @param annotation The annotation that was read from your
   * test class.
   * @param testClass If the annotation was found on a class, this
   * parameter represents this class (null otherwise).
   * @param testConstructor If the annotation was found on a constructor,
   * this parameter represents this constructor (null otherwise).
   * @param testMethod If the annotation was found on a method,
   * this parameter represents this method (null otherwise).
   */
  public void transform(ITest annotation, Class testClass,
      Constructor testConstructor, Method testMethod);
  }

您可以在命令列或使用 ant 指定此類,如下所示:

  java org.testng.TestNG -listener TestTransformer testng.xml

或者以程式設計方式,如下所示:

  TestNG test = new TestNG();
  test.setAnnotationTransformer(new TestTransformer());
  // ...

此介面 *IAnnotationTransformer* 在執行時修改預設的 TestNG 測試行為。使用此監聽器,我們可以透過呼叫其setter方法來修改@Test註解中定義的所有屬性的值。

建立類

讓我們在下面的示例中看看註解變換器的用法。讓我們跳過標記到特定組的測試,而無需每次都更改 TestNG 套件。建立一個要測試的 Java 類,例如,在 **/work/testng/src** 中的 **ListenerTest.java**。

  import org.testng.annotations.Test;

  public class ListenerTest {
  	@Test(groups={"betaTest","aplhaTest"})
  	public void test1() {
  		System.out.println("I am test1");
  	}

  	@Test(groups={"aplhaTest"})
  	public void test2() {
  		System.out.println("I am test2");
  	}
  }

建立變換器類案例類

  • 建立一個 Java 測試類,例如,在 **/work/testng/src** 中的 **TestTransformer.java**(實現 IAnnotationTransformer)。

  • 重寫方法 transform()。

  • 向方法 test1() 和 test2() 添加註解 @Test 並對其進行分組。

  • 新增邏輯以跳過具有組名 *betaTest* 的測試。

以下是 *TestTransformer.java* 的內容。

  import java.lang.reflect.Constructor;
  import java.lang.reflect.Method;
  import java.util.Arrays;
  import java.util.List;

  import org.testng.IAnnotationTransformer;
  import org.testng.annotations.ITestAnnotation;

  public class TestTransformer implements IAnnotationTransformer{
  	@Override
  	public void transform(ITestAnnotation annotation, Class testClass,
  			Constructor testConstructor, Method testMethod) {

  		List groupNames = Arrays.asList(annotation.getGroups());
  		System.out.println(groupNames.toString());
  		//Value 'betaTest' can be read from many places like properties file, run time parameter etc...
  		//For Simplicity, group is hardcoded in this program

  		String groupNameToSkip = "betaTest";

  		if(groupNames.contains(groupNameToSkip)){
  			System.out.println("found group name");
  			annotation.setEnabled(false);
  		}
  	}
  }

建立 testng.xml

接下來,讓我們在 **/work/testng/src** 中建立 testng.xml 檔案來執行測試用例。

  <suite name="Suite" parallel="classes" thread-count="2">
  <listeners>
    <listener class-name="TestTransformer"></listener>
  </listeners>
  <test name="Test">
    <classes>
      <class name="ListenerTest"/>
    </classes>
  </test>
</suite>

使用 javac 編譯測試用例。

/work/testng/src$ javac TestTransformer.java ListenerTest.java

現在,執行 testng.xml,它將執行在 <test> 標籤中定義的測試用例。正如您所看到的,分組在 *betaTest* 下的測試被跳過了。

/work/testng/src$ java org.testng.TestNG testng.xml

驗證輸出。

  [aplhaTest]
  [betaTest, aplhaTest]
  found group name
  I am test2

  ===============================================
  Suite
  Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
  ===============================================
廣告
© . All rights reserved.