zoukankan      html  css  js  c++  java
  • 接口自动化框架(java)--1.项目概述

    项目github地址:

    https://github.com/tianchiTester/API_AutoFramework

     这套框架的报告是自己封装的

    1.测试基类TestBase:

    接口请求的testcase类需要继承此类去读取properties文件

    package com.qa.base;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.util.Properties;
     
    public class TestBase {
        //这个类作为所有接口请求的父类,加载读取properties文件
        public Properties prop;
        //构造函数
        public TestBase(){
            try{
                prop = new Properties();
                FileInputStream fis = new FileInputStream(System.getProperty("user.dir")+"/src/main/java/com/qa/config/config.properties");
                prop.load(fis);
            }catch(FileNotFoundException f){
                f.printStackTrace();
            }catch (IOException i){
                i.printStackTrace();
            }
        }
    }

    2.配置文件

    配置文件里存放项目的endpoint,可以通过修改endpoint进行环境的切换

    测试数据存放读取excel的地址

    tokenpath存放jsonpath的所在路径用于需要token才能调用的接口

    #项目的根url(endpoint)
    Host= https://xxx.com
     
    #测试数据excel地址
    postdata = xxx
    getdata = xxx
     
     
    #通过jsonpath获取返回结果token的所在路径
    tokenPath = xxx

    3.Excel文件中测试数据存放接口地址endpoint后面的url

    4.report样式,我使用的是extentreprot插件,实现方式是实现testNG的IReporter接口,再通过testng.xml中listener标签进行监听

    package com.qa.report;
     
    import com.aventstack.extentreports.ExtentReports;
    import com.aventstack.extentreports.ExtentTest;
    import com.aventstack.extentreports.ResourceCDN;
    import com.aventstack.extentreports.Status;
    import com.aventstack.extentreports.model.TestAttribute;
    import com.aventstack.extentreports.reporter.ExtentHtmlReporter;
    import com.aventstack.extentreports.reporter.configuration.ChartLocation;
    import org.testng.*;
    import org.testng.xml.XmlSuite;
     
    import java.io.File;
    import java.util.*;
     
    public class ExtentTestNGReporterListener implements IReporter{
     
        //生成的路径以及文件名
        private static final String OUTPUT_FOLDER = "test-output/";
        private static final String FILE_NAME = "index.html";
        private ExtentReports extent;
     
        @Override
        public void generateReport(List<XmlSuite>  xmlSuites, List<ISuite> suites, String outputDirectory) {
            init();
            boolean createSuiteNode = false;
            if(suites.size()>1){
                createSuiteNode=true;
            }
            for (ISuite suite : suites) {
                Map<String, ISuiteResult> result = suite.getResults();
                //如果suite里面没有任何用例,直接跳过,不在报告里生成
                if(result.size()==0){
                    continue;
                }
                //统计suite下的成功、失败、跳过的总用例数
                int suiteFailSize=0;
                int suitePassSize=0;
                int suiteSkipSize=0;
                ExtentTest suiteTest=null;
                //存在多个suite的情况下,在报告中将同一个一个suite的测试结果归为一类,创建一级节点。
                if(createSuiteNode){
                    suiteTest = extent.createTest(suite.getName()).assignCategory(suite.getName());
     
                }