問題描述

有沒有辦法 (輕鬆) 生成包含測試結果的 HTML 報告?我目前正在使用 JUnit,除了 Selenium 用於測試網路應用程式 UI 。

PS:鑑於專案結構我不應該使用 Ant 🙁

最佳解決方法

如果您可以使用 Ant,那麼您只需使用 JUnitReport 任務,詳細說明如下:http://ant.apache.org/manual/Tasks/junitreport.html,但您在問題中提到您不應該使用 Ant 。我相信該任務只是將 XML 報告轉換為 HTML,因此使用任何 XSLT 處理器生成類似的報告是可行的。

或者,您可以切換到使用與 JUnit 非常相似的 TestNG(http://testng.org/doc/index.html),但是具有預設的 HTML 報告以及其他幾個很酷的功能。

次佳解決方法

或者對於使用 Maven 構建工具的使用者,還有一個名為 Surefire Report 的外掛。

報告如下所示:Sample

第三種解決方法

你可以很容易地透過螞蟻做這個。這是一個 build.xml 檔案

 <project name="genTestReport" default="gen" basedir=".">
        <description>
                Generate the HTML report from JUnit XML files
        </description>

        <target name="gen">
                <property name="genReportDir" location="${basedir}/unitTestReports"/>
                <delete dir="${genReportDir}"/>
                <mkdir dir="${genReportDir}"/>
                <junitreport todir="${basedir}/unitTestReports">
                        <fileset dir="${basedir}">
                                <include name="**/TEST-*.xml"/>
                        </fileset>
                        <report format="frames" todir="${genReportDir}/html"/>
                </junitreport>
        </target>
</project>

這將找到格式為 TEST – *。 xml 的檔案,並將報告生成到名為 unitTestReports 的資料夾中。

要執行這個 (假設上面的檔案稱為 buildTestReports.xml),在終端中執行以下命令:

ant -buildfile buildTestReports.xml

第四種方法

有多個選項可用於生成 Selenium WebDriver 指令碼的 HTML 報告。

1. 使用 JUNIT TestWatcher 類建立您自己的 Selenium HTML 報告

TestWatcher JUNIT 類允許覆蓋當 JUNIT 測試失敗或透過時自動呼叫的 failed() 和 succeeded() JUNIT 方法。

TestWatcher JUNIT 類允許覆蓋以下方法:

  • protected void failed(Throwable e,Description description)

在測試失敗時呼叫 fail() 方法

  • 受保護的空白完成 (描述描述)

在測試方法完成 (無論是否透過) 時呼叫 finished() 方法

  • protected void skipped(AssumptionViolatedException e,描述描述)

由於失敗的假設跳過測試時,會呼叫 skipped() 方法。

  • protected void starting(描述描述)

在測試即將開始時呼叫 starts() 方法

  • protected void succeeded(描述描述)

successed() 方法在測試成功時被呼叫

有關此案例,請參閱以下示例程式碼:

import static org.junit.Assert.assertTrue;
import org.junit.Test;

public class TestClass2 extends WatchManClassConsole {

    @Test public void testScript1() {
        assertTrue(1 < 2); >
    }

    @Test public void testScript2() {
        assertTrue(1 > 2);
    }

    @Test public void testScript3() {
        assertTrue(1 < 2);
    }

    @Test public void testScript4() {
        assertTrue(1 > 2);
    }
}

import org.junit.Rule; 
import org.junit.rules.TestRule; 
import org.junit.rules.TestWatcher; 
import org.junit.runner.Description; 
import org.junit.runners.model.Statement; 

public class WatchManClassConsole {

    @Rule public TestRule watchman = new TestWatcher() { 

        @Override public Statement apply(Statement base, Description description) { 
            return super.apply(base, description); 
        } 

        @Override protected void succeeded(Description description) { 
            System.out.println(description.getDisplayName() + " " + "success!"); 
        } 

        @Override protected void failed(Throwable e, Description description) { 
            System.out.println(description.getDisplayName() + " " + e.getClass().getSimpleName()); 
        }
    }; 
}

2. 使用 Allure 報告框架

Allure 框架可以幫助您為 Selenium WebDriver 專案生成 HTML 報告。

報告框架非常靈活,可以與許多程式語言和單元測試框架一起使用。

您可以在 http://allure.qatools.ru/上閱讀有關它的所有內容。

您將需要將以下依賴項和外掛新增到您的 pom.xml 檔案中

  1. maven surefire

  2. aspectjweaver

  3. 誘惑介面卡

檢視更多詳細資訊,包括本文的程式碼示例:http://test-able.blogspot.com/2015/10/create-selenium-html-reports-with-allure-framework.html

第五種方法

我發現上面的答案非常有用,但並不是一般的目的,他們都需要一些其他主要的構建系統,如 Ant 或 Maven 。

我想透過一個簡單的 one-shot 命令生成一個報告,我可以從任何內容 (從構建,測試或只是自己) 呼叫,所以我建立了 junit2html,可以在這裡找到:https://github.com/inorton/junit2html

您可以透過以下方式進行安裝:

pip install junit2html

參考文獻

注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。