Allure2+Maven+Testng部署及使用詳細教程

AllureReport部署

前言:最近做自動化測試要用到AllureReport生成測試報告,網上逛了一下,發現有很多帖子,但是大家描述的都模棱兩可,沒有詳細的步驟,因此編寫此貼對部署方式進行記錄;
一、Maven&Testng在eclipse中的安裝部署不詳述,百度比較豐富;
二、Allure的安裝部署:
1、下載Allure,地址:https://bintray.com/qameta/generic/allure2
下載最新Allure版本的zip包,解壓到本地如:D:\allure-2.6.0,在系統環境中添加ALLURE_HOME=D:\allure-2.6.0,並修改path在最後添加;%ALLURE_HOME%\bin
之後打開cmd命令窗口,輸入allure --version,顯示如下結果:

allure安裝完成。

2、maven-testng-allure插件部署
在maven項目的pom.xml文件中添加如下內容

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>ChromeTest</groupId>
    <artifactId>ChromeTest</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>

        <suiteXmlFile>src/main/resources/testng.xml</suiteXmlFile>
        <webdriver.chrome>src/main/resources/chromedriver.exe</webdriver.chrome>
        <aspectj.version>1.8.10</aspectj.version>
        <allure.version>1.5.4</allure.version>
        <!--<maven.surefire.plugin.version>2.20</maven.surefire.plugin.version>-->

    </properties>

    <dependencies>

        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.8.5</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.6.0</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-support</artifactId>
            <version>2.53.1</version>
        </dependency>
        <dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>1.7.25</version>
			<scope>test</scope>
		</dependency>
			<!--allure的testng插件-->
        <dependency>
            <groupId>ru.yandex.qatools.allure</groupId>
            <artifactId>allure-testng-adaptor</artifactId>
            <version>1.5.2</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>

                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.0</version>
                <configuration>
                    <compilerVersion>1.8</compilerVersion>
                    <source>1.6</source>
                    <target>1.6</target>
                    <encoding>utf-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.20</version>
                <configuration>
                    <systemPropertyVariables>
                        <webdriver.chrome.driver>${webdriver.chrome}</webdriver.chrome.driver>
                    </systemPropertyVariables>
                    <argLine>
                        -javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar"
                    </argLine>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.aspectj</groupId>
                        <artifactId>aspectjweaver</artifactId>
                        <version>${aspectj.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>

    </build>


</project>

創建Testng class自動化測試代碼:

public class FirstTest
{
	String keyword="Hello World";
	@Features("百度搜索")
	@Stories("百度首頁")
	@Title("輸入關鍵字")
    @Test
    @Step("輸入內容")
    @Description("測試百度搜索功能")
    public void loginSalesForce() throws InterruptedException {

        WebDriver driver = new ChromeDriver();
        driver.navigate().to("https://www.baidu.com/");
        Thread.sleep(3000);
        WebElement baidu = driver.findElement(By.id("kw"));
        baidu.sendKeys("Allure Report");
        baidu.sendKeys(Keys.RETURN);

    }
}

保存後在工程目錄對應的workspace下執行:mvn clean test命令,如下圖所示:

maven test 通過後,輸入:allure serve target/allure-results,回車後結果如下:

執行成功後自動打開瀏覽器,顯示測試報告,如下圖所示:

部署方法二:

此處只是pom.xml調用的依賴不一樣,上一種方法在斷言時沒有找到更好方法
pom.xml內容

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>ChromeTest</groupId>
    <artifactId>ChromeTest</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
    	<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    	<argLine>-Dfile.encoding=UTF-8</argLine>
        <!--suiteXmlFile>src/main/resources/testng.xml</suiteXmlFile-->
        <webdriver.chrome>src/main/resources/chromedriver.exe</webdriver.chrome>
        <aspectj.version>1.8.10</aspectj.version>
        <allure.version>1.5.4</allure.version>
        <!--<11maven.surefire.plugin.version>2.20</maven.surefire.plugin.version>-->

    </properties>

    <dependencies>

        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.9.5</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.6.0</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-support</artifactId>
            <version>2.53.1</version>
        </dependency>
        <!--此處與之前方法引用的依賴不同,該依賴做斷言更簡便-->
        <dependency>
            <groupId>io.qameta.allure</groupId>
            <artifactId>allure-testng</artifactId>
            <version>2.0-BETA19</version>
            <scope>test</scope>
        </dependency>
        <dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.17</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>1.7.25</version>
			<scope>test</scope>
		</dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>

                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.0</version>
                <configuration>
                    <compilerVersion>1.8</compilerVersion>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.20</version>
                <configuration>
                    <systemPropertyVariables>
                        <webdriver.chrome.driver>${webdriver.chrome}</webdriver.chrome.driver>
                    </systemPropertyVariables>
                    <argLine>
                        -javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar"
                    </argLine>
                    <!--重要配置:生成allure-result的目錄-->
                    <systemProperties>
                        <property>
                            <name>allure.results.directory</name>
                            <value>./target/allure-results</value>
                        </property>
                    </systemProperties>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.aspectj</groupId>
                        <artifactId>aspectjweaver</artifactId>
                        <version>${aspectj.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>

    </build>


</project>

Jenkins部署allurereport插件完成集成

1、安裝Jenkins,默認安裝推薦插件
2、安裝Allure插件,如下圖所示:

3、在“系統管理”->“全局工具配置”中配置jdk、Maven和Allure工具路徑,如下圖所示:

之後點擊“保存”按鈕進行保存。

 
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章