• allure与junit结合生成漂亮的demo


    1.allure安装

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

    2.案例demo

    创建的maven项目,结构如下

     allure注解基本用法

    /*  @Step:测试步骤动作,放在具体业务逻辑方法中,可以放在关键步骤中,在报告中显示;
    " + 
                "* @Attachments:附件信息,在截图或者其他方法上加上该注解即可(注意图片和文字区别),https://github.com/allure-framework/allure1/wiki/Attachments
    " + 
                "* @Features:将case分类到某个feature中,报告中behaviore中显示,可以理解为testsuite,用于组织管理测试用例https://github.com/allure-framework/allure1/wiki/Features-and-Stories
    " + 
                "* @Store:属于feature之下的结构,报告中features中显示,可以理解为testcase,说明此用例是某个feature中的某个story下的用例https://github.com/allure-framework/allure1/wiki/Features-and-Stories
    " + 
                "* @Title: 测试用例的标题,报告中stories信息中展示
    " + 
                "* @Description: 测试用例的描述,报告中stories信息中展示
    " + 
                "* @Issue: 跟测试用例相关的bug Id(这是一个链接,可以配置bug管理系统的URL,直接跳转到bug管理系统中)https://github.com/allure-framework/allure1/wiki/Issues。pom文件中添加配置patterm,见下方
    " + 
                "* @TestCaseId:测试用例的id(这是一个连接,可以配置用例管理系统的URL,直接跳转到用例管理系统中)https://github.com/allure-framework/allure1/wiki/Test-Case-ID,pom文件中添加配置patterm,见下方
    " + 
                "* ……
    " + 
                */

    TestJunit2.java文件

    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文件

    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,生成报告,自动打开数据报告

    以下部分页面结果预览:

     

     

  • 相关阅读:
    js 对象数组 排序
    sql 时间条件查询
    idea和Pycharm 等系列产品激活激活方法和激活码 100 年
    开源协议简介
    面试题
    VIM|基础命令
    git|基础命令
    VIM|复制
    lua|基础教程
    Printf格式输出详解
  • 原文地址:https://www.cnblogs.com/chongyou/p/11767721.html
Copyright © 2020-2023  润新知