面向百度編程之-測試報(bào)告

官方網(wǎng)站在這里:http://extentreports.relevantcodes.com/
當(dāng)年github上開源,如今pro版本居然收錢了
先說總結(jié):

1. 生成的報(bào)告簡潔美觀,

2.生成的單html方便jenkins集成發(fā)郵件

3.自帶集中展示歷史報(bào)告的服務(wù)端

4.支持java,

5.可定制報(bào)告

6.數(shù)據(jù)入庫

官網(wǎng)提供V2.x版本和V3.x版本,只支持java8。注意

客戶端地址:https://github.com/anshooarora/extentreports-java/commits/master

服務(wù)端地址:https://github.com/anshooarora/extentx

安裝過程,官方文檔上比較消息。

官方說明在這里:http://extentreports.com/docs/versions/3/java/, 提供了3種和testng集成示例:

1.直接在@BeforeSuite@BeforeClass進(jìn)行初始化

2.自己實(shí)現(xiàn)testng的ITestListener接口,監(jiān)聽的適合你已經(jīng)有測試狂簡了,不想動(dòng)原來的case

3.自己實(shí)現(xiàn)testng的IReporter接口,更加直觀

以上隨便選擇一種都可以,實(shí)現(xiàn)ITestListener接口。內(nèi)容類似下面:

public class ExtentTestNGITestListener implements ITestListener {

    private static ExtentReports extent = ExtentManager.getInstance("test-output/extent.html");
    private static ThreadLocal test = new ThreadLocal();
    public static MacacaClient driver;

    @Override
    public synchronized void onStart(ITestContext context) {
    }

    @Override
    public synchronized void onFinish(ITestContext context) {
        extent.flush();
    }

    @Override
    public synchronized void onTestStart(ITestResult result) {
        test.set(extent.createTest(result.getMethod().getMethodName()));
    }

    @Override
    public synchronized void onTestSuccess(ITestResult result) {
        ((ExtentTest)test.get()).pass("Test passed");
    }

    @Override
    public synchronized void onTestFailure(ITestResult result) {
        ((ExtentTest)test.get()).fail(result.getThrowable());
        File directory = new File("test-output");
        try {
            String screenPath = directory.getCanonicalPath() + "/";
            File file = new File(screenPath);
            if (!file.exists()){
                file.mkdirs();
            }
            String fileName = result.getMethod().getMethodName() + ".png";
            driver.saveScreenshot(screenPath + fileName);
            ((ExtentTest)test.get()).addScreenCaptureFromPath(screenPath + fileName);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public synchronized void onTestSkipped(ITestResult result) {
        ((ExtentTest)test.get()).skip(result.getThrowable());
    }

    @Override
    public synchronized void onTestFailedButWithinSuccessPercentage(ITestResult result) {

    }
}

onTestFailure函數(shù)里面實(shí)現(xiàn)了出錯(cuò)自動(dòng)截圖,調(diào)用的是ExtentTest的addScreenCaptureFromPath方法。

ExtentManager用來做初始化:

public class ExtentManager {
    private static ExtentReports extent;

    public static ExtentReports getInstance(String filePath) {
        if (extent == null)
            createInstance(filePath);
        return extent;
    }

    public static void createInstance(String filePath) {
        extent = new ExtentReports();
        extent.setSystemInfo("os", "Linux");
        extent.attachReporter(createHtmlReporter(filePath), createExtentXReporter());
    }

    public static ExtentHtmlReporter createHtmlReporter(String filePath){
        ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter(filePath);
        //報(bào)表位置
        htmlReporter.config().setTestViewChartLocation(ChartLocation.TOP);
        //使報(bào)表上的圖表可見
        htmlReporter.config().setChartVisibilityOnOpen(true);
        htmlReporter.config().setTheme(Theme.STANDARD);
        htmlReporter.config().setDocumentTitle(filePath);
        htmlReporter.config().setEncoding("utf-8");
        htmlReporter.config().setReportName("XXX項(xiàng)目測試報(bào)告");
        return htmlReporter;
    }

    public static ExtentXReporter createExtentXReporter() {
        ExtentXReporter extentx = new ExtentXReporter("127.0.0.1",27017);
        extentx.config().setProjectName("test1");
        extentx.config().setReportName("Build-1224");
        extentx.config().setServerUrl("http://localhost:1337");
        return extentx;
    }

ExtentXReporter構(gòu)造函數(shù)里填的是mongodb的地址和端口。

在res/testng.xml里面注冊這個(gè)監(jiān)聽器,測試類也寫上:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
    <listeners>
        <listener class-name="com.util.extent.ExtentTestNGITestListener"></listener>
    </listeners>
    <test name="Test">
        <classes>
            <class name="macaca.client.ExtentTestNGReportBuilder"></class>
        </classes>
    </test>
</suite>

測試類調(diào)用,隨便寫2個(gè)case演示:macaca是阿里的自動(dòng)化框架,也不錯(cuò)的,可以直接刪除也行,內(nèi)容罷了

public class ExtentTestNGReportBuilder {
    MacacaClient driver = new MacacaClient();

    @BeforeClass
    public synchronized void beforeClass() throws Exception {
        JSONObject porps = new JSONObject();
        porps.put("platformName", "android");
        porps.put("reuse", 1);
        porps.put("app", "https://npmcdn.com/android-app-bootstrap@latest/android_app_bootstrap/build/outputs/apk/android_app_bootstrap-debug.apk");
        JSONObject desiredCapabilities = new JSONObject();
        desiredCapabilities.put("desiredCapabilities", porps);
        driver.initDriver(desiredCapabilities);
        ExtentTestNGITestListener.driver = driver;
    }

    @AfterClass
    public synchronized void afterClass() throws Exception{
        driver.quit();
    }

    @Test
    public void test_1() throws Exception{
        assert 1==0;
    }

    @Test
    public void test_2() {
        assert 1 == 1;
    }

}

運(yùn)行命令

mvn clean test

開始測試

生成的本地報(bào)告在test-output下面,內(nèi)容類似下面:

最下面可以看到失敗的截圖:


只查看失敗的case:
image

Dashboard頁面:
image

看看服務(wù)端的報(bào)告(運(yùn)行了多次):

匯總頁面:

這里的PROJECT和BUILD是ExtentManager類里面

extentx.config().setProjectName("test1");
extentx.config().setReportName("Build-1224");

這里設(shè)定的,這里實(shí)際使用時(shí)可以用jenkins集成時(shí)直接由jenkins傳進(jìn)來

具體某一次的報(bào)告:

如果覺得這種報(bào)告太笨重了,可以看下這種
http://www.itdecent.cn/p/ce09d3bd1c37
輕量級(jí)別的

ps:所有酷炫的測試報(bào)告,都不適合發(fā)郵件,因?yàn)閮?nèi)容太多了
適合發(fā)郵件的模板去掉各種渲染

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,535評(píng)論 19 139
  • 想來,你畢業(yè)也有三個(gè)多月了,當(dāng)初寫下的這段文字,再翻出來,心里還是酸酸的,總會(huì)想起你,不知道你的日子過得好不好。 ...
    洛簡紫閱讀 683評(píng)論 0 2
  • 為了遇見你 我翻過山越過嶺 見過大江穿過人海 就為了和你邂逅 你是否也在城市的另一端 和我一樣走過街道 看過華燈 ...
    李嫑笑閱讀 291評(píng)論 0 1

友情鏈接更多精彩內(nèi)容