allure与junit结合生成漂亮的demo
1.allure安装
环境配置可参考https://blog.csdn.net/huggh/article/details/90905845,且博客中也分享了官网的案例https://github.com/allure-examples
2.案例demo
创建的maven项目,结构如下
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文件
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,生成报告,自动打开数据报告
以下部分页面结果预览:
作者:做梦的人(小姐姐) 出处:https://www.cnblogs.com/chongyou/ 本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。 如果文中有什么错误,欢迎指出。以免更多的人被误导。 微信号:18582559217 |