如何自定义gradle android test html报告

时间:2015-04-02 03:43:44

标签: android junit gradle android-gradle

我正在为Android应用程序开发验收测试。 android gradle插件为测试生成了漂亮的html报告。现在我想自定义报告,将屏幕截图链接嵌入到失败测试用例中,如下所示:

enter image description here

这是我目前的解决方案:

在生成报告后按adb拉取屏幕截图并装饰html报告

task fetchScreenshots(type: Exec) {  
    executable "adb"
    args "pull", 
        "/sdcard/Robotium-Screenshots/",   
        "${buildDir}/outputs/reports/androidTests/connected/Robotium-Screenshots/"
}

task decoratingAndroidTestsHtmlReport << {
    def screenshots = fileTree("${buildDir}/outputs/reports/androidTests/connected/Robotium-Screenshots")

    screenshots.each { screenshot ->

        def testName = screenshot.name.substring(0, screenshot.name.lastIndexOf("."))
        def testClassName = testClassName(testName)
        def testMethodName = testMethodName(testName)

        def html = file("${buildDir}/outputs/reports/androidTests/connected/${testClassName}.html")
        def patternToFind = "<h3 class=\"failures\">${testMethodName}</h3>"
        def patternToReplace = "${patternToFind}<a href=\"Robotium-Screenshots/${screenshot.name}\">Screenshot</a>"

        replacePatternInFile(html){
            it.replaceAll(patternToFind, patternToReplace)
        }
    }
}

gradle.projectsEvaluated {

    decoratingAndroidTestsHtmlReport.dependsOn(fetchScreenshots)

    connectedAndroidTest {
        finalizedBy decoratingAndroidTestsHtmlReport
    }
}

这种方法有效,但我认为这不是正确的方法。我认为修改html模板而不是黑客攻击html内容会更加清晰。但我找不到开始的线索。

0 个答案:

没有答案