問題描述

有沒有辦法 (輕鬆) 生成包含測試結果的 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 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。