测试: TestNg + Allure Java项目单元测试生成测试报告

TestNg + Allure 生成测试报告

完成后结果如图
测试: TestNg + Allure Java项目单元测试生成测试报告_第1张图片
测试: TestNg + Allure Java项目单元测试生成测试报告_第2张图片

介绍

  • TestNg 是基于Junit 的测试框架
  • Allure 监听测试信息,生成报告

使用

  • 因项目需要,我也是第一次编写测试用例,并生成报告,在网上找的这套方案.

TestNg

  • 项目导入依赖
    
    
        
            io.qameta.allure
            allure-testng
            2.0-BETA18
        
        
            org.testng
            testng
            6.10
        
    
  • maven 编译插件
    
            
                org.apache.maven.plugins
                maven-surefire-plugin
                2.20
                
                    true
                    
                        -javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/1.9.2/aspectjweaver-1.9.2.jar"
                    
                
                
                    
                        org.aspectj
                        aspectjweaver
                        1.9.2
                    
                
            
    
  • 编写测试用例,注意@Test 注解使用testNg 相关包
    @WebAppConfiguration()
    @RunWith(SpringRunner.class)
    @SpringBootTest
    @Slf4j
    public class TestNg extends AbstractTestNGSpringContextTests {
        @Test(description = "testNg 通过")
        public void testSuccess() {
            Assert.assertEquals(1, 1);
            System.out.println("测试通过");
        }
    }
    
    测试: TestNg + Allure Java项目单元测试生成测试报告_第3张图片

Allure 报告工具使用

  • 安装 : 需要node 环境,安装命令,allure 需要jdk 1.8及以上

    npm install -g allure-commandline --save-dev
    
  • 配置文件 allure.properties ,可以定义报告文件输出路径

    # 定义输出在项目 target 目录
    allure.results.directory=target/allure-results
    
  • allure 命令.输入 allure -h 查看

    Cai:bxadex-deal miya$ allure -help
    Could not parse arguments: Expected a command, got -help
    Usage: allure [options] [command] [command options]
    Options:
    --help
      Print commandline help.
    -q, --quiet
      Switch on the quiet mode.
      Default: false
    -v, --verbose
      Switch on the verbose mode.
      Default: false
    --version
      Print commandline version.
      Default: false
    Commands:
    generate      Generate the report
      Usage: generate [options] The directories with allure results
        Options:
          -c, --clean
            Clean Allure report directory before generating a new one.
            Default: false
          --config
            Allure commandline config path. If specified overrides values from 
            --profile and --configDirectory.
          --configDirectory
            Allure commandline configurations directory. By default uses 
            ALLURE_HOME directory.
          --profile
            Allure commandline configuration profile.
          -o, --report-dir, --output
            The directory to generate Allure report into.
            Default: allure-report
    serve      Serve the report
      Usage: serve [options] The directories with allure results
        Options:
          --config
            Allure commandline config path. If specified overrides values from 
            --profile and --configDirectory.
          --configDirectory
            Allure commandline configurations directory. By default uses 
            ALLURE_HOME directory.
          -h, --host
            This host will be used to start web server for the report.
          -p, --port
            This port will be used to start web server for the report.
            Default: 0
          --profile
            Allure commandline configuration profile.
    
    open      Open generated report
      Usage: open [options] The report directory
        Options:
          -h, --host
            This host will be used to start web server for the report.
          -p, --port
            This port will be used to start web server for the report.
            Default: 0
    
    plugin      Generate the report
      Usage: plugin [options]
        Options:
          --config
            Allure commandline config path. If specified overrides values from 
            --profile and --configDirectory.
          --configDirectory
            Allure commandline configurations directory. By default uses 
            ALLURE_HOME directory.
          --profile
            Allure commandline configuration profile.
    
    
  • 找到serve 服务相关操作, 可以使用 -p 指定端口启动,不然每次启动都是随机端口

    allure serve -p target/allure-results/
    

你可能感兴趣的:(工具,测试)