1. 程式人生 > >testNG+javamail —— 傳送測試報告

testNG+javamail —— 傳送測試報告

1.配置

  1. 專案->右擊->Build Path->Add Libraries->選擇testNG->點選Finish
  2. 引入jar包
  3. 在xml檔案中新增監聽器

<listeners> 
   <listener class-name="com.webtest.core.WebTestListener" /> ---------------------------這個為想要執行的
   <listener class-name="org.uncommons.reportng.HTMLReporter"/>----------------------加上這個才能執行reportng
   <listener class-name="org.uncommons.reportng.JUnitXMLReporter"/>------------------加上這個才能執行reportng		
</listeners>

   4. Project->properties->點左邊的testng->勾選Disable default listeners->

        填入org.uncommons.reportng.HTMLReporter->點選apply

2.使用java實現檔案的壓縮

ZipCompressor.java

package com.reportng;

import java.io.BufferedInputStream;    
import java.io.File;    
import java.io.FileInputStream;    
import java.io.FileOutputStream;    
import java.util.zip.CRC32;    
import java.util.zip.CheckedOutputStream;    
  
import org.apache.log4j.Logger;  
import org.apache.tools.zip.ZipEntry;    
import org.apache.tools.zip.ZipOutputStream;    
  
/** 
 * @ClassName: ZipCompressor 
 * @Description: 壓縮檔案的通用工具類-採用org.apache.tools.zip.ZipOutputStream實現,較複雜。 
 * 
 */  
public class ZipCompressor {  
    private Logger logger = Logger.getLogger(ZipCompressor.class);  
    static final int BUFFER = 8192;    
    private File zipFile;    
      
    /** 
     * 壓縮檔案建構函式 
     * @param pathName 壓縮的檔案存放目錄 
     */  
    public ZipCompressor(String pathName) {    
        zipFile = new File(pathName);    
    }    
    
    /** 
     * 執行壓縮操作 
     * @param srcPathName 被壓縮的檔案/資料夾 
     */  
    public void compressExe(String srcPathName) {    
        File file = new File(srcPathName);    
        if (!file.exists()){  
            throw new RuntimeException(srcPathName + "不存在!");    
        }  
        try {    
            FileOutputStream fileOutputStream = new FileOutputStream(zipFile);    
            CheckedOutputStream cos = new CheckedOutputStream(fileOutputStream,new CRC32());    
            ZipOutputStream out = new ZipOutputStream(cos);    
            String basedir = "";    
            compressByType(file, out, basedir);    
            out.close();    
        } catch (Exception e) {   
            e.printStackTrace();  
            logger.error("執行壓縮操作時發生異常:"+e);  
            throw new RuntimeException(e);    
        }    
    }    
    
    /** 
     * 判斷是目錄還是檔案,根據型別(檔案/資料夾)執行不同的壓縮方法 
     * @param file  
     * @param out 
     * @param basedir 
     */  
    private void compressByType(File file, ZipOutputStream out, String basedir) {    
        /* 判斷是目錄還是檔案 */    
        if (file.isDirectory()) {    
            logger.info("壓縮:" + basedir + file.getName());    
            this.compressDirectory(file, out, basedir);    
        } else {    
            logger.info("壓縮:" + basedir + file.getName());    
            this.compressFile(file, out, basedir);    
        }    
    }    
    
    /** 
     * 壓縮一個目錄 
     * @param dir 
     * @param out 
     * @param basedir 
     */  
    private void compressDirectory(File dir, ZipOutputStream out, String basedir) {    
        if (!dir.exists()){  
             return;    
        }  
             
        File[] files = dir.listFiles();    
        for (int i = 0; i < files.length; i++) {    
            /* 遞迴 */    
            compressByType(files[i], out, basedir + dir.getName() + "/");    
        }    
    }    
    
    /** 
     * 壓縮一個檔案 
     * @param file 
     * @param out 
     * @param basedir 
     */  
    private void compressFile(File file, ZipOutputStream out, String basedir) {    
        if (!file.exists()) {    
            return;    
        }    
        try {    
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));    
            ZipEntry entry = new ZipEntry(basedir + file.getName());    
            out.putNextEntry(entry);    
            int count;    
            byte data[] = new byte[BUFFER];    
            while ((count = bis.read(data, 0, BUFFER)) != -1) {    
                out.write(data, 0, count);    
            }    
            bis.close();    
        } catch (Exception e) {    
            throw new RuntimeException(e);    
        }    
    }    
}  

 TestZip.java

package com.reportng;

import org.testng.annotations.Test;

public class TestZip {
	@Test
	 public void nihao(){  
		//壓縮後的存放路徑
	        ZipCompressor zc = new  ZipCompressor("E:\\z_eclipse_workspace\\framework\\auto2018\\test-output\\html.zip");  
	    //壓縮誰
	        zc.compressExe("E:\\z_eclipse_workspace\\framework\\auto2018\\test-output\\html");
	    }  
}

 testng.xml

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name="s1" verbose="1" >
	<listeners> 
		<listener class-name="org.uncommons.reportng.HTMLReporter"/>
    	<listener class-name="org.uncommons.reportng.JUnitXMLReporter"/>
	</listeners>

	<test name="test1">
		<classes>
			<class name="com.webtest.demo.demo" />
			<class name="com.webtest.demo.javamail" />
		</classes>
	</test>
</suite>

 3.把壓縮後的檔案路徑作為一個附件傳給javamail傳送

 4.執行.xml檔案