• 自定义testng Report


    package com.report;
    
    import java.sql.*;
    import java.text.SimpleDateFormat;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.List;
    import java.util.Map;
    import java.util.Set;
    import org.testng.IReporter;
    import org.testng.IResultMap;
    import org.testng.ISuite;
    import org.testng.ISuiteResult;
    import org.testng.ITestContext;
    import org.testng.ITestResult;
    import org.testng.xml.XmlSuite;
    
    import com.mysql.jdbc.Connection;
    import com.mysql.jdbc.PreparedStatement;
     
    public class Report implements IReporter{
        static String mySqlUrl= GlobalSettings.MySqlUrl;
        static String mySqlUserName=GlobalSettings.mySqlUserName;    
        static String mySqlPassword=GlobalSettings.mySqlPassword;     
        
        @Override
        public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) {
            List<ITestResult> list = new ArrayList<ITestResult>();
            for (ISuite suite : suites) {          
                Map<String, ISuiteResult> suiteResults = suite.getResults();
                for (ISuiteResult suiteResult : suiteResults.values()) {
                    ITestContext testContext = suiteResult.getTestContext();           
                    IResultMap passedTests = testContext.getPassedTests();
                    IResultMap failedTests = testContext.getFailedTests();
                    IResultMap skippedTests = testContext.getSkippedTests();
                    IResultMap failedConfig = testContext.getFailedConfigurations();   
                    list.addAll(this.listTestResult(passedTests));
                    list.addAll(this.listTestResult(failedTests));
                    list.addAll(this.listTestResult(skippedTests));
                    list.addAll(this.listTestResult(failedConfig));
                }          
            }
            this.sort(list);
            setPrjName();
            this.outputResult(list, outputDirectory+"/test.txt");
        }
    
        public void setPrjName() {
    //        GlobalSettings.REPORT_PRJ_NAME = "test";
        }
    
        private ArrayList<ITestResult> listTestResult(IResultMap resultMap){
            Set<ITestResult> results = resultMap.getAllResults();    
            return new ArrayList<ITestResult>(results);
        }
         
        private void sort(List<ITestResult> list){
            Collections.sort(list, new Comparator<ITestResult>() {
                @Override
                public int compare(ITestResult r1, ITestResult r2) {
                    if(r1.getStartMillis()>r2.getStartMillis()){
                        return 1;
                    }else{
                        return -1;
                    }              
                }
            });
        }
         //把报告的信息存入数据库
        private void outputResult(List<ITestResult> list, String path){      
            Connection conn=getConn();
            String projectName = GlobalSettings.REPORT_PRJ_NAME;
            String strInsert="";
            strInsert = "insert into test_log(ProjectName,ModuleName,TestName,BeginTime,EndTime,LastTime,Status,Description,BatchNo) values(?,?,?,?,?,?,?,?,?)";
            try {
                String date = new SimpleDateFormat("yyyy-MM-dd").format(new java.util.Date());
                String batchNo = date;
                Statement sta = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
                ResultSet resultSet = sta.executeQuery("SELECT BatchNo from test_log WHERE ProjectName = '" + projectName + "' AND BatchNo like '" + date + "%' ORDER BY BatchNo DESC LIMIT 1");
                resultSet.last();
                int rowCount = resultSet.getRow();
                resultSet.first();
                if (1 == rowCount){
                    String dbBatchNo = resultSet.getString(1);
                    if (dbBatchNo.indexOf("_") != -1){
                        String str = dbBatchNo.split("_")[1];
                        if (!str.equals("")){
                            try{
                                batchNo = batchNo + "_" + addZeroToIntStr(Integer.parseInt(str) + 1,3);
                            }catch (Exception e){
                                e.printStackTrace();
                            }
                        }
                    }else {
                        batchNo = batchNo + "_" + addZeroToIntStr(1, 3);
                    }
                }
                PreparedStatement prest = (PreparedStatement) conn.prepareStatement(strInsert);
                for (ITestResult result : list) {
                    prest.setString(1, projectName);
                    prest.setString(2, result.getMethod().getXmlTest().getName());
                    prest.setString(3, result.getMethod().getMethodName());
                    Timestamp startTime=new Timestamp(result.getStartMillis());
                    Timestamp endTime=new Timestamp(result.getEndMillis());
                    prest.setTimestamp(4, startTime);
                    prest.setTimestamp(5, endTime);
                    Float tempLast=(float) (result.getEndMillis()-result.getStartMillis());
                    Float lastTime=tempLast/1000;
                    prest.setString(6,lastTime.toString());
                    prest.setString(7, this.getStatus(result.getStatus()));
                    prest.setString(8, result.getMethod().getDescription());
                    prest.setString(9, batchNo);
                    prest.execute();
                    conn.commit();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }finally{
                close(conn);
            }
        }
         
        public String getStatus(int status){
            String statusString = null;
            switch (status) {      
            case 1:
                statusString = "SUCCESS";
                break;
            case 2:
                statusString = "FAILURE";
                break;
            case 3:
                statusString = "SKIP";
                break;
            default:           
                break;
            }
            return statusString;
        }
    
        
        /** 
         * 创建连接 
         * @return 
         */  
        public static Connection getConn(){  
            try {          
                    Class.forName("com.mysql.jdbc.Driver");          
                    Connection conn = (Connection) DriverManager.getConnection(mySqlUrl, mySqlUserName, mySqlPassword);       
                    conn.setAutoCommit(false);    
                    return conn;  
            } catch (Exception ex) {    
                ex.printStackTrace();    
            }
            return null;  
        }  
        /** 
         * 关闭连接 
         * @return 
         */  
        public static void close(Connection conn){  
             try {    
                 if(conn!=null){  
                     conn.close();    
                 }  
             } catch (SQLException e) {    
                 e.printStackTrace();    
             }  
        }
    
        public static String addZeroToIntStr(Integer num, int zeroNum) {
            if(num==null) {
                return null;
            }else if(zeroNum<=0) {
                return num+"";
            }else {
                String numStr = num + "";
                int numLength  = numStr.length();
                if(zeroNum>numLength) {
                    for(int i=0; i<(zeroNum-numLength); i++) {
                        numStr = "0" + numStr;
                    }
                }
                return numStr;
            }
        }
     
    }
  • 相关阅读:
    ios数据存储方式FMDB
    ios中的coredata
    编程岗位电话面试问答Top 50[转]
    活动图
    时序图
    用例图
    类图
    Movie
    EasyMock
    cglib应用
  • 原文地址:https://www.cnblogs.com/hong0632/p/8341949.html
Copyright © 2020-2023  润新知