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

news2024/11/16 21:25:24

接口自动化测试 – 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里也相应的加上期望结果,这里就不再赘述了。

总结:

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

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

 

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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

成都爱尔樊映川院长解析视网膜脱离之紧急72小时

熬夜工作、熬夜游戏…… 眼睛模糊到看不清? 突然眼前发生大量漂浮物? 眼前有块遮挡黑影? 眼前有闪光感? 这些情况虽然并非确诊视网膜发生脱离&#xff0c;但需要赶紧到院就诊&#xff0c;做到及时发现及时治疗。 视网膜脱离 视网膜脱离(简称“网脱”)是视网膜神经上皮层与色…

【并发设计模式】聊聊 基于Copy-on-Write模式下的CopyOnWriteArrayList

在并发编程领域&#xff0c;其实除了使用上一篇中的属性不可变。还有一种方式那就是针对读多写少的场景下。我们可以读不加锁&#xff0c;只针对于写操作进行加锁。本质上就是读写复制。读的直接读取&#xff0c;写的使用写一份数据的拷贝数据&#xff0c;然后进行写入。在将新…

基于Java (spring-boot)的超市管理系统

一、项目介绍 超市管理系统对商品日常维护、售卖、商品类别、存储记录、库存维护等功能的统计及整理。系统设计了系统管理员&#xff0c;销售人员&#xff0c;仓库管理员多个角色。通过多角色、多功能的使用&#xff0c;可以方便地查询商品的库存值、是否临期、是否库存紧张&am…

ubuntu20 安装缺失的字体

在/usr/share/fonts创建文件夹winfonts sudo mkdir winfonts 下载缺失的字体后&#xff0c;复制命令到对应的文件夹。 刷新字体库 sudo mkfontscale sudo mkfontdir sudo fc-cache

可视化远程监控EasyCVR及AI识别技术在种植养殖基地/果园场景中的应用建设

一、背景与需求 种植园、果园一般场地面积较大&#xff0c;看守人员较少&#xff0c;财产安全成为了关注的重点。传统靠人力监管的方式效率低下&#xff0c;管理也较为片面且人力投入大&#xff0c;那么如何实现少人化、智能化监管&#xff0c;助力果园安全监控、守护园区安全…

怎么用Facebook找客户?Facebook开发客户攻略分享

跨境人最了解的电商平台之一就是Facebook了&#xff0c;说到Facebook&#xff0c;它拥有超过20亿的活跃用户&#xff0c;所以你可以在这个平台上面找到更多的潜在客户。今天的文章就主要分享用Facebook开发客户的方法&#xff0c;全是干货&#xff0c;建议收藏&#xff01; 一、…

IPv4 VS IPv6代理全解,跨境电商代理IP选哪种?

IP&#xff08;互联网协议&#xff09;被网站用来收集用户位置信息。当今最流行的两种互联网协议是 IPv4 和 IPv6。 在我们选择IP代理的过程中可以看到IPv4、IPv6两种类型&#xff0c;他们有何区别&#xff1f;如何选择&#xff1f;下面为大家讲解&#xff01; 在这篇文章中&a…

全面理解Stable Diffusion采样器

全面理解Stable Diffusion采样器 原文&#xff1a;Stable Diffusion Samplers: A Comprehensive Guide 在 AUTOMATIC1111 的 SD webui 中&#xff0c;有许多采样器&#xff08;sampler&#xff09;&#xff0c;如 Euler a&#xff0c;Heun&#xff0c;DDIM&#xff0c;… 什么是…

.NET CORE 无法调试 当前不会命中断点

多个项目直接可以设置项目的属性->生成->输出的配置文件输出地址 然后路径统一输入该项目的bib/debug/.netcorex.x就可以了

Pandas教程(二)—— 不同格式的数据读取

前言&#xff1a;几种常用数据格式的介绍 csv文件 1. 逗号分隔值文件&#xff0c;以纯文本形式&#xff08;记事本&#xff09;存储表格数据 2. 它是一种平面文件&#xff1a;即只存储数据和文字&#xff0c;不能存储公式、图表等 3. 更适合存储大数据&#xff0c;一般用来批…

如何解决msvcr100.dll丢失问题,深度解析5种靠谱的方法

在计算机的世界里&#xff0c;我们常常会遇到一些看似微不足道的问题&#xff0c;但它们却可能引发一系列的困扰。其中之一就是“msvcr100.dll丢失”。这个问题可能会导致某些程序无法正常运行。本文将介绍5种解决msvcr100.dll丢失问题的方法。 msvcr100.dll是什么 msvcr100.d…

NXP S32K358 davinci cfg can驱动配置

NXP S32K358一共有8路can&#xff0c;都支持Can FD和Classical Can。其中FlexCan0-2是增强型CAN&#xff0c;支持8byte的mailbox 96个。FlexCan3-7支持8byte的mailbox 64个。 1.增强型Can和普通Can波特率采样点设置区别 在NXP S32K358中增强CAN的波特率采样点的寄存器和普通C…

新手为什么跟着大型机构交易?fpmarkets总结理由

正所谓方向不对努力白费&#xff0c;这也就是为什么fpmarkets建议新手在刚开始的时候&#xff0c;跟着大型机构进行交易。 这些大型机构包括中央银行、巨额对冲基金、投资和保险公司等等&#xff0c;首先fpmarkets认为这些大型机构的交易量巨大&#xff0c;能够影响市场的走势。…

2023年12月27日学习记录_加入噪声

目录 1、今日计划学习内容2、今日学习内容1、add noise to audio clipssignal to noise ratio(SNR)加入 additive white gaussian noise(AWGN)加入 real world noises 2、使用kaggel上的一个小demo&#xff1a;CNN模型运行时出现的问题调整采样率时出现bug 3、明确90dB下能否声…

hadoop hive spark flink 安装

下载地址 Index of /dist ubuntu安装hadoop集群 准备 IP地址主机名称192.168.1.21node1192.168.1.22node2192.168.1.23node3 上传 hadoop-3.3.5.tar.gz、jdk-8u391-linux-x64.tar.gz JDK环境 node1、node2、node3三个节点 解压 tar -zxvf jdk-8u391-linux-x64.tar.gz…

Git系统有哪些优势

在现在的这个软件开发领域&#xff0c;版本控制是一项非常重要的工作。Git作为比较流行的分布式版本控制系统&#xff0c;他有着独特的优势成为了很多开发者们的首选。那Git系统都有哪些优势呢&#xff0c;下面我以自己的理解简单的介绍一下。 分布式版本控制的优势 Git用的是…

003、一起来玩猜数游戏吧!

1. 上篇补充 在项目 hello_world 中&#xff0c;有一些文件。这里提一下每个文件的用途&#xff0c;了解一下即可&#xff0c;暂时不用深究&#xff0c;后面用到会详细讨论。 1. src &#xff1a;这个文件夹里主要用于存放源代码文件。Rust 项目的源代码文件通常以 .rs 为后缀&…

给typora更换字体

给typora更换字体 1.字体推荐(程序员) JetBrains MonoFira CodeCascadia Code 如果你不知道用哪个&#xff0c;把所有字体都装上。 2.安装字体 以 JetBrainsMono-1.0.3 为例&#xff0c;打开 ttf 文件夹&#xff0c;选择字体&#xff0c;右键 安装 3.设置typora的字体 文…

three.js 模型 居中

物体不居中 模型的几何中心位置不对&#xff0c; 设置偏离物体实际几何中心&#xff0c;当设置position&#xff08;0,0,0&#xff09;时就会出现偏离。 解决方案 此处有两种解决方案 建模师处理模型&#xff0c;将模型的几何中心移动到&#xff08;0&#xff0c; 0&#…

【Linux】进程查看|fork函数|进程状态

&#x1f984; 个人主页——&#x1f390;开着拖拉机回家_Linux,大数据运维-CSDN博客 &#x1f390;✨&#x1f341; &#x1fa81;&#x1f341;&#x1fa81;&#x1f341;&#x1fa81;&#x1f341;&#x1fa81;&#x1f341; &#x1fa81;&#x1f341;&#x1fa81;&am…