如何編寫可以獲取正在執行測試方法名稱的 TestNG Listener?


一個 TestNG 類可以包含不同的測試,例如 test1、test2、test3 等,並且這些測試可以根據不同的組進行分組,例如單元測試、整合測試或兩者兼而有之,或者任何錯誤編號。使用者可能希望根據組執行 @Test 方法。並且,瞭解哪個測試方法屬於哪個組始終很方便,以便可以將這些詳細資訊包含到報告中。

TestNG 支援多種方法在執行時獲取測試名稱,例如注入依賴項和 Listener,其中 Listener 最受歡迎。即使在 Listener 中,也可以使用 ITestListener 或 IInvokedMethodListener 來實現。

在本文中,讓我們分析如何使用 IInvokedMethodListener 獲取組名稱。

解決此問題的方法/演算法

  • 步驟 1:匯入 org.testng.annotations.Test 用於 TestNG。

  • 步驟 2:在 NewTest 類中編寫一個註釋作為 @test

  • 步驟 3:為 @Test 註釋建立一個方法,例如 test1,並新增不同的組名稱,如程式部分所示

  • 步驟 4:對 test2 和 test3 重複這些步驟以獲取多個組名稱。

  • 步驟 5:現在,建立一個使用 IInvokedMethodListener 在 beforeInvocation 和 afterInvocation 方法中獲取測試名稱的 ListenerClass。

    以下程式碼應該會獲取 @Test 方法的所有組名稱

    method.getTestMethod().getMethodName()

  • 步驟 6:現在建立 testNG.xml 並新增 Listener 類。

  • 步驟 7:現在,執行 testNG.xml 或直接在 IDE 中執行 TestNG 類,或者使用命令列編譯並執行它。

示例

以下程式碼用於建立 TestNG 類並顯示 Listener 功能

src/NewTest.java

import org.testng.annotations.Test;
public class NewTest {
    @Test(groups = { "unit", "integration" })
    public void testCase1() {
        System.out.println("in test case 1 of NewTest");
    }
    @Test(groups = { "integration" })
    public void testCase2() {
        System.out.println("in test case 2 of NewTest");
    }
    @Test(groups = { "unit" })
    public void testCase3() {
        System.out.println("in test case 3 of NewTest");

    }
}

src/ListenerClass.java

import org.testng.IInvokedMethod;
import org.testng.IInvokedMethodListener;
import org.testng.ITestResult;

import java.util.Arrays;

public class ListenerClass implements IInvokedMethodListener {

    @Override
    public void afterInvocation(IInvokedMethod method, ITestResult result) {
        System.out.println("This method is invoked after every config method - " + method.getTestMethod().getMethodName());
        System.out.println("This method is invoked after every config method to get groups name- " + Arrays.toString(method.getTestMethod().getGroups()));

    }

    @Override
    public void beforeInvocation(IInvokedMethod method, ITestResult result) {
        System.out.println("This method is invoked before every config method to get test name- " + Arrays.toString(method.getTestMethod().getMethodName()));

    }
}

testng.xml

這是一個用於組織和執行 TestNG 測試用例的配置檔案。

當只需要執行有限的測試而不是完整的套件時,它非常方便。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Parent_Suite">
    <listeners>
        <listener class-name="ListenerClass"/>
    </listeners>
    <test name="group test">
        <classes>
            <class name="NewTest" />
        </classes>
    </test>
</suite>

輸出

This method is invoked before every config method to get test name- testCase1
in test case 1 of NewTest
This method is invoked after every config method - testCase1
This method is invoked after every config method to get groups name- [unit, integration]
This method is invoked before every config method to get test name- testCase2
in test case 2 of NewTest
This method is invoked after every config method - testCase2
This method is invoked after every config method to get groups name- [integration]
This method is invoked before every config method to get test name- testCase3
in test case 3 of NewTest
This method is invoked after every config method - testCase3
This method is invoked after every config method to get groups name- [unit]

===============================================
Parent_Suite
Total tests run: 3, Passes: 3, Failures: 0, Skips: 0
===============================================

更新於: 2023-08-18

176 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告