net自动化测试之道API测试-用时间戳区别测试结果

Time-Stamping Test Case Results

用时间戳区别测试结果

Problem

You want to time-stamp your test caseresults so you can distinguish the results of different

test runs.

问题

如果我们想区别测试套件的每次运行结果,若用时间戳区别该如何实现呢?

Design

Use the DateTime.Nowproperty passed as an argument to the static CreateDirectory()method

to create a time-stampedfolder.Alternatively,you can pass DateTime.Now to theFileStream()

constructor to create a time-stamped filename.

设计

使用CreateDirectory()静态方法,传递DateTime.Now属性给该方法创建一个时间戳文件夹。当然,我也可以给FileStream()传递DateTime.Now参数创建一个时间戳文件名。

解决方案

stringfolder="Results"+DateTime.Now.ToString("s");

folder=folder.Replace(":","-");

Directory.CreateDirectory("..\\..\\"+folder);

stringpath="..\\..\\"+folder+"\\TestResults.txt"

FileStream ofs=newFileStream(path,FileMode.Create);

StreamWriter sw=new StreamWriter(ofs);

 

Comments

You create a folder name using the DateTime.Nowproperty,which grabs the current system date

and time.Passing an“s”argument to the ToString()method returns a date-time string in a

sortable pattern like“2006-07-30T13:57:00”.Youcan use many other formatting arguments

with ToString(),but a sortable pattern willhelp you manage test results better than a non-

sortable pattern.You must replace the coloncharacter with some other character(here we use

a hyphen)because colons are not valid in apath or file name.

Next,you create the time-stamped folderusing the static CreateDirectory()method,and

then you can pass the entire path and filename to the FileStream constructor.After instanti-

ating a StreamWriter object using theFileStream object,you can use the StreamWriter object

to write into a file named TestResults.txt,whichis located inside the time-stamped folder.

A slight variation on this idea is to writeall results to the same folder but time-stamp their

file names:

stringstamp=DateTime.Now.ToString("s");

stamp=stamp.Replace(":","-");

stringpath="..\\..\\TestResults-"+stamp+".txt";

FileStream ofs=newFileStream(path,FileMode.Create);

StreamWriter sw=new StreamWriter(ofs);

This variation assumes that an arbitraryresult directory is located two directories above the

test harness executable directory.If thedirectory does not exist,an exception is thrown.The test

case result file name becomes thetime-stamp value appended to the string TestResults-with a

.txt extension added,for example,TestResults-2006-12-25T23-59-59.txt.

 

注解

我们创建一个文件夹名,使用DateTime.Now属性获取系统的当前日期和时间。给ToString()方法传递参数”s”将返回一个有序形式的日期时间字符串,如“2006-07-30T13:57:00”。当然我们也可以使用其他格式的参数,但是这种有序形式的相比无序形式另利于我们管理测试结果。我们必须用其他字符(在这个例子中我们用连接符)替换冒号,因为在路径或文件名中冒号是无效的。

接下来,我们使用CreateDirectory()静态方法创建一个时间戳文件夹,然后将路径和文件名传给FileStream构成函数。实例化一个StreamWriter对象,将FileStream对象传给它,这样就将结果写到名为时间戳文件夹中的TestResults.txt中。

将结果写在同一个文件夹中的时间戳文件中略有区别:

stringstamp=DateTime.Now.ToString("s");

stamp=stamp.Replace(":","-");

string path="..\\..\\TestResults-"+stamp+".txt";

FileStream ofs=newFileStream(path,FileMode.Create);

StreamWriter sw=new StreamWriter(ofs);

我们假设任意的结果目录在测试套件可执行目录的上两级。如果目录已经存在,则会抛出异常。测试结果文件名是这样组成的,TestResults-"+stamp+".txt",如TestResults-2006-12-25T23-59-59.txt

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值