Data Factory - Generic Use Cases for Generating Interfaces

1. Background introduction

What use cases can be generalized and fixed?

  • In view of the previously mentioned interface use case design ideas , it is divided into three entry points :

  • for example:
{
    "field": "value"
}
  • For this string type input parameter, we can design:
    • The current data type input parameter (for example: empty string, space character, special character, upper and lower limit of the number of characters, etc.)
    • Non-current data type input parameters (for example: integer, floating point type, Boolean type, etc.)
    • special values ​​(0, null values, etc.)

2. Preparation

Prerequisites for running Data Factory.

  • javadevelopment and operating environment.
  • mavenBuild tools.
  • Dependencies used:
    <dependencies>
        <!--解析 json-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.76</version>
        </dependency>

        <!--操作 excel 文件-->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.1.2</version>
        </dependency>
    </dependencies>

 

3. Design ideas

How tools interact with each other.

  • Package level directory :
+---java
|   \---com
|       \---example
|           \---myproject
|               +---boot
|               |       Launcher.java
|               |
|               +---core
|               |       DataFactory.java
|               |
|               +---pojo
|               |       WriteBackData.java
|               |
|               \---util
|                       CaseUtils.java
|                       ExcelUtils.java
|                       FileUtils.java
|                       JsonPathParser.java
|                       JsonUtils.java
|
\---resources
        request.json
        TestCase.xls
  • The main flow of script execution :

Fourth, the specific implementation of the code

  • Launcher (startup class):
package com.example.myproject.boot;

import com.alibaba.fastjson.JSONObject;
import com.example.myproject.core.DataFactory;

/**
 * 执行入口。
 *
 * @author Jan
 * @date 2023/08
 */
public class Launcher {
    public static void main(String[] args) throws Exception {
        // 这里支持传入自定义的用例拓展字段 -> new JSONObject() 。
        DataFactory.runAndCreateTestCases(null);
    }
}

  • DataFactory (core class):
package com.example.myproject.core;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONPath;
import com.example.myproject.pojo.WriteBackData;
import com.example.myproject.util.*;

import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;

/**
 * 数据工厂。
 *
 * @author Jan
 * @date 2023/08
 */
public class DataFactory {

    private DataFactory() {

    }

    /**
     * 回写数据的集合。
     */
    private static final List<WriteBackData> WRITE_BACK_DATA = new ArrayList<>();

    /**
     * 运行和创建测试用例。
     *
     * @param ext ext 额外的拓展参数。
     * @throws Exception 异常。
     */
    public static void runAndCreateTestCases(JSONObject ext) throws Exception {
        // 获取请求示例。
        String jsonStr = FileUtils.readSampleRequest();
        // 解析json的字段数据类型及jsonPath。
        Set<String> jsonPaths = JsonPathParser.getJsonPaths(jsonStr);
        for (String jsonPath : jsonPaths) {
            // 字段数据类型。
            String filedDataType = JSONPath.read(jsonStr, jsonPath).getClass().getSimpleName();

            // 跳过复合类型。
            if ("JSONObject".equals(filedDataType)) {
                continue;
            }

            // 字段名。
            String[] split = jsonPath.split("\\.");
            String filedName = split[split.length - 1];

            // 通过反射生成对应数据类型的测试用例。
            List<Object> caseValues = DataFactory.getObjectArrayFromReflectType(CaseUtils.class, filedDataType);
            Map<String, String> caseNameAndRequestValueMap = new HashMap<>();

            for (Object value : caseValues) {
                String caseName = CaseUtils.createSpecifyCaseNameByCaseValue(filedName, value);
                // 修改字段值。
                JSONObject jsonObject = JsonUtils.checkAndSetJsonPathValue(jsonStr, jsonPath, value);
                caseNameAndRequestValueMap.put(caseName, jsonObject.toJSONString());
            }

            for (Map.Entry<String, String> entry : caseNameAndRequestValueMap.entrySet()) {
                String caseName = entry.getKey();
                String requestValue = "";
                if (null != ext) {
                    // 额外参数。
                    ext.put("title", caseName);
                    ext.put("case", JSON.parseObject(entry.getValue()));
                    requestValue = ext.toJSONString();
                } else {
                    requestValue = entry.getValue();
                }
                WRITE_BACK_DATA.add(new WriteBackData(caseName, requestValue));
            }
        }
        System.out.println("组装完成的用例数为 = " + WRITE_BACK_DATA.size());
        //开始回写
        ExcelUtils.initFileAndWriteDataToExcel(WRITE_BACK_DATA);
    }

    /**
     * 通过反射获取用例集合。
     *
     * @param clazz clazz
     * @param type  类型
     * @return {@link List}<{@link Object}>
     * @throws NoSuchMethodException     没有这样方法异常。
     * @throws InvocationTargetException 调用目标异常。
     * @throws InstantiationException    实例化异常。
     * @throws IllegalAccessException    非法访问异常。
     */
    private static List<Object> getObjectArrayFromReflectType(Class<? extends CaseUtils> clazz, String type) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        Object obj = clazz.getConstructor().newInstance();
        String methodName = "get" + type + "TypeCases";
        Method method = clazz.getDeclaredMethod(methodName);
        Object invoke = method.invoke(obj);
        int length = Array.getLength(invoke);
        List<Object> caseValues = new ArrayList<>();
        for (int i = 0; i < length; i++) {
            caseValues.add(Array.get(invoke, i));
        }
        return caseValues;
    }
}

  • WriteBackData (encapsulates write-back information):
package com.example.myproject.pojo;

/**
 * 回写对象。
 *
 * @author Jan
 * @date 2023/08
 */
public class WriteBackData {
    /**
     * 用例名称。
     */
    private String caseName;

    /**
     * 操作步骤。(即用例报文)
     */
    private String step;

    public WriteBackData(String caseName, String step) {
        this.caseName = caseName;
        this.step = step;
    }

    public String getCaseName() {
        return caseName;
    }

    public void setCaseName(String caseName) {
        this.caseName = caseName;
    }

    public String getStep() {
        return step;
    }

    public void setStep(String step) {
        this.step = step;
    }
}

  • CaseUtils (static storage use case design):
package com.example.myproject.util;

import java.util.Arrays;
import java.util.Collections;

/**
 * 测试用例。
 *
 * @author Jan
 * @date 2023/08
 */
public class CaseUtils {

    private static final String STRING = "string";
    private static final String INTERFACE = "[接口名]";

    /**
     * 字符串类型用例。
     * 空串、空格字符、特殊字符、整型、精度类型、null值。
     *
     * @return {@link Object[]}
     */
    public static Object[] getStringTypeCases() {
        return new Object[]{"", " ", "@", -1, -1.1, "null"};
    }

    /**
     * 整数类型用例。
     * 字符串类型、特殊值0、负数值、整型较大值、整型边界值、精度类型、null值。
     *
     * @return {@link Object[]}
     */
    public static Object[] getIntegerTypeCases() {
        return new Object[]{STRING, 0, -1, 2147483647, 2147483648L, -1.1, "null"};
    }

    /**
     * 长整型用例。
     * 字符串类型、特殊值0、负数值、精度类型、null值、长整型边界值。
     *
     * @return {@link Object[]}
     */
    public static Object[] getLongTypeCases() {
        return new Object[]{STRING, 0, -1, -1.1, "null", 9223372036854775807L};
    }

    /**
     * 浮点类型用例。
     * 字符串类型、负精度值、负整数值、null值、三位小数。
     *
     * @return {@link Object[]}
     */
    public static Object[] getBigDecimalTypeCases() {
        return new Object[]{STRING, -1.1, -1, 0, "null", 999.999D};
    }

    /**
     * 布尔类型用例。
     * 字符串类型、负精度值、负整数值、特殊值0、null值、真布尔、假布尔。
     *
     * @return {@link Object[]}
     */
    public static Object[] getBooleanTypeCases() {
        return new Object[]{STRING, -1, -1.1, 0, "null", true, false};
    }

    /**
     * 集合类型用例。
     * 字符串类型、负精度值、null值、负整数值、空集合、混合类型。
     *
     * @return {@link Object[]}
     */
    public static Object[] getJSONArrayTypeCases() {
        return new Object[]{
                Collections.singletonList(STRING),
                Collections.singletonList(-1.1),
                Collections.singletonList(null),
                Collections.singletonList(-1),
                Collections.emptyList(),
                Arrays.asList(STRING, -1, -1.1)
        };
    }

    /**
     * 创建指定用例名。
     *
     * @param baseName 基本名称。
     * @param value    值。
     * @return {@link String}
     */
    public static String createSpecifyCaseNameByCaseValue(String baseName, Object value) {
        String caseName = "";
        if ("".equals(value)) {
            caseName = INTERFACE + baseName + "-传空 ".trim();
        } else if (" ".equals(value)) {
            caseName = INTERFACE + baseName + "-传空格 ".trim();
        } else if ("@".equals(value)) {
            caseName = INTERFACE + baseName + "-传特殊符号\"@\" ".trim();
        } else if ("null".equals(value)) {
            caseName = INTERFACE + baseName + "-特殊值null ".trim();
        } else if (STRING.equals(value)) {
            caseName = INTERFACE + baseName + "-传字符类型\"string\" ".trim();
        } else if ("[string]".equals(value)) {
            caseName = INTERFACE + baseName + "-传字符串值类型集合 ".trim();
        } else if ("[-1.1]".equals(value)) {
            caseName = INTERFACE + baseName + "-传精度值类型集合 ".trim();
        } else if ("[null]".equals(value)) {
            caseName = INTERFACE + baseName + "-传null值集合 ".trim();
        } else if ("[-1]".equals(value)) {
            caseName = INTERFACE + baseName + "-传整型值类型集合 ".trim();
        } else if ("[]".equals(value)) {
            caseName = INTERFACE + baseName + "-传空集合 ".trim();
        } else if ("[string, -1, -1.1]".equals(value)) {
            caseName = INTERFACE + baseName + "-传混合数据类型集合 ".trim();
        } else {
            caseName = INTERFACE + baseName + "-传" + value + " ".trim();
        }
        return caseName;
    }

}

  • ExcelUtils (excel manipulation):
package com.example.myproject.util;

import com.example.myproject.pojo.WriteBackData;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.List;

/**
 * Excel 操作。
 *
 * @author Jan
 * @date 2023/08
 */
public class ExcelUtils {

    private ExcelUtils() {

    }

    /**
     * 写出路径。
     */
    private static final String OUT_PATH = "src/main/resources";

    /**
     * 工作表名。
     */
    private static final String SHEET_NAME = "testCase";

    /**
     * 用例写入resource目录。
     *
     * @param writeBackDataList 回写数据列表。
     */
    public static void initFileAndWriteDataToExcel(List<WriteBackData> writeBackDataList) {
        File filePath = new File(OUT_PATH);
        FileUtils.initTestCaseFile(filePath);
        String testCaseFilePath = filePath + File.separator + FileUtils.getFileName();
        ExcelUtils.writeExcel(writeBackDataList, testCaseFilePath);
        System.out.println("            ===> 用例写入完成");
    }

    /**
     * 写入。
     *
     * @param dataList 数据列表。
     * @param filePath 文件路径。
     */
    private static void writeExcel(List<WriteBackData> dataList, String filePath) {
        try (
                XSSFWorkbook workbook = new XSSFWorkbook();
                OutputStream out = new FileOutputStream(filePath)
        ) {
            XSSFSheet sheet = workbook.createSheet(SHEET_NAME);
            // 第一行表头。
            XSSFRow firstRow = sheet.createRow(0);
            XSSFCell[] cells = new XSSFCell[3];
            String[] titles = new String[]{
                    "用例名称",
                    "用例编号",
                    "操作步骤(生成用例后,记得将\"null\"替换为null,9223372036854775807替换为9223372036854775808)"
            };
            // 循环设置表头信息。
            for (int i = 0; i < 3; i++) {
                cells[0] = firstRow.createCell(i);
                cells[0].setCellValue(titles[i]);
            }
            // 遍历数据集合,将数据写入 Excel 中。
            for (int i = 0; i < dataList.size(); i++) {
                XSSFRow row = sheet.createRow(i + 1);
                WriteBackData writeBackData = dataList.get(i);
                //第一列 用例名
                XSSFCell cell = row.createCell(0);
                cell.setCellValue(writeBackData.getCaseName());
                //第二列 用例编号
                cell = row.createCell(1);
                cell.setCellValue(i + 1);
                //第三列 操作步骤
                cell = row.createCell(2);
                cell.setCellValue(writeBackData.getStep());
            }
            workbook.write(out);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

  • FileUtils (use case file operations):
package com.example.myproject.util;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;

/**
 * 文件操作。
 *
 * @author Jan
 * @date 2023/08
 */
public class FileUtils {

    private FileUtils() {

    }

    /**
     * 生成的用例文件名。
     */
    private static final String FILE_NAME = "TestCase.xls";

    /**
     * 读取文件流。
     *
     * @param inputStream 输入流。
     * @return {@link String}
     */
    public static String readFileStream(InputStream inputStream) {
        StringBuilder sb = new StringBuilder();
        try (BufferedReader reader = new BufferedReader(
                new InputStreamReader(inputStream, StandardCharsets.UTF_8))
        ) {
            String line;
            while (null != (line = reader.readLine())) {
                sb.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sb.toString();
    }

    /**
     * 读取请求示例。
     *
     * @return {@link String}
     */
    public static String readSampleRequest() {
        InputStream is = FileUtils.class.getClassLoader().getResourceAsStream("request.json");
        return FileUtils.readFileStream(is);
    }

    /**
     * 用例文件名称。
     *
     * @return {@link String}
     */
    public static String getFileName() {
        return FILE_NAME;
    }

    /**
     * 初始化测试用例文件。
     *
     * @param filePath 文件路径。
     */
    public static void initTestCaseFile(File filePath) {
        Path testFilePath = filePath.toPath().resolve(getFileName());
        try {
            boolean deleted = Files.deleteIfExists(testFilePath);
            System.out.println(deleted ? "初始化开始 ===> 旧用例删除成功" : "用例初始化开始 ===> 旧用例删除失败");
        } catch (NoSuchFileException e) {
            System.err.println("文件未找到:" + filePath);
        } catch (IOException e) {
            System.err.println("删除文件失败:" + e.getMessage());
        }

        try {
            Files.createFile(testFilePath);
            System.out.println("用例初始化结束 ===> 新用例创建成功");
        } catch (IOException e) {
            System.err.println("新用例创建失败:" + e.getMessage());
        }
    }
}

  • JsonPathParser (recursive parsing to get the jsonPath of the leaf node):
package com.example.myproject.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.ParserConfig;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;


/**
 * 解析 json 得到具体字段的 jsonPath。
 *
 * @author Jan
 * @date 2023/08
 */
public class JsonPathParser {

    static {
        // 设置全局白名单,解析 pb3 中的 @type 类型。
        ParserConfig.getGlobalInstance().setAutoTypeSupport(true);
    }

    private JsonPathParser() {
    }

    /**
     * 得到json路径。
     *
     * @param jsonStr json str
     * @return {@link Set}<{@link String}>
     */
    public static Set<String> getJsonPaths(String jsonStr) {
        // 解析JSON字符串为JSON对象。
        JSONObject jsonObj = JSON.parseObject(jsonStr);

        // 存储JSONPath路径的集合。
        Set<String> jsonPaths = new HashSet<>();

        // 递归遍历JSON对象的所有字段,并提取出JSONPath路径。
        parseJsonObj(jsonObj, "$", jsonPaths);

        return jsonPaths;

    }

    /**
     * 解析 json 对象。
     *
     * @param jsonObj    json obj。
     * @param parentPath 父路径。
     * @param jsonPaths  json路径。
     */
    private static void parseJsonObj(JSONObject jsonObj, String parentPath, Set<String> jsonPaths) {

        for (Map.Entry<String, Object> entry : jsonObj.entrySet()) {
            String key = entry.getKey();
            // 跳过PBv3的类型标识。
            if (key.contains("@type")) {
                continue;
            }
            Object value = jsonObj.get(key);
            String currPath = parentPath + "." + key;

            // 将当前字段的JSONPath路径添加到集合中。
            jsonPaths.add(currPath);

            if (value instanceof JSONObject) {
                // 递归处理嵌套的JSON对象。
                parseJsonObj((JSONObject) value, currPath, jsonPaths);
            } else if (value instanceof JSONArray) {
                // 递归处理嵌套的JSON数组。
                parseJsonArray((JSONArray) value, currPath, jsonPaths);
            }
        }
    }

    /**
     * 解析 json 数组。
     *
     * @param jsonArray  json数组。
     * @param parentPath 父路径。
     * @param jsonPaths  json路径。
     */
    private static void parseJsonArray(JSONArray jsonArray, String parentPath, Set<String> jsonPaths) {
        for (int i = 0; i < jsonArray.size(); i++) {
            // 只取集合中第一个元素的字段。
            if (0 < i) {
                continue;
            }

            Object value = jsonArray.get(i);
            String currPath = parentPath + "[" + i + "]";

            if (value instanceof JSONObject) {
                // 递归处理嵌套的JSON对象。
                parseJsonObj((JSONObject) value, currPath, jsonPaths);
            } else if (value instanceof JSONArray) {
                // 递归处理嵌套的JSON数组。
                parseJsonArray((JSONArray) value, currPath, jsonPaths);
            }
        }
    }
}

  • JsonUtils (set use case value):
package com.example.myproject.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONPath;


/**
 * 替换字段值。
 *
 * @author Jan
 * @date 2023/08
 */
public class JsonUtils {

    private JsonUtils() {

    }

    /**
     * 检查并设置json路径值。
     *
     * @param json          json。
     * @param jsonPath      json路径。
     * @param testCaseValue 测试用例价值。
     * @return {@link JSONObject}
     */
    public static JSONObject checkAndSetJsonPathValue(String json, String jsonPath, Object testCaseValue) {
        JSONObject jsonObject = null;
        try {
            jsonObject = JSON.parseObject(json);
            // 还原直接替换值 testCaseValue。
            JSONPath.set(jsonObject, jsonPath, testCaseValue);

        } catch (Exception e) {
            System.err.println("error case:" + jsonPath);
        }
        return jsonObject;
    }
}

5. Implementation effect

have a test.

  • The tested json string :
{
    "String":"str",
    "Int":1,
    "Float":0.1,
    "Long":622337203685477500,
    "Bool":true,
    "test":{
        "Array":[

        ]
    }
}
  • Run the test :

  • Generated final use case :

6. Other instructions

  • Supports protobuf v3 converted json (@type type).
  • Advantages of script use case generation:
    • High efficiency, reducing redundant operations.
    • Avoid writing wrong use cases due to human error.
    • It is convenient for later use case iteration.
  • To sum up, in jsonthe case of many fields, the efficiency improvement is particularly obvious.
  • Source address: gitee.com/Jan7/datafa…

Finally: The complete software testing video tutorial below has been sorted out and uploaded, and friends who need it can get it by themselves [Guaranteed 100% free]

Software Testing Interview Documentation

We must study to find a high-paying job. The following interview questions are the latest interview materials from first-tier Internet companies such as Ali, Tencent, and Byte, and some Byte bosses have given authoritative answers. Finish this set The interview materials believe that everyone can find a satisfactory job.

Guess you like

Origin blog.csdn.net/wx17343624830/article/details/132669369