【零成本实现接口自动化测试】Java+TestNG 测试Restful service

接口自动化测试 – Java+TestNG 测试 Restful Web Service

关键词:基于Rest的Web服务,接口自动化测试,数据驱动测试,测试Restful Web Service, 数据分离,Java+Maven+TestNG

本文主要介绍如何用Java针对Restful web service 做接口自动化测试(数据驱动),相比UI自动化,接口自动化稳定性可靠性高,实施难易程度低,做自动化性价比高。所用到的工具或类库有 TestNG, Apache POI, Jayway rest-assured,Skyscreamer - JSONassert

简介:

思想是数据驱动测试,用Excel来管理数据,‘Input’ Sheet中存放输入数据,读取数据后拼成request 调用service, 拿到response后写入 ‘Output’ Sheet 即实际结果, ‘Baseline’为基线(期望结果)用来和实际结果对比的,‘Comparison’ Sheet里存放的是对比结果不一致的记录,‘Result’ Sheet 是一个简单的结果报告。

Maven工程目录结构:

详细介绍

核心就一个测试类HTTPReqGenTest.java 由四部分组成

@BeforeTest  读取Excel (WorkBook) 的 ‘Input’ 和 ‘Baseline’ sheet

 

并且新建‘Output’, ‘Comparison’, ‘Result’ 三个空sheet

读取http_request_template.txt 内容转成string

@DataProvider (name = "WorkBookData")

TestNG的DataProvider, 首先用DataReader构造函数,读取Excel中Input的数据,放入HashMap<String, RecordHandler>, Map的key值就是test case的ID,value是RecordHandler对象,此对象中一个重要的成员属性就是input sheet里面 column和value 的键值对,遍历Map将test case ID 与 test case的value 即input sheet前两列的值放入List<Object[]> ,最后返回List的迭代器iterator (为了循环调用@Test方法)

@Test (dataProvider = "WorkBookData", description = "ReqGenTest")

测试方法,由DataProvider提供数据,首先根据ID去取myInputData里的RecordHandler, 由它和template 去生成request, 然后执行request 返回response,这些工作都是由HTTPReqGen.java这个类完成的,借助com.jayway.restassured, 返回的response是一个JSON体,通过org.skyscreamer.jsonassert.JSONCompare 与Baseline中事先填好的期望结果(同样也是JSON格式)进行比较, 根据结果是Pass还是Fail, 都会相应的往Excel里的相应Sheet写结果。

@AfterTest

写入统计的一些数据

关闭文件流

实现代码:

 HTTPReqGenTest.java

package com.demo.qa.rest_api_test;
 
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
 
import org.apache.commons.io.IOUtils;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.json.JSONException;
import org.skyscreamer.jsonassert.JSONCompare;
import org.skyscreamer.jsonassert.JSONCompareMode;
import org.skyscreamer.jsonassert.JSONCompareResult;
import org.testng.Assert;
import org.testng.ITest;
import org.testng.ITestContext;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
 
import com.demo.qa.utils.DataReader;
import com.demo.qa.utils.DataWriter;
import com.demo.qa.utils.HTTPReqGen;
import com.demo.qa.utils.RecordHandler;
import com.demo.qa.utils.SheetUtils;
import com.demo.qa.utils.Utils;
import com.jayway.restassured.response.Response;
 
public class HTTPReqGenTest implements ITest {
 
    private Response response;
    private DataReader myInputData;
    private DataReader myBaselineData;
    private String template;
 
    public String getTestName() {
        return "API Test";
    }
    
    String filePath = "";
    
    XSSFWorkbook wb = null;
    XSSFSheet inputSheet = null;
    XSSFSheet baselineSheet = null;
    XSSFSheet outputSheet = null;
    XSSFSheet comparsionSheet = null;
    XSSFSheet resultSheet = null;
    
    private double totalcase = 0;
    private double failedcase = 0;
    private String startTime = "";
    private String endTime = "";
 
    
    @BeforeTest
    @Parameters("workBook")
    public void setup(String path) {
        filePath = path;
        try {
            wb = new XSSFWorkbook(new FileInputStream(filePath));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        inputSheet = wb.getSheet("Input");
        baselineSheet = wb.getSheet("Baseline");
 
        SheetUtils.removeSheetByName(wb, "Output");
        SheetUtils.removeSheetByName(wb, "Comparison");
        SheetUtils.removeSheetByName(wb, "Result");
        outputSheet = wb.createSheet("Output");
        comparsionSheet = wb.createSheet("Comparison");
        resultSheet = wb.createSheet("Result");
 
        try {
            InputStream is = HTTPReqGenTest.class.getClassLoader().getResourceAsStream("http_request_template.txt");
            template = IOUtils.toString(is, Charset.defaultCharset());
        } catch (Exception e) {
            Assert.fail("Problem fetching data from input file:" + e.getMessage());
        }
        
        SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        startTime = sf.format(new Date());
    }
 
    @DataProvider(name = "WorkBookData")
    protected Iterator<Object[]> testProvider(ITestContext context) {
 
        List<Object[]> test_IDs = new ArrayList<Object[]>();
 
            myInputData = new DataReader(inputSheet, true, true, 0);
            Map<String, RecordHandler> myInput = myInputData.get_map();
 
            // sort map in order so that test cases ran in a fixed order
            Map<String, RecordHandler> sortmap = Utils.sortmap(myInput);
            
            for (Map.Entry<String, RecordHandler> entry : sortmap.entrySet()) {
                String test_ID = entry.getKey();
                String test_case = entry.getValue().get("TestCase");
                if (!test_ID.equals("") && !test_case.equals("")) {
                    test_IDs.add(new Object[] { test_ID, test_case });
                }
                totalcase++;
            }
            
            myBaselineData = new DataReader(baselineSheet, true, true, 0);
 
        return test_IDs.iterator();
    }
 
    @Test(dataProvider = "WorkBookData", description = "ReqGenTest")
    public void api_test(String ID, String test_case) {
 
        HTTPReqGen myReqGen = new HTTPReqGen();
 
        try {
            myReqGen.generate_request(template, myInputData.get_record(ID));
            response = myReqGen.perform_request();
        } catch (Exception e) {
            Assert.fail("Problem using HTTPRequestGenerator to generate response: " + e.getMessage());
        }
        
        String baseline_message = myBaselineData.get_record(ID).get("Response");
 
        if (response.statusCode() == 200)
            try {
                DataWriter.writeData(outputSheet, response.asString(), ID, test_case);
                
                JSONCompareResult result = JSONCompare.compareJSON(baseline_message, response.asString(), JSONCompareMode.NON_EXTENSIBLE);
                if (!result.passed()) {
                    DataWriter.writeData(comparsionSheet, result, ID, test_case);
                    DataWriter.writeData(resultSheet, "false", ID, test_case, 0);
                    DataWriter.writeData(outputSheet);
                    failedcase++;
                } else {
                    DataWriter.writeData(resultSheet, "true", ID, test_case, 0);
                }
            } catch (JSONException e) {
                DataWriter.writeData(comparsionSheet, "", "Problem to assert Response and baseline messages: "+e.getMessage(), ID, test_case);
                DataWriter.writeData(resultSheet, "error", ID, test_case, 0);
                failedcase++;
                Assert.fail("Problem to assert Response and baseline messages: " + e.getMessage());
            }
        else {
            DataWriter.writeData(outputSheet, response.statusLine(), ID, test_case);
 
            if (baseline_message.equals(response.statusLine())) {
                DataWriter.writeData(resultSheet, "true", ID, test_case, 0);
            } else {
                DataWriter.writeData(comparsionSheet, baseline_message, response.statusLine(), ID, test_case);
                DataWriter.writeData(resultSheet, "false", ID, test_case, 0);
                DataWriter.writeData(outputSheet);
                failedcase++;
            }
        }
    }
 
    @AfterTest
    public void teardown() 
        SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        endTime = sf.format(new Date());
        DataWriter.writeData(resultSheet, totalcase, failedcase, startTime, endTime);
        
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(filePath);
            wb.write(fileOutputStream);
            fileOutputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

DataReader

package com.demo.qa.utils;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
 
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
/**
 * Class that read data from XSSF sheet
 * 
 */
public class DataReader {
  
  protected static final Logger logger = LoggerFactory.getLogger(DataReader.class);
 
  private HashMap<String, RecordHandler> map = new HashMap<String, RecordHandler>();
 
  private Boolean byColumnName = false;
  private Boolean byRowKey = false;
  private List<String> headers = new ArrayList<String>();
 
  private Integer size = 0;
 
  public DataReader() {
  }
 
  /**
   * Primary constructor. Uses Apache POI XSSF to pull data from given excel workbook sheet. Data is stored in a
   * structure depending on the options from other parameters.
   * 
   * @param sheet Given excel sheet.
   * @param has_headers Boolean used to specify if the data has a header or not. The headers will be used as field keys.
   * @param has_key_column Boolean used to specify if the data has a column that should be used for record keys.
   * @param key_column Integer used to specify the key column for record keys.
   */
  public DataReader(XSSFSheet sheet, Boolean has_headers, Boolean has_key_column, Integer key_column) {
 
    XSSFRow myRow = null;
    HashMap<String, String> myList;
    size = 0;
 
    this.byColumnName = has_headers;
    this.byRowKey = has_key_column;
    
    try {
    
      if(byColumnName) {
        myRow = sheet.getRow(0);
        for(Cell cell: myRow) {
          headers.add(cell.getStringCellValue());
        }
        size = 1;
      }
  
      for(; (myRow = sheet.getRow(size)) != null; size++ ) {
  
        myList = new HashMap<String, String>();
  
        if(byColumnName) {
          for(int col = 0; col < headers.size(); col++ ) {
            if(col < myRow.getLastCellNum()) {
              myList.put(headers.get(col), getSheetCellValue(myRow.getCell(col))); // myRow.getCell(col).getStringCellValue());
            } else {
              myList.put(headers.get(col), "");
            }
          }
        } else {
          for(int col = 0; col < myRow.getLastCellNum(); col++ ) {
            myList.put(Integer.toString(col), getSheetCellValue(myRow.getCell(col)));
          }
        }
  
        if(byRowKey) {
          if(myList.size() == 2 && key_column == 0) {
            map.put(getSheetCellValue(myRow.getCell(key_column)), new RecordHandler(myList.get(1)));
          } else if(myList.size() == 2 && key_column == 1) {
            map.put(getSheetCellValue(myRow.getCell(key_column)), new RecordHandler(myList.get(0)));
          } else {
            map.put(getSheetCellValue(myRow.getCell(key_column)), new RecordHandler(myList));
          }
        } else {
          map.put(Integer.toString(size), new RecordHandler(myList));
        }
      }
     
    } catch (Exception e) {
      logger.error("Exception while loading data from Excel sheet:"+e.getMessage());
    }
  }
 
  /**
   * Utility method used for getting an excel cell value. Cell's type is switched to String before accessing.
   * 
   * @param cell Given excel cell.
   */
  private String getSheetCellValue(XSSFCell cell) {
 
    String value = "";
 
    try {
      cell.setCellType(Cell.CELL_TYPE_STRING);
      value = cell.getStringCellValue();
    } catch(NullPointerException npe) {
      return "";
    }
 
    return value;
  }
 
  /**
   * Returns entire HashMap containing this class's data.
   * 
   * @return HashMap<String, RecordHandler>, map of ID-Record data.
   */
  public HashMap<String, RecordHandler> get_map() {
 
    return map;
  }
 
 
  /**
   * Gets an entire record.
   * 
   * @param record String key value for record to be returned.
   * @return HashMap of key-value pairs representing the specified record.
   */
  public RecordHandler get_record(String record) {
 
    RecordHandler result = new RecordHandler();
 
    if(map.containsKey(record)) {
      result = map.get(record);
    }
 
    return result;
  }
 
}
package com.demo.qa.utils;
 
import static com.jayway.restassured.RestAssured.given;
 
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
 
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import com.jayway.restassured.response.Response;
import com.jayway.restassured.specification.RequestSpecification;
 
/**
 * Wrapper for RestAssured. Uses an HTTP request template and a single record housed in a RecordHandler object to
 * generate and perform an HTTP requests.
 * 
 */
public class HTTPReqGen {
  
  protected static final Logger logger = LoggerFactory.getLogger(HTTPReqGen.class);
 
  private RequestSpecification reqSpec;
 
  private String call_host = "";
  private String call_suffix = "";
  private String call_string = "";
  private String call_type = "";
  private String body = "";
  private Map<String, String> headers = new HashMap<String, String>();
  private HashMap<String, String> cookie_list = new HashMap<String, String>();
 
  public Map<String, String> getHeaders() {
    return headers;
  }
 
  public String getCallString() {
    return call_string;
  }
 
  /**
   * Constructor. Initializes the RequestSpecification (relaxedHTTPSValidation avoids certificate errors).
   * 
   */
  public HTTPReqGen() {
    reqSpec = given().relaxedHTTPSValidation();
  }
 
  public HTTPReqGen(String proxy) {
    reqSpec = given().relaxedHTTPSValidation().proxy(proxy);
  }
 
  /**
   * Pulls HashMap from given RecordHandler and calls primary generate_request method with it.
   * 
   * @param template String, should contain the full template.
   * @param record RecordHandler, the input data used to fill in replacement tags that exist in the template.
   * @return this Reference to this class, primarily to allow request generation and performance in one line.
   * @throws Exception 
   */
  public HTTPReqGen generate_request(String template, RecordHandler record) throws Exception {
 
    return generate_request(template, (HashMap<String, String>) record.get_map());
  }
 
  /**
   * Generates request data, using input record to fill in the template and then parse out relevant data. To fill in the
   * template, identifies tags surrounded by << and >> and uses the text from the corresponding fields in the
   * RecordHandler to replace them. The replacement is recursive, so tags may also exist in the fields of the
   * RecordHandler so long as they are also represented by the RecordHandler and do not form an endless loop.
   * After filling in the template, parses the resulting string in preparation for performing the HTTP request. Expects the
   * the string to be in the following format:
   *
   * <<call_type>> <<call_suffix>>
   * Host: <<root_host_name>>
   * <<header1_name>>:<<header1_value>>
   * ...
   * <<headerN_name>>: <<headerN_value>>
   *
   * <<body_text>>
   *
   * <<call_type>> must be GET, PUT, POST, or DELETE. <<call_suffix>> must be a string with no spaces. It is appended to
   * <<root_host_name>> to form the complete call string. After a single blank line is encountered, the rest of the file
   * is used as the body of text for PUT and POST calls. This function also expects the Record Handler to include a field
   * named "VPID" containing a unique record identifier for debugging purposes.
   * 
   * @param template String, should contain the full template.
   * @param record RecordHandler, the input data used to fill in replacement tags that exist in the template.
   * @return this Reference to this class, primarily to allow request generation and performance in one line.
   * @throws Exception 
   */
  public HTTPReqGen generate_request(String template, HashMap<String, String> record) throws Exception {
 
    String filled_template = "";
    Boolean found_replacement = true;
    headers.clear();
    
    try {
      
      // Splits template into tokens, separating out the replacement strings
      // like <<id>>
      String[] tokens = tokenize_template(template);
 
      // Repeatedly perform replacements with data from record until no
      // replacements are found
      // If a replacement's result is an empty string, it will not throw an
      // error (but will throw one if there is no column for that result)
      while(found_replacement) {
        found_replacement = false;
        filled_template = "";
  
        for(String item: tokens) {
  
          if(item.startsWith("<<") && item.endsWith(">>")) {
            found_replacement = true;
            item = item.substring(2, item.length() - 2);
            
            if( !record.containsKey(item)) {
              logger.error("Template contained replacement string whose value did not exist in input record:[" + item + "]");
            }            
            
            item = record.get(item);
          }
  
          filled_template += item;
        }
  
        tokens = tokenize_template(filled_template);
      }
      
    } catch (Exception e) {
      logger.error("Problem performing replacements from template: ", e);
    }
    try {
      
      // Feed filled template into BufferedReader so that we can read it line
      // by line.
      InputStream stream = IOUtils.toInputStream(filled_template, "UTF-8");
      BufferedReader in = new BufferedReader(new InputStreamReader(stream));
      String line = "";
      String[] line_tokens;
      
      // First line should always be call type followed by call suffix
      line = in.readLine();
      line_tokens = line.split(" ");
      call_type = line_tokens[0];
      call_suffix = line_tokens[1];
      // Second line should contain the host as it's second token
      line = in.readLine();
      line_tokens = line.split(" ");
      call_host = line_tokens[1];
 
      // Full call string for RestAssured will be concatenation of call
      // host and call suffix
      call_string = call_host + call_suffix;
 
      // Remaining lines will contain headers, until the read line is
      // empty
      line = in.readLine();
      while(line != null && !line.equals("")) {
 
        String lineP1 = line.substring(0, line.indexOf(":")).trim();
        String lineP2 = line.substring(line.indexOf(" "), line.length()).trim();
 
        headers.put(lineP1, lineP2);
 
        line = in.readLine();
      }
 
      // If read line is empty, but next line(s) have data, create body
      // from them
      if(line != null && line.equals("")) {
        body = "";
        while( (line = in.readLine()) != null && !line.equals("")) {
          body += line;
        }
      }
 
    } catch(Exception e) {
      logger.error("Problem setting request values from template: ", e);
    }
 
    return this;
  }
  
  /**
   * Performs the request using the stored request data and then returns the response.
   * 
   * @return response Response, will contain entire response (response string and status code).
   */
  public Response perform_request() throws Exception {
    
    Response response = null;
    
    try {
 
      for(Map.Entry<String, String> entry: headers.entrySet()) {
        reqSpec.header(entry.getKey(), entry.getValue());
      }
  
      for(Map.Entry<String, String> entry: cookie_list.entrySet()) {
        reqSpec.cookie(entry.getKey(), entry.getValue());
      }
  
      switch(call_type) {
  
        case "GET": {
          response = reqSpec.get(call_string);
          break;
        }
        case "POST": {
          response = reqSpec.body(body).post(call_string);
          break;
        }
        case "PUT": {
          response = reqSpec.body(body).put(call_string);
          break;
        }
        case "DELETE": {
          response = reqSpec.delete(call_string);
          break;
        }
  
        default: {
          logger.error("Unknown call type: [" + call_type + "]");
        }
      }
      
    } catch (Exception e) {
      logger.error("Problem performing request: ", e);
    }
 
    return response;
  }
 
  /**
   * Splits a template string into tokens, separating out tokens that look like "<<key>>"
   * 
   * @param template String, the template to be tokenized.
   * @return list String[], contains the tokens from the template.
   */
  private String[] tokenize_template(String template) {
    return template.split("(?=[<]{2})|(?<=[>]{2})");
  }
 
}

RecordHandler

package com.demo.qa.utils;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
 
public class RecordHandler {
 
  private enum RecordType {
    VALUE, NAMED_MAP, INDEXED_LIST
  }
 
  private String single_value = "";
  private HashMap<String, String> named_value_map = new HashMap<String, String>();
  private List<String> indexed_value_list = new ArrayList<String>();
  private RecordType myType;
 
  public RecordHandler() {
    this("");
  }
 
  public RecordHandler(String value) {
 
    this.myType = RecordType.VALUE;
    this.single_value = value;
 
  }
 
  public RecordHandler(HashMap<String, String> map) {
 
    this.myType = RecordType.NAMED_MAP;
    this.named_value_map = map;
 
  }
 
  public RecordHandler(List<String> list) {
 
    this.myType = RecordType.INDEXED_LIST;
    this.indexed_value_list = list;
 
  }
 
  public HashMap<String, String> get_map() {
    return named_value_map;
  }
 
  public int size() {
    int result = 0;
 
    if(myType.equals(RecordType.VALUE)) {
      result = 1;
    } else if(myType.equals(RecordType.NAMED_MAP)) {
      result = named_value_map.size();
    } else if(myType.equals(RecordType.INDEXED_LIST)) {
      result = indexed_value_list.size();
    }
 
    return result;
  }
 
  public String get() {
    String result = "";
 
    if(myType.equals(RecordType.VALUE)) result = single_value;
    else {
      System.out.println("Called get() on wrong type:" + myType.toString());
    }
 
    return result;
  }
 
  public String get(String key) {
    String result = "";
 
    if(myType.equals(RecordType.NAMED_MAP)) result = named_value_map.get(key);
 
    return result;
  }
 
  public String get(Integer index) {
    String result = "";
 
    if(myType.equals(RecordType.INDEXED_LIST)) result = indexed_value_list.get(index);
 
    return result;
  }
 
  public Boolean set(String value) {
    Boolean result = false;
 
    if(myType.equals(RecordType.VALUE)) {
      this.single_value = value;
      result = true;
    } else if(myType.equals(RecordType.INDEXED_LIST)) {
      this.indexed_value_list.add(value);
      result = true;
    }
 
    return result;
  }
 
  public Boolean set(String key, String value) {
    Boolean result = false;
 
    if(myType.equals(RecordType.NAMED_MAP)) {
      this.named_value_map.put(key, value);
      result = true;
    }
 
    return result;
  }
 
  public Boolean set(Integer index, String value) {
    Boolean result = false;
 
    if(myType.equals(RecordType.INDEXED_LIST)) {
      if(this.indexed_value_list.size() > index) this.indexed_value_list.set(index, value);
 
      result = true;
    }
 
    return result;
  }
 
  public Boolean has(String value) {
    Boolean result = false;
 
    if(myType.equals(RecordType.VALUE) && this.single_value.equals(value)) {
      result = true;
    } else if(myType.equals(RecordType.NAMED_MAP) && this.named_value_map.containsKey(value)) {
      result = true;
    } else if(myType.equals(RecordType.INDEXED_LIST) && this.indexed_value_list.contains(value)) {
      result = true;
    }
 
    return result;
  }
 
  public Boolean remove(String value) {
    Boolean result = false;
 
    if(myType.equals(RecordType.VALUE) && this.single_value.equals(value)) {
      this.single_value = "";
      result = true;
    }
    if(myType.equals(RecordType.NAMED_MAP) && this.named_value_map.containsKey(value)) {
      this.named_value_map.remove(value);
      result = true;
    } else if(myType.equals(RecordType.INDEXED_LIST) && this.indexed_value_list.contains(value)) {
      this.indexed_value_list.remove(value);
      result = true;
    }
 
    return result;
  }
 
  public Boolean remove(Integer index) {
    Boolean result = false;
 
    if(myType.equals(RecordType.INDEXED_LIST) && this.indexed_value_list.contains(index)) {
      this.indexed_value_list.remove(index);
      result = true;
    }
 
    return result;
  }
 
}

其它不重要的类不一一列出来了。

pom.xml

<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>com.demo</groupId>
    <artifactId>qa</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>Automation</name>
    <description>Test project for Demo</description>
 
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
            </plugin>
            <plugin>
                <artifactId>maven-dependency-plugin</artifactId>
            </plugin>
        </plugins>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>2.5</version>
                    <executions>
                        <execution>
                            <id>default-jar</id>
                            <goals>
                                <goal>test-jar</goal>
                            </goals>
                            <configuration>
                                <archive>
                                    <manifest>
                                        <mainClass>com.demo.qa.utils.TestStartup</mainClass>
                                        <addClasspath>true</addClasspath>
                                        <classpathPrefix>lib/</classpathPrefix>
                                        <useUniqueVersions>false</useUniqueVersions>
                                    </manifest>
                                </archive>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.17</version>
                    <configuration>
                        <skip>true</skip>
                        <suiteXmlFiles>
                            <suiteXmlFile>src\test\resources\HTTPReqGenTest.xml</suiteXmlFile>
                        </suiteXmlFiles>
                    </configuration>
                </plugin>
                <plugin>
                    <artifactId>maven-dependency-plugin</artifactId>
                    <version>2.8</version>
                    <executions>
                        <execution>
                            <id>default-cli</id>
                            <phase>package</phase>
                            <goals>
                                <goal>copy-dependencies</goal>
                            </goals>
                            <configuration>
                                <outputDirectory>${project.build.directory}/lib</outputDirectory>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>org.eclipse.m2e</groupId>
                    <artifactId>lifecycle-mapping</artifactId>
                    <version>1.0.0</version>
                    <configuration>
                        <lifecycleMappingMetadata>
                            <pluginExecutions>
                                <pluginExecution>
                                    <pluginExecutionFilter>
                                        <groupId>org.apache.maven.plugins</groupId>
                                        <artifactId>maven-dependency-plugin</artifactId>
                                        <versionRange>[1.0.0,)</versionRange>
                                        <goals>
                                            <goal>copy-dependencies</goal>
                                        </goals>
                                    </pluginExecutionFilter>
                                    <action>
                                        <execute />
                                    </action>
                                </pluginExecution>
                            </pluginExecutions>
                        </lifecycleMappingMetadata>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.3.2</version>
        </dependency>
 
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>
        <dependency>
            <groupId>com.jayway.restassured</groupId>
            <artifactId>rest-assured</artifactId>
            <version>2.3.3</version>
        </dependency>
        <dependency>
            <groupId>com.jayway.restassured</groupId>
            <artifactId>json-path</artifactId>
            <version>2.3.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.10.1</version>
            <exclusions>
                <exclusion>
                    <artifactId>commons-codec</artifactId>
                    <groupId>commons-codec</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.8</version>
        </dependency>
        <dependency>
            <groupId>commons-cli</groupId>
            <artifactId>commons-cli</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.10.1</version>
            <exclusions>
                <exclusion>
                    <artifactId>xml-apis</artifactId>
                    <groupId>xml-apis</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.skyscreamer</groupId>
            <artifactId>jsonassert</artifactId>
            <version>1.2.3</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.7</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.7.6</version>
        </dependency>
    </dependencies>
</project>

运行是通过TestNG的xml文件来执行的, 里面配置了Parameter “workBook” 的路径

TestNG的运行结果都是Pass, 但事实上里面有case是Fail的,我只是借助TestNG来运行,我并没有在@Test方法里加断言Assert, 所以这里不会Fail, 我的目的是完全用Excel来管理维护测试数据以及测试结果,做到数据脚本完全分离。

Output sheet

 Comparison sheet

Result sheet

当然 你也可以把maven工程打成一个可执行jar来运行,不过需要增加一个main函数作为入口,xml测试文件通过参数传递进去,另外还需要在pom里配置一些插件,像maven-jar-plugin。

如果你还需要做back-end DB check,你可以在Input里再增加几列,你要查询的表,字段,Baseline里也相应的加上期望结果,这里就不再赘述了。

总结:

感谢每一个认真阅读我文章的人!!!

作为一位过来人也是希望大家少走一些弯路,如果你不想再体验一次学习时找不到资料,没人解答问题,坚持几天便放弃的感受的话,在这里我给大家分享一些自动化测试的学习资源,希望能给你前进的路上带来帮助

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:/a/275347.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

金蝶云星空反写规则表结构同步另一个数据库

文章目录 金蝶云星空反写规则表结构同步另一个数据库在BOS找到《反写规则》的表反写规则相关表创建反写规则&#xff0c;或者已经创建好的反写规则定位反写规则数据导出表数据执行脚本BOS导入数据库直接执行 金蝶云星空反写规则表结构同步另一个数据库 在BOS找到《反写规则》的…

k8s 网络

还没有部署网络。 k8s的网络类型&#xff1a; k8s中的通信模式&#xff1a; 1&#xff0c;pod内部之间容器和容器之间的通信。 在同一个pod中的容器共享资源和网络&#xff0c;使用同一个网络命名空间。可以直接通信的。 2&#xff0c;同一个node节点之内不同pod之间的通信。…

香橙派5plus从ssd启动Ubuntu

官方接口图 我实际会用到的就几个接口&#xff0c;背面的话就一个M.2固态的位置&#xff1a; 其中WIFI模块的接口应该也可以插2230的固态&#xff0c;不过是pcie2.0的速度&#xff0c;背面的接口则是pcie3.0*4的速度&#xff0c;差距还是挺大的。 开始安装系统 准备工作 一张…

开源轻量级分布式文件系统FastDFS本地部署并实现远程访问服务器

文章目录 前言1. 本地搭建FastDFS文件系统1.1 环境安装1.2 安装libfastcommon1.3 安装FastDFS1.4 配置Tracker1.5 配置Storage1.6 测试上传下载1.7 与Nginx整合1.8 安装Nginx1.9 配置Nginx 2. 局域网测试访问FastDFS3. 安装cpolar内网穿透4. 配置公网访问地址5. 固定公网地址5.…

算法学习系列(十三):Trie树

目录 引言一、Trie概念二、Trie树模板三、例题 引言 这个Trie还是比较有用的&#xff0c;主要的功能就是高效的存储和查找字符串的数据结构。 一、Trie概念 假设这个Trie只存储小写字母的话&#xff1a; 这个大概就是这么个概念&#xff0c;就是头结点是0号&#xff0c;然后…

使用腾讯云轻量应用服务器基于SRS搭建个人直播间

使用腾讯云轻量应用服务器基于SRS音视频服务器应用模板镜像即可一键搭建个人直播间&#xff0c;SRS Stack让你一键拥有自己的视频云解决方案&#xff0c;可以在云上或私有化部署&#xff0c;支持丰富的音视频协议&#xff0c;提供鉴权、私人直播间、多平台转播、录制、虚拟直播…

js中变量的使用

文章目录 一、变量二、声明三、赋值四、更新变量五、声明多个变量(不推荐)六、变量的本质七、关键字八、变量名命名规则 一、变量 理解变量是计算机存储数据的“容器”&#xff0c;掌握变量的声明方式 白话&#xff1a;变量就是一个装东西的盒子。通俗&#xff1a;变量是计算机…

【MySQL学习笔记007】约束

1、概述 &#xff08;1&#xff09;概念&#xff1a;约束是作用于表中字段上的规则&#xff0c;用于限制存储在表中的数据。 &#xff08;2&#xff09;目的&#xff1a;保证数据库中数据的正确、有效性和完整性。 &#xff08;3&#xff09;分类 约束 描述 关键字 …

ZStack Cube超融合一体机助力电子支付企业升级改造

电子支付服务企业实壹信息通过ZStack Cube超融合一体机为业务生产环境构建新一代云基础设施&#xff0c;结合V2V迁移模块实现ZStack社区版云平台应用迁移到全新的云基础设施ZStack Cube 超融合一体机上&#xff0c;同时共享分布式存储和外接FC-SAN存储。此外&#xff0c;运维人…

Android 8.1 设置USB传输文件模式(MTP)

项目需求&#xff0c;需要在电脑端adb发送通知手机端接收指令&#xff0c;将USB的仅充电模式更改成传输文件&#xff08;MTP&#xff09;模式&#xff0c;便捷用户在我的电脑里操作内存文件&#xff0c;下面是我们的常见的修改方式 1、android12以下、android21以上是这种方式…

Elasticsearch:在不停机的情况下优化 Elasticsearch Reindex

实现零停机、高效率和成功迁移更新的指南。更多阅读&#xff1a;Elasticsearch&#xff1a;如何轻松安全地对实时 Elasticsearch 索引 reindex 你的数据。 在使用 Elasticsearch 的时候&#xff0c;总会有需要修改索引映射的时候&#xff0c;遇到这种情况&#xff0c;我们只能做…

go语言,ent库与gorm库,插入一条null值的time数据

情景介绍 使用go语言&#xff0c;我需要保存xxxTime的字段至数据库中&#xff0c;这个字段可能为空&#xff0c;也可能是一段时间。我采取的是统一先赋值为空&#xff0c;若有需要&#xff0c;则再进行插入&#xff08;需要根据另一个字段判断是否插入&#xff09; 在我的数据…

PTS 3.0:可观测加持的下一代性能测试服务

作者&#xff1a;肖长军&#xff08;穹谷&#xff09; 大家好&#xff0c;我是来自阿里云云原生应用平台的肖长军&#xff0c;花名穹谷&#xff0c;我此次分享的主题是《可观测加持的下一代性能测试服务》。提到性能测试大家并不陌生&#xff0c;性能测试已成为评估系统能力、…

使用rsync构建镜像网站

实验环境 某公司在深圳、北京两地各放置了一台网站服务器&#xff0c;分别应对南北大区内不断增长的客户访问需求&#xff0c;两台服务器的网站文档必须保持一致&#xff0c;如图12.3所示&#xff0c;同步链路已通过VPN专用线路实现。 需求描述 > 服务器 A&#xff08;北京…

SpringBoot多线程与任务调度总结

一、前言 多线程与任务调度是java开发中必须掌握的技能&#xff0c;在springBoot的开发中&#xff0c;多线程和任务调度变得越来越简单。实现方式可以通过实现ApplicationRunner接口&#xff0c;重新run的方法实现多线程。任务调度则可以使用Scheduled注解 二、使用示例 Slf…

linux如何清理磁盘,使得数据难以恢复

sda 是硬盘&#xff0c;sda1 和 sda2 是硬盘的两个分区。centos-root 是一个逻辑卷&#xff0c;挂载在根目录 /。 /dev/sda 是硬盘&#xff0c;/dev/sda1 和 /dev/sda2 是硬盘的两个分区。 [rootnode2 ~]# dd if/dev/urandom of/dev/sda bs4M这个命令将从 /dev/urandom 读取随…

【软件工程大题】数据流图_DFD图_精简易上手

数据流图(DFD)是一种图形化技术,它描绘信息流和数据从输人移动到输出的过程中所经受的变换。 首先给出一个数据流图样例 基本的四种图形 直角矩形:代表源点或终点,一般来说,是人,如例图的仓库管理员和采购员圆形(也可以画成圆角矩形):是处理,一般来说,是动作,是动词名词的形式…

<JavaEE> TCP 的通信机制(二) -- 连接管理(三次握手和四次挥手)

目录 TCP的通信机制的核心特性 三、连接管理 1&#xff09;什么是连接管理&#xff1f; 2&#xff09;“三次握手”建立连接 1> 什么是“三次握手”&#xff1f; 2> “三次握手”的核心作用是什么&#xff1f; 3&#xff09;“四次挥手”断开连接 1> 什么是“…

vue动态路由,三级及以上路由,地址跳转,但是页面不显示

vue动态路由的时候,一级,二级路由都正常展示,但是三级,四级,五级等就只看到地址跳转了,但是页面并没有跳转,原因是共用了一个<router-view></router-view> import Layout from /layout import Vue from vue import Router from vue-router import db from /utils/…

工具系列:TensorFlow决策森林_(8)组合决策森林和神经网络模型

文章目录 介绍安装 TensorFlow Decision Forests导入库数据集模型结构模型训练评估决策森林下一步是什么&#xff1f; 介绍 欢迎来到TensorFlow Decision Forests&#xff08;TF-DF&#xff09;的模型组合教程。本教程将向您展示如何使用通用的预处理层和Keras函数式API将多个…