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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章