单元测试工具(连载5)

简介: 单元测试工具(连载5)

1.8 使用JAVA脚本发送测试报告


测试报告产生了,为了配合CI的实现,可以用JAVA来实现发送测试报告到相关人员的邮件系统中,代码如下。


案例3:利用JAVA发送电子邮件。


package Util.com.jerry;
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.AuthenticationFailedException;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
/**
 * 实现邮件发送功能
 * @authorAdministrator
 *
 */
public class EmailSender {
    privatestatic final Logger logger = LogManager.getLogger(EmailSender.class);
    privateString host; // 服务器地址
    privateString from; // 发件人
    privateString to; // 收件人 多个收件人以,分隔
    privateString title; // 主题
    privateString content; // 内容
    privateList<File> attachmentlist ; //附件集
    privateString username; // 用户名
    privateString password; // 密码
    /**发件人员工编号*/
    privateString sendEmployeeId;
    publicString getSendEmployeeId() {
        returnsendEmployeeId;
    }
    publicvoid setSendEmployeeId(String sendEmployeeId) {
        this.sendEmployeeId= sendEmployeeId;
    }
    publicString getHost() {
        returnhost;
    }
    publicvoid setHost(String host) {
        this.host= host;
    }
    publicString getFrom() {
        returnfrom;
    }
    publicvoid setFrom(String from) {
        this.from= from;
    }
    publicString getTo() {
        returnto;
    }
    publicvoid setTo(String to) {
        this.to= to;
    }
    publicString getTitle() {
        returntitle;
    }
    publicvoid setTitle(String title) {
        this.title= title;
    }
    publicString getContent() {
        returncontent;
    }
    publicvoid setContent(String content) {
        this.content= content;
    }
    publicList<File> getAttachmentlist() {
        returnattachmentlist;
    }
    publicvoid setAttachmentlist(List<File> attachmentlist) {
        this.attachmentlist= attachmentlist;
    }
    publicString getUsername() {
        returnusername;
    }
    publicvoid setUsername(String username) {
        this.username= username;
    }
    publicString getPassword() {
        returnpassword;
    }
    publicvoid setPassword(String password) {
        this.password= password;
    }
    publicString getPort() {
        returnport;
    }
    publicvoid setPort(String port) {
        this.port= port;
    }
    privateString port;
    publicEmailSender(String host, String from, String to, String title,
            Stringcontent, List attachmentlist, String username, String password,String port) {
        this.host= host;
        this.from= from;
        this.to= to;
        this.title= title;
        this.content= content;
        this.attachmentlist= attachmentlist;
        this.username= username;
        this.password= password;
        this.port=port;
    }
    publicEmailSender(String to, String title,
            Stringcontent, List attachmentlist) {
        this.to= to;
        this.title= title;
        this.content= content;
        this.attachmentlist= attachmentlist;
    }
    /**
     * 发送邮件
     * @return 发送状态信息index0:状态 0成功 1失败;index1:描述错误信息
     */
    publicString[] sendMail(){
        String[]result=new String[2];
        Sessionsession=null;
        Propertiesprops = System.getProperties();
        props.put("mail.smtp.host",host);
        props.put("mail.smtp.sendpartial","true");
        props.put("mail.smtp.port",port);
        if(StringUtils.isBlank(username)){//不需要验证用户名密码
            session= Session.getDefaultInstance(props, null);
        }else{
            props.put("mail.smtp.auth","true");
            EmailAuthenticatorauth = new EmailAuthenticator(username, password);
            session= Session.getInstance(props, auth);
        }
        //设置邮件发送信息
        try{
            //创建邮件
            MimeMessagemessage = new MimeMessage(session);
            //设置发件人地址
            message.setFrom(newInternetAddress(from));
            //设置收件人地址(多个邮件地址)
            InternetAddress[]toAddr = InternetAddress.parse(to);
            message.addRecipients(Message.RecipientType.TO,toAddr);
            //设置邮件主题
            message.setSubject(title);
            //设置发送时间
            message.setSentDate(newDate());
            //设置发送内容
            Multipartmultipart = new MimeMultipart();
            MimeBodyPartcontentPart = new MimeBodyPart();
            contentPart.setText(content);
            multipart.addBodyPart(contentPart);
            //设置附件
            if(attachmentlist!=null&& attachmentlist.size()>0){
                for(inti = 0 ; i < attachmentlist.size();i++){
                    MimeBodyPartattachmentPart = new MimeBodyPart();
                    FileDataSourcesource = new FileDataSource(attachmentlist.get(i));
                    attachmentPart.setDataHandler(newDataHandler(source));
                    attachmentPart.setFileName(MimeUtility.encodeWord(attachmentlist.get(i).getName(),"gb2312", null));
                    multipart.addBodyPart(attachmentPart);
                }
            }
            message.setContent(multipart);
            //登录SMTP服务器
            if(StringUtils.isBlank(username)) {
                //不需验证
                Transport.send(message);
            }else {
                //需要验证
                Transporttransport = session.getTransport("smtp");
                transport.connect();
                transport.sendMessage(message,message.getAllRecipients());
                transport.close();
            }
            result[0]="0";
            result[1]="发送成功";
            logger.info("邮件发送成功!发送人:"+from);
        }catch(MessagingExceptionmex){
            result[0]="1";
            result[1]="邮件服务器发生错误";
            if(mexinstanceof AuthenticationFailedException){
                result[1]="用户名或密码错误";
            }
        }catch (Exception e) {
            result[0]="1";
            result[1]="系统异常";
        }
        returnresult;
    }
    publicstatic void main(String[] args){
        StringSNMPTServer = "smtp.126.com";
        Stringfrom="xianggu625@126.com";
        Stringto="hedan@cmss.chinamobile.com";
        Stringtitle="发送测试报告";
        Stringcontent="附件为测试报告";
        Stringusername="xianggu625@126.com";
        Stringpassword="123456";
        Stringport="25";
        Listlist=new ArrayList();
        list.add(newFile("C:\\myjava\\web\\junit.rar"));#junit.rar为发送测试报告的目录压缩jar包
        EmailSendersender=newEmailSender(SNMPTServer,from,to,title,content,list,username,password,port);
        String[] result = sender.sendMail();
        System.out.println(result[1]+"ffffffffffffffff");
    }
}
/**
 * classMyAuthenticator用于邮件服务器认证 构造器需要用户名、密码作参数
 */
class EmailAuthenticator extends Authenticator {
    privateString username = null;
    privateString password = null;
    publicEmailAuthenticator(String username, String password) {
        this.username= username;
        this.password= password;
    }
    publicPasswordAuthentication getPasswordAuthentication() {
        returnnew PasswordAuthentication(username, password);
    }
}


读者只要修改main()方法开始部分的设置就可以了。


星云测试

http://www.teststars.cc

奇林软件

http://www.kylinpet.com

联合通测

http://www.quicktesting.net


顾翔凡言:

k=(p+m)t

其中:

k为常数。

p:团队人员质量水平,为单位小时内产生的有效质量,单位为/h;

m:团队方法质量水平,为单位小时内产生的有效质量,单位为/h;

t:为单位质量产品的交付时间,单位为h。


在团队方法质量水平不变,团队人员质量水平提高的情况下,交付时间变短;

在团队人员质量水平不变,团队方法质量水平提高的情况下,交付时间变短;

团队人员质量水平与方法质量水平乘积决定了软件的质量水平,如果这个值变小,则t变大;反之t变小。


例如,当k=4时:

当人员质量水平为1/小时、方法水平为1/小时时,交付时间为2小时。

当人员质量水平为2/小时、方法水平为1/小时时,交付时间为4/3小时。

当人员质量水平为1/小时、方法水平为2/小时时,交付时间为4/3小时。

当人员质量水平为0.5/小时、方法水平为0.5/小时时,交付时间为4小时。

目录
相关文章
|
21天前
|
测试技术 C语言
网站压力测试工具Siege图文详解
网站压力测试工具Siege图文详解
27 0
|
2月前
|
JavaScript jenkins 测试技术
这10款性能测试工具,收藏起来,测试人的工具箱!
这10款性能测试工具,收藏起来,测试人的工具箱!
|
2月前
|
测试技术
现代软件测试中的自动化工具与挑战
传统软件测试面临着越来越复杂的系统架构和不断增长的测试需求,自动化测试工具应运而生。本文将探讨现代软件测试中自动化工具的应用和挑战,深入分析其优势与局限性,为软件测试领域的发展提供思路和启示。
|
2月前
|
测试技术 持续交付
现代软件测试中的自动化工具应用与挑战
随着信息技术的快速发展,软件行业对于质量和效率的要求日益提高,自动化测试工具在软件开发过程中扮演着至关重要的角色。本文将探讨现代软件测试中自动化工具的应用现状以及所面临的挑战,旨在帮助开发人员更好地理解并充分利用这一技术手段。
|
4天前
|
机器学习/深度学习 数据采集 人工智能
【专栏】利用AI辅助工具提高软件测试效率与准确性
【4月更文挑战第27天】本文探讨了AI在软件测试中的应用,如自动执行测试用例、识别缺陷和优化测试设计。AI辅助工具利用机器学习、自然语言处理和图像识别提高效率,但面临数据质量、模型解释性、维护更新及安全性挑战。未来,AI将更注重用户体验,提升透明度,并在保护隐私的同时,通过联邦学习等技术共享知识。AI在软件测试领域的前景广阔,但需解决现有挑战。
|
2月前
|
jenkins 测试技术 持续交付
现代软件测试中的自动化工具与挑战
随着软件开发领域的不断发展,自动化测试工具在测试过程中扮演着越来越重要的角色。本文将探讨现代软件测试中自动化工具的应用及面临的挑战,旨在帮助开发人员和测试人员更好地理解和应对自动化测试中的问题。
|
17小时前
|
敏捷开发 监控 测试技术
探索自动化测试工具Selenium Grid的高效集成策略
【4月更文挑战第30天】在现代Web应用的快速迭代和持续部署中,测试自动化已成为确保产品质量的关键。Selenium Grid作为一款支持多种浏览器和操作系统的测试工具,提供了并行执行测试用例的能力,极大地提升了测试效率。本文将深入探讨如何高效地将Selenium Grid集成到现有的测试框架中,以及实施过程中的最佳实践,帮助团队最大化测试覆盖率,同时降低资源消耗。
|
23小时前
|
中间件 测试技术 API
探索自动化测试工具的新边界:Selenium与Appium的集成实践
【4月更文挑战第30天】 随着移动应用和Web应用的不断融合,传统的自动化测试工具需要适应新的测试环境。本文将详细分析Selenium和Appium这两款流行的自动化测试工具的集成实践,探讨如何构建一个能够同时支持Web和移动端应用的自动化测试框架。通过对比两者的技术架构、功能特性以及在实际项目中的集成过程,我们旨在为读者提供一个清晰的指导,帮助他们在复杂的应用环境中实现高效、稳定的自动化测试流程。
|
1天前
|
机器学习/深度学习 人工智能 机器人
深入理解自动化测试:框架、工具与实践
【4月更文挑战第30天】 在现代软件开发周期中,自动化测试已成为确保产品质量和加速市场交付的关键环节。本文将探讨自动化测试的核心框架、常用工具以及实际应用的最佳实践,旨在为软件测试工程师提供深入的理解和有效的策略,以改进其自动化测试流程。我们将分析几种流行的测试自动化框架,包括Selenium、Appium和JUnit,并讨论如何根据项目需求选择适合的工具。此外,文中还将介绍持续集成(CI)环境下的自动化测试策略,以及如何通过测试结果分析和报告来优化测试过程。目标是帮助读者构建更健壮、更高效的自动化测试系统。
|
1天前
|
IDE 测试技术 持续交付
探索自动化测试工具Selenium的高效应用
【4月更文挑战第29天】 在快速迭代的软件开发过程中,高效的测试策略是确保产品质量的关键。本文将深入探讨如何利用自动化测试工具Selenium来提高软件测试的效率和准确性。通过介绍Selenium的核心功能、脚本编写技巧以及与持续集成环境的集成方法,我们旨在为读者提供一个全面的Selenium应用指南。此外,我们还将讨论常见的问题解决策略,并通过案例分析展示如何有效地运用Selenium进行复杂的Web应用测试。