亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在測試執行后,如何使用 TestNg ITestResult

在測試執行后,如何使用 TestNg ITestResult

白衣染霜花 2022-09-01 16:45:14
我創建了一個實現 IReporter 接口的自定義報告器,并希望將測試輸入參數發布到最終測試報告中。我的測試輸入參數是通過TestNg Dataprovider提供的。每個輸入參數都是 TestCase 類的一個實例。我可以訪問報表中的輸入參數,但它只是對象的哈希碼,而不是實例本身,我可以從中調用所需的測試數據并發布在 html 報表中。我能夠使用以下代碼打印所有輸入參數Set<ITestResult> failedTests = testContext                    .getFailedTests()                    .getAllResults();            for (ITestResult result: failedTests) {                for (Object param: result.getParameters()) {                    System.out.println(param);                }            }Output:[email protected]@4dbb42b7getParameters() 方法返回一個對象數組,我不知道如何將其轉換為 TestCase。請建議一種方法來獲取data.service.entities.TestCase@1a1da881的實例,以便調用其方法。
查看完整描述

1 回答

?
神不在的星期二

TA貢獻1963條經驗 獲得超6個贊

為了獲取實例,我創建了 TestNg CustomListener 類,在其中我手動將輸入參數 (TestCase) 設置為每個已執行測試 ITestResult 的屬性:


    public class CustomListener extends TestListenerAdapter {


    @Override

    public void onTestFailure(ITestResult iTestResult) {

        super.onTestFailure(iTestResult);

        TestCase tCase = (TestCase) iTestResult.getParameters()[0];

        iTestResult.setAttribute("failed_case", tCase);

    }


    @Override

    public void onTestSuccess(ITestResult iTestResult) {

        super.onTestSuccess(iTestResult);

        TestCase tCase = (TestCase) iTestResult.getParameters()[0];

        iTestResult.setAttribute("passed_case", tCase);

    }


    @Override

    public void onTestSkipped(ITestResult iTestResult) {

        super.onTestSkipped(iTestResult);

        TestCase tCase = (TestCase) iTestResult.getParameters()[0];

        iTestResult.setAttribute("skipped_case", tCase);

    }

  }

在我的自定義報告類中,我獲取每個測試用例的對象,如下所示:


        TestCase failedCase = (TestCase) testResult.getAttribute("failed_case");

        TestCase passedCase = (TestCase) testResult.getAttribute("passed_case");

        TestCase skippedCase = (TestCase) testResult.getAttribute("skipped_case");


查看完整回答
反對 回復 2022-09-01
  • 1 回答
  • 0 關注
  • 165 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號