走進Java接口測試之服務端測試報告Klov ExtentReports

引言

在上文走進Java接口測試之測試報告ExtentReport中我們已經知道 ExtentReport 可以爲接口測試提供了出色的可視化報告。而 Klov 是Extent Framework 的新的報表服務器。Klov 提供了對最新版本的詳細分析,能夠利用歷史數據分析接口測試的執行情況。本文並沒有詳細介紹 TestNG 和其他配置,詳細配置請參照上文。

ps:在 ExtentReports 4.0版本中 extentx 已被廢棄。
在這裏插入圖片描述

功能簡介

官方已經在 Heroku上使用模擬數據創建了一個demo。
注意:demo上的某些功能已禁用,以防止數據被修改。
地址:http://klov.herokuapp.com/

目前可以使用以下功能:

  • Login
  • Project
  • Dashboard
  • Build list
  • Build
  • Error States
  • Test
  • Tag Overview (pro版)
  • Tag (pro版)
  • Device (pro版)
  • Environment
  • User Settings
  • User new
  • User manage
  • Search

可以在時間線圖表中查看歷史視圖中的失敗, 可以看到執行這些測試及其構建執行信息的時間等。
在這裏插入圖片描述
然後,可以檢查哪些測試失敗最多。
在這裏插入圖片描述
最後,可以使用條形圖跟蹤狀態。
在這裏插入圖片描述

安裝配置

安裝 Mongo DB

安裝是一個簡單的過程。對於MongoDB的Windows安裝,可以參考以下:
https://docs.mongodb.com/manual/tutorial/install-mongodb-on-windows/

安裝 Redis

安裝Redis,此處省略(如果不打算使用Redis,請跳過,請參閱 “使用沒有 Redis 的” 一節)

運行 Klov

下載地址:http://extentreports.com/community/
下載 Klov-xxx.jar
有2個文件對您很重要:

  • klov-xx.jar
  • application.properties

啓動 Klov

java -jar klov-x.x.x.jar

MongoDB 設置

可以從`application.properties``以下位置配置 MongoDB 環境設置:

# data.mongodb
spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.database=klov

Redis 設置

可以從application.properties以下位置配置 Redis 服務器設置:

# redis, session
spring.session.store-type=redis
server.session.timeout=-1
spring.redis.host=localhost
spring.redis.port=6379

使用沒有 Redis 的設置

要在沒有 Redis 的情況下使用 Klov,只需在 application.properties 以下位置取消註釋:

spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.session.SessionAutoConfiguration

Email 設置

您可以從中配置電子郵件設置application.properties。需要這些設置才能重置忘記的密碼。

spring.mail.host=
spring.mail.port=
spring.mail.username=
spring.mail.password=
spring.mail.properties.mail.smtp.ssl.enable=true
#spring.mail.properties.mail.smtp.starttls.enable=true
#spring.mail.properties.mail.smtp.starttls.required=true
spring.mail.properties.mail.smtp.auth=true
#spring.mail.properties.mail.smtp.connectiontimeout=5000
#spring.mail.properties.mail.smtp.timeout=5000
#spring.mail.properties.mail.smtp.writetimeout=5000
spring.mail.test-connection=true

打開Klov

轉到 http://localhost:portNo,看到歡迎頁面。
在這裏插入圖片描述

默認使用以下超級用戶憑據:

  • user: klovadmin
  • pass: password

測試使用

引包

引入pom.xml

<dependency>
            <groupId>com.aventstack</groupId>
            <artifactId>extentreports</artifactId>
            <version>4.0.6</version>
        </dependency>

添加 Listener

首先,沒有必要更改測試代碼。需要做的就是在項目中添加一個 Listener,接上文代碼修改 `MyExtentTestNgFormatter```類

主要步驟:

  • 創建一個 KlovReporter 對象。
  • 定義 MongoDB 連接
  • 爲我們的測試項目提供項目名稱
  • 將構建號定義爲報告名稱。
  • 設置 klov 服務器 URL
  • 最後,創建一個ExtentReports對象並將其綁定到 KlovReport 對象。

通過這樣做,Klov 將創建一個具有給定名稱的項目。如果存在,Klov 將使用先前創建的項目並將結果存儲到其中。

 public MyExtentTestNgFormatter() {
        setInstance(this);
        testRunnerOutput = new ArrayList<>();
        // reportPath 報告路徑
        String reportPathStr = System.getProperty("reportPath");
        File reportPath;

        try {
            reportPath = new File(reportPathStr);
        } catch (NullPointerException e) {
            reportPath = new File(TestNG.DEFAULT_OUTPUTDIR);
        }

        if (!reportPath.exists()) {
            if (!reportPath.mkdirs()) {
                throw new RuntimeException("Failed to create output run directory");
            }
        }
        //  報告名report.html
        File reportFile = new File(reportPath, "report.html");
		//  郵件報告名emailable-report.html
        File emailReportFile = new File(reportPath, "emailable-report.html");

        //  創建一個KlovReporter對象
        ExtentKlovReporter klov = new ExtentKlovReporter();
        //  定義MongoDB連接
        klov.initMongoDbConnection("localhost", 27017);
        //  設置klov服務器URL
        klov.initKlovServerConnection("http://localhost");
        //  爲我們的測試項目提供項目名稱
        klov.setProjectName("zuozewei-test");
        klov.setReportName("1.0");

        htmlReporter = new ExtentHtmlReporter(reportFile);
        EmailReporter emailReporter = new EmailReporter(emailReportFile);
        reporter = new ExtentReports();

	//  如果cdn.rawgit.com訪問不了,可以設置爲:ResourceCDN.EXTENTREPORTS或者ResourceCDN.GITHUB
    //   htmlReporter.config().setResourceCDN(ResourceCDN.EXTENTREPORTS);

        // 綁定 Reporters
        reporter.attachReporter(htmlReporter, emailReporter,klov);
    }

注意:在ExtentReports 4.0setResourceCDN的方法已失效

運行測試

現在運行測試,看看結果。
在這裏插入圖片描述

項目代碼示例:
https://github.com/zuozewei/Java-API-Test-Examples

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章