杭州线下第一期课程 3_接口测试进阶

杭州线下第一期课程3_接口测试进阶

数据驱动:

DataProvider常用方法

    @DataProvider(name = "providerDemo")
    public Object[][] createData() {
        return new Object[][]{
                {"TestHome", "sihan"},
                {"Netease", "lihe"},
        };
    }

    @Test(dataProvider = "providerDemo")
    public void verifyData(String company, String people) {
        System.out.println(company + " " + people);
    }

DataProvider扩展

从csv中读取数据

public class CsvReader {

    public static Object[][] getTestData(String fileName) throws IOException {

        List<Object[]> records = new ArrayList<Object[]>();
        String record;
        /**
         * 设定UTF-8字符集,使用带缓冲区的字符输入流BufferedReader读取文件内容
         */

        BufferedReader file = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), "UTF-8"));

        /**
         * 忽略读取CSV文件的标题行(第一行)
         */

        file.readLine();

        /**
         * 遍历读取文件中除第一行外的其他所有行内容 并存储在名为records的ArrayList中
         * 每一个recods中存储的对象为一个String数组
         */
        while ((record = file.readLine()) != null) {
            String fileds[] = record.split(",");
            records.add(fileds);
        }
        file.close();

        /**
         *   定义函数返回值,即Object[][]
         *   将存储测试数据的list转换为一个Object的二维数组
         */

        Object[][] result = new Object[(records.size())][];
        for (int i = 0; i < records.size(); i++) {
            result[i] = records.get(i);
        }
        return result;
    }


}

从excel中读取数据(添加poi依赖)

public class ExcelReader {


    public static Object[][] getTestData(String fileName, String sheetName) throws IOException {


        File file = new File(fileName);
        FileInputStream inputStream = new FileInputStream(file);
        Workbook workbook = null;

        /**
         *  获取文件名参数的拓展名,判断是.xlsx还是xls
         */

        String fileExtensionName = fileName.substring(fileName.indexOf("."));
        /**
         *   如果是.xlsx,则使用XSSFWorkbook对象进行实例化
         *   如果是.xls,则使用HSSFWorkbook对象进行实例化
         */

        if (fileExtensionName.equals(".xlsx")) {
            workbook = new XSSFWorkbook(inputStream);
        } else if (fileExtensionName.equals(".xls")) {
            workbook = new HSSFWorkbook(inputStream);
        } else {
            System.out.println("该文件不是excel文件");
        }

        /**
         *  通过sheetName参数,生成sheet对象
         */

        Sheet sheet = workbook.getSheet(sheetName);

        /***
         * 获取Excel数据文件sheet1中数据的行数,getLastRowNum方法获取数据的最后一行行号
         * getFirstRowNum方法获取数据的第一行行号,相减之后算出数据的行数
         * Excel的行号和列号都是从0开始的
         */
        int rowCount = sheet.getLastRowNum() - sheet.getFirstRowNum();

        /***
         *  创建名为records的list对象来存储从Excel中读取的数据
         */

        List<Object[]> records = new ArrayList<Object[]>();

        /**
         *  使用两个for循环遍历Excel数据文件的所有数据。(除去第一行的列名称)
         */
        for (int i = 1; i <= rowCount; i++) {
            Row row = sheet.getRow(i);

            /**
             *  声明一个数组files,用来存储excel数据文件每行数据,数组的大小用getLastCellNum方法来进行动态声明。
             */

            String files[] = new String[row.getLastCellNum()];


            for (int j = 0; j < row.getLastCellNum(); j++) {
                Cell cell =row.getCell(j);
                cell.setCellType(CellType.STRING);
                files[j] = cell.getStringCellValue();
            }
            records.add(files);
        }

        Object[][] results = new Object[records.size()][];
        for (int i = 0; i < records.size(); i++) {
            results[i] = records.get(i);
        }

        return results;

    }
}

接口源码解析(javaparser)

/**
 * @author by LiHe on 2018/8/12.14:14
 */


public class JavaParseDemo extends VoidVisitorAdapter {

    private String rooturl;


    private Set<MethodDeclaration> allEmement = new HashSet<>();

    private Set<MethodDeclaration> getAllMethod(CompilationUnit cu) {
        this.visit(cu, null);
        return allEmement;
    }

    @Override
    public void visit(MethodDeclaration n, Object arg) {

        allEmement.add(n);
    }

    @Test
    public void testJavaCode() throws IOException {

        FileInputStream in = new FileInputStream("file/BaseController.java");


        /***
         * javaParser初始化,得到一个CompilationUnit树
         */
        CompilationUnit cu = JavaParser.parse(in);
        in.close();


        /***
         * 这里是取class上方的注解,得到value="/test/api"根URL
         */
        List<Node> culist = cu.getChildNodes();

        for (Node cuunit : culist) {

            if (cuunit instanceof ClassOrInterfaceDeclaration) {
                ClassOrInterfaceDeclaration classOrInterfaceDeclaration = (ClassOrInterfaceDeclaration) cuunit;
                List<Node> annotationExprs = classOrInterfaceDeclaration.getChildNodes();
                for (Node annotationExpr : annotationExprs) {
                    if (annotationExpr instanceof NormalAnnotationExpr) {
                        NormalAnnotationExpr temp = (NormalAnnotationExpr) annotationExpr;
                        for (Node temp2 : temp.getChildNodes()) {
                            if (temp2 instanceof MemberValuePair) {
                                MemberValuePair memberValuePair = (MemberValuePair) temp2;
                                if ("value".equalsIgnoreCase(memberValuePair.getName().toString())) {
                                    rooturl =memberValuePair.getValue().toString().replace("\\"","");
                                    System.out.println("URL: " + memberValuePair.getValue().toString().replace("\\"",""));
                                }


                            }
                        }

                    }

                }

            }


        }


        /***
         * 解析方法中的注解、请求方式和参数类型
         */
        for (MethodDeclaration m : this.getAllMethod(cu)) {

            System.out.println("=======================");
            List<AnnotationExpr> listAnnotation = m.getAnnotations();

            for (AnnotationExpr annotationExpr : listAnnotation) {

                if ("RequestMapping".equalsIgnoreCase(annotationExpr.getName().toString())) {

                    List<Node> childNode = annotationExpr.getChildNodes();

                    for (Node n : childNode) {
                        if (n instanceof MemberValuePair) {
                            MemberValuePair memberValuePair = (MemberValuePair) n;
                            if ("value".equalsIgnoreCase(memberValuePair.getName().toString())) {
                                System.out.println("接口地址: " +rooturl+ memberValuePair.getValue().toString().replace("\\"",""));
                            }
                            if ("method".equalsIgnoreCase(memberValuePair.getName().toString())) {
                                System.out.println("请求方式: " + memberValuePair.getValue());
                            }

                        }

                    }
                }

            }
            List<Parameter> parameterList = m.getParameters();


            for (Parameter parameter : parameterList) {


                List<Node> requestParamList = parameter.getChildNodes();

                for (Node requestParam : requestParamList) {

                    if (requestParam instanceof NormalAnnotationExpr) {
                        List<Node> valueList = requestParam.getChildNodes();

                        for (Node value : valueList) {

                            if (value instanceof MemberValuePair) {

                                MemberValuePair tmp = (MemberValuePair) value;

                                if ("value".equalsIgnoreCase(tmp.getName().toString())) {

                                    System.out.println("参数名:  " + tmp.getValue().toString().replace("\\"",""));

                                }

                                if ("required".equalsIgnoreCase(tmp.getName().toString())) {
                                    System.out.println("是否必传:" + tmp.getValue());
                                }


                            }


                        }

                    }

                }

                if (!parameter.getType().toString().equalsIgnoreCase("HttpServletRequest") && !parameter.getType().toString().equalsIgnoreCase("HttpServletResponse")) {
                    System.out.println("参数类型: " + parameter.getType().toString());
                }

            }
        }


    }
}

Jenkins驱动