allure與junit結合生成漂亮的demo

1.allure安裝

環境配置可參考https://blog.csdn.net/huggh/article/details/90905845,且博客中也分享了官網的案例https://github.com/allure-examplesjava

2.案例demo

建立的maven項目,結構以下git

 

 

 allure註解基本用法

/*  @Step:測試步驟動做,放在具體業務邏輯方法中,能夠放在關鍵步驟中,在報告中顯示;\r\n" + 
            "* @Attachments:附件信息,在截圖或者其餘方法上加上該註解便可(注意圖片和文字區別),https://github.com/allure-framework/allure1/wiki/Attachments\r\n" + 
            "* @Features:將case分類到某個feature中,報告中behaviore中顯示,能夠理解爲testsuite,用於組織管理測試用例https://github.com/allure-framework/allure1/wiki/Features-and-Stories\r\n" + 
            "* @Store:屬於feature之下的結構,報告中features中顯示,能夠理解爲testcase,說明此用例是某個feature中的某個story下的用例https://github.com/allure-framework/allure1/wiki/Features-and-Stories\r\n" + 
            "* @Title: 測試用例的標題,報告中stories信息中展現\r\n" + 
            "* @Description: 測試用例的描述,報告中stories信息中展現\r\n" + 
            "* @Issue: 跟測試用例相關的bug Id(這是一個連接,能夠配置bug管理系統的URL,直接跳轉到bug管理系統中)https://github.com/allure-framework/allure1/wiki/Issues。pom文件中添加配置patterm,見下方\r\n" + 
            "* @TestCaseId:測試用例的id(這是一個鏈接,能夠配置用例管理系統的URL,直接跳轉到用例管理系統中)https://github.com/allure-framework/allure1/wiki/Test-Case-ID,pom文件中添加配置patterm,見下方\r\n" + 
            "* ……\r\n" + 
            */

TestJunit2.java文件github

package my.jiguang.tests;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import io.github.bonigarcia.wdm.ChromeDriverManager;
import io.qameta.allure.Attachment;
import io.qameta.allure.Description;
import io.qameta.allure.Epic;
import io.qameta.allure.Feature;
import io.qameta.allure.Issue;
import io.qameta.allure.Step;


@Epic("極光瀏覽器打開操做")
@Feature("對極光首頁進行截圖")
public class JunitdemoTest {
    static WebDriver driver;
    
    @BeforeClass
    public static  void setUp() throws Exception {
        ChromeDriverManager.getInstance().setup();
        driver=new ChromeDriver();         
    }
    
    @Test
    @Issue("open-1")
    @Description("打開瀏覽器,獲取項目的title,對比結果")
    public void openhome() {
        driver.get("https://www.jiguang.cn/");
        String title=driver.getTitle();
        System.out.println(title);
        assertEquals("首頁 - 極光",title);
        makeScreenShot();
        driver.close();
    }
      @Attachment
       @Step("進行截圖")
       public byte[] makeScreenShot() {
            return ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
       }
    
      @Attachment
        public String makeAttach() {
            return "yeah, 2 is 2";
        }

        @Test
        public void simpleTestWithAttachments() throws Exception {
            assertThat(2, is(2));
            makeAttach();
        }
        @Description("Test shows CSV attachment")
        @Test
        public void csvAttachmentTest() throws Exception {
            saveCsvAttachment();
        }
        
        @Issue("123")
        @Attachment(value = "Sample csv attachment", type = "text/csv")
        public byte[] saveCsvAttachment() throws URISyntaxException, IOException {
            URL resource = getClass().getClassLoader().getResource("sample.csv");
            if (resource == null) {
                fail("不能打開資源文件 'sample.csv'");
            }
            return Files.readAllBytes(Paths.get(resource.toURI()));
        }
}

JunitDemo.class文件chrome

package my.jiguang.tests;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import io.github.bonigarcia.wdm.ChromeDriverManager;
import io.qameta.allure.Attachment;
import io.qameta.allure.Description;
import io.qameta.allure.Epic;
import io.qameta.allure.Feature;
import io.qameta.allure.Issue;
import io.qameta.allure.Step;


@Epic("極光瀏覽器打開操做")
@Feature("對極光首頁進行截圖")
public class JunitdemoTest {
    static WebDriver driver;
    
    @BeforeClass
    public static  void setUp() throws Exception {
        ChromeDriverManager.getInstance().setup();
        driver=new ChromeDriver();         
    }
    
    @Test
    @Issue("open-1")
    @Description("打開瀏覽器,獲取項目的title,對比結果")
    public void openhome() {
        driver.get("https://www.jiguang.cn/");
        String title=driver.getTitle();
        System.out.println(title);
        assertEquals("首頁 - 極光",title);
        makeScreenShot();
        driver.close();
    }
      @Attachment
       @Step("進行截圖")
       public byte[] makeScreenShot() {
            return ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
       }
    
      @Attachment
        public String makeAttach() {
            return "yeah, 2 is 2";
        }

        @Test
        public void simpleTestWithAttachments() throws Exception {
            assertThat(2, is(2));
            makeAttach();
        }
        @Description("Test shows CSV attachment")
        @Test
        public void csvAttachmentTest() throws Exception {
            saveCsvAttachment();
        }
        
        @Issue("123")
        @Attachment(value = "Sample csv attachment", type = "text/csv")
        public byte[] saveCsvAttachment() throws URISyntaxException, IOException {
            URL resource = getClass().getClassLoader().getResource("sample.csv");
            if (resource == null) {
                fail("不能打開資源文件 'sample.csv'");
            }
            return Files.readAllBytes(Paths.get(resource.toURI()));
        }
}

使用 mvn clean test 進行編譯,兩個類文件編譯成功瀏覽器

 

 使用:allure serve target/allure-results,生成報告,自動打開數據報告maven

 

 

如下部分頁面結果預覽:測試

 

 

 

 

相關文章
相關標籤/搜索