您的位置:首页 > 大数据 > 人工智能

net自动化测试之道API测试-自动发送Email报告测试失败

2011-08-15 22:41 513 查看
Programmatically Sending E-mail Alerts on TestCase Failures

自动发送Email报告测试失败

Problem

You want your test harness to programmaticallysend an e-mail message when a test case fails.

问题

当测试用例执行失败时,如何让我们的测试套件自动发送email报告失败信息?

Design

Use the System.Web.Mail class to create aMailMessage object.Supply properties such as To and

Subject,and add details of the test casefailure to the Body property.

设计

使用System.Web.Mail类创建MailMessage对象,为该对象提供如To、Subject、Body属性,并且将测试用例的详细失败信息赋给Body属性。

解决方案

if(method=="ArithmeticMean")

{

actual=MathLib.Methods.ArithmeticMean(input);

if(actual.ToString("F4")==expected)

{

Console.WriteLine("Pass");

}

Else

{

Console.WriteLine("*FAIL*.Sendinge-mail");

try

{

MailMessage m=new MailMessage();

m.From="Test Automation Harness";

m.To="you@somewhere.com";

m.Subject="Test Case Failure";

m.BodyEncoding=System.Text.Encoding.ASCII;

m.BodyFormat=MailFormat.Html;

m.Priority=MailPriority.High;

m.Body="Testcase"+caseID+"failed";

SmtpMail.SmtpServer="127.0.0.1";

SmtpMail.Send(m);

}

catch(Exception ex)

{

Console.WriteLine("Fatal error sendingmail:"+ex.Message);

}

}//test case failed

}

Comments

Because test automation often runsunattended,you may want to send an e-mail message to

yourself or one of your team members when atest case fails,so that the test case failure does

not lie unnoticed in a log filesomewhere.You may also want to send an e-mail message summa-

rizing the test run results.There areseveral ways to programmatically send e-mail in a.NET

environment,but using the MailMessage classin the System.Web.Mail namespace is usually

the easiest.The code in this solutionassumes you have first added a project reference to the

System.Web namespace(it’s not accessible bya console application by default)and placed a

using System.Web.Mail; statement in your harness code.

After instantiating a MailMessageobject,you supply values for properties From,To,

Subject,BodyEncoding,BodyFormat,andBody.You can also set the optional Priorityproperty

to MailPriority.High.Youset the BodyEncoding value to one of the encodingrepresentations

from System.Text.Encoding.Youwill usually use Encoding.ASCII,but you can use

Encoding.Unicode or Encoding.UTF8 if youwant to.The BodyFormat property can be set to

MailFormat.Html or MailFormat.Text.Either will workfine as long as there are no quirks in

your e-mail system.The Body propertyis a string that holds the text of the message.At a mini-

mum,you’ll want to include the test case IDthat failed and the actual and expected return

values.

In theory,programmatically sending e-mailis easy,but in practical terms,a lot of things

can go wrong.You have to deal with relayservers,proxy servers,network security policies,and

firewalls,just to name a few.Because ofthis,your best strategy is to make sure that whenever

feasible,your test harness machine isrunning as isolated as possible from other machines.

This will help prevent unintended sideeffects as well as make sending e-mail from your test

harness much more reliable.

注解

由于自动化测试运行是无人看管的,因此我们在测试用例执行失败的时候想发邮件给自己或给团队成员告知测试失败,这样不至于忽略日志文件中某处的失败记录。有时候,我们可能也想通过发邮件了解自动化执行的一些结果摘要信息。在.NET环境下自动发送email有多种实现方式,但使用System.Web.Mail命名空间下的MailMessage类通常是最简单的方式。在此解决方案中,我们假设已经添加了System.Web命名空间的引用(该命名空间在控制台应用程序中默认不会被引用),并且在测试套件中加上了using
System.Web.Mail;的语句。实例化一个MailMessage对象后,我们分别给From,To,Subject,BodyEncoding,BodyFormat,and Body属性赋值。我们也可以为Priority这个可选的属性赋值为MailPriority.High。我们将System.Text.Encoding中代表性的编码方式,通常使用ASCII,赋值为BodyEncoding属性。如果需要我们也可以使用Unicode编码或者UTF-8。BodyFormat的属性值为MailFormat.Html或MailFormat.Text.Either都可以,只要邮件系统没有特殊要求。Body属性值为一个存储了文本的信息的字符串,至少我们在该字符串中包含失败用例的ID、实际结果以及预期结果。

理论上,自动发送email很容易,但是实际上,这个过程中会出现很多问题。我们必须处理中转服务器、代理服务器、网络安全策略和防火墙,这里只是举了一些例子。因此,我们最好的策略是让我们的测试套件所在的机器尽可能的独立与其他机器运行。这样可以防止外界因素的影响,也可以使我们的测试套件发送email更可靠。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: