• Jenkins+maven+testng+htmlreport单元自动化测试


    背景说明

    为了可以在jenkins自动化运行单元测试的代码,所以使用maven+testng的技术结合,达到手动或者定时去执行单元测试的代码,以便提高人工运行的自动化的效率。单元通过该方案也可以套用在httpclient框架去执行测试web api接口的自动化测试,原理是一致的。

    环境准备

    1. 安装开发工具:eclipse开发工具
    2. 安装maven:在官方下载maven在开发环境和jenkins环境都需要安装配置,下载地址:Maven – Download Apache Mavenicon-default.png?t=N7T8https://maven.apache.org/download.cgi
    3. 安装jenkins服务:Jenkins
    4. 安装jdk1.7以上的版本:Java Downloads | Oracle

    代码结构

    配置pom.xml

    创建测试项目时,以maven类型项目创建

    1. <project xmlns="http://maven.apache.org/POM/4.0.0"
    2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    4. <modelVersion>4.0.0</modelVersion>
    5. <groupId>jenkins.testng.demo</groupId>
    6. <artifactId>TestDemo</artifactId>
    7. <version>0.0.1-SNAPSHOT</version>
    8. <packaging>jar</packaging>
    9. <name>TestDemo</name>
    10. <url>http://maven.apache.org</url>
    11. <properties>
    12. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    13. </properties>
    14. <build>
    15. <plugins>
    16. <!-- 加入maven-surefire-plugin插件用来使用maven执行用例,其中suiteXmlFile配置的就是testNG用例执行文件的地址 -->
    17. <plugin>
    18. <groupId>org.apache.maven.plugins</groupId>
    19. <artifactId>maven-surefire-plugin</artifactId>
    20. <version>3.0.0-M5</version>
    21. <configuration>
    22. <!-- 忽略测试错误,继续编译 -->
    23. <testFailureIgnore>true</testFailureIgnore>
    24. <suiteXmlFiles>
    25. <suiteXmlFile>src/main/java/jenkins/testng/demo/TestngSample/testng.xml</suiteXmlFile>
    26. </suiteXmlFiles>
    27. <!-- 加入编码设置,否则生成的报告会中文乱码 -->
    28. <argLine>-Dfile.encoding=UTF-8</argLine>
    29. </configuration>
    30. </plugin>
    31. </plugins>
    32. </build>
    33. <dependencies>
    34. <dependency>
    35. <groupId>junit</groupId>
    36. <artifactId>junit</artifactId>
    37. <version>3.8.1</version>
    38. <scope>test</scope>
    39. </dependency>
    40. <!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-surefire-plugin -->
    41. <dependency>
    42. <groupId>org.apache.maven.plugins</groupId>
    43. <artifactId>maven-surefire-plugin</artifactId>
    44. <version>3.0.0-M5</version>
    45. </dependency>
    46. <!-- https://mvnrepository.com/artifact/org.apache.maven/maven-plugin-api -->
    47. <dependency>
    48. <groupId>org.apache.maven</groupId>
    49. <artifactId>maven-plugin-api</artifactId>
    50. <version>3.6.3</version>
    51. </dependency>
    52. <!-- https://mvnrepository.com/artifact/org.apache.maven/maven-artifact -->
    53. <dependency>
    54. <groupId>org.apache.maven</groupId>
    55. <artifactId>maven-artifact</artifactId>
    56. <version>3.6.3</version>
    57. </dependency>
    58. <!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-compiler-plugin -->
    59. <dependency>
    60. <groupId>org.apache.maven.plugins</groupId>
    61. <artifactId>maven-compiler-plugin</artifactId>
    62. <version>3.8.1</version>
    63. </dependency>
    64. <!-- https://mvnrepository.com/artifact/org.apache.maven.plugin-tools/maven-plugin-annotations -->
    65. <dependency>
    66. <groupId>org.apache.maven.plugin-tools</groupId>
    67. <artifactId>maven-plugin-annotations</artifactId>
    68. <version>3.6.0</version>
    69. <scope>provided</scope>
    70. </dependency>
    71. <!-- https://mvnrepository.com/artifact/org.testng/testng -->
    72. <dependency>
    73. <groupId>org.testng</groupId>
    74. <artifactId>testng</artifactId>
    75. <version>7.1.0</version>
    76. <scope>test</scope>
    77. </dependency>
    78. <!-- https://mvnrepository.com/artifact/org.uncommons/reportng -->
    79. <dependency>
    80. <groupId>org.uncommons</groupId>
    81. <artifactId>reportng</artifactId>
    82. <version>1.1.4</version>
    83. <scope>test</scope>
    84. </dependency>
    85. </dependencies>
    86. </project>

    测试代码

    1. package jenkins.testng.demo.TestngSample;
    2. import static org.testng.Assert.assertEquals;
    3. import org.testng.annotations.Test;
    4. public class TestDemong {
    5. @Test
    6. public void demo() {
    7. assertEquals(true, true);
    8. }
    9. @Test
    10. public void demo2() {
    11. assertEquals(true, true);
    12. }
    13. }

    配置testng.xml

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
    3. <suite name="test">
    4. <test name="test">
    5. <classes>
    6. <class name="jenkins.testng.demo.TestngSample.TestDemong">
    7. </class>
    8. </classes>
    9. </test>
    10. <listeners>
    11. <listener class-name="org.uncommons.reportng.HTMLReporter" />
    12. <listener
    13. class-name="org.uncommons.reportng.JUnitXMLReporter" />
    14. </listeners>
    15. </suite>

    配置jenkins

    运行查看

  • 相关阅读:
    好的摄影师都会iPhone 8和iOS 11的这三项功能
    最新时间注入攻击和代码分析技术
    ST‐LINK V2 使用说明(安装,调试,烧录)
    Python实现MYSQL蜜罐
    Anaconda一文入门笔记
    flyway7.1.1适配人大金仓postgres版本
    多线程 - 锁策略 & CAS
    基于C++MFC的活塞环外观表面缺陷机器视觉检测系统
    [ROS]Ubuntu18.04下安装指定版本OpenCV
    RSLO:自监督激光雷达里程计(实时+高精度,ICRA2022)
  • 原文地址:https://blog.csdn.net/Chris_111X/article/details/132646342