net自動化測試之道API測試-讀取測試用例

Reading Test Case Data

讀取測試用例數據

 

Problem

You want to read each test case in a testcase file stored as a simple text file.

 問題

如何從簡單文本文件中讀取每個測試用例呢?

 

Design

Iterate through each line of the test casefile using a while loop with a System.IO.StreamReader

object.

設計

使用while循環語句循環每一行,用System.IO.StreamReader對象讀取。

 解決方案

Solution

FileStream fs=newFileStream("..\\..\\TestCases.txt",FileMode.Open);

StreamReader sr=new StreamReader(fs);

string line;

while((line=sr.ReadLine())!=null)

{

//parse each test case line

//call method under test

//determine pass or fail

//log test case result

}

sr.Close();

fs.Close();

註解

In general,console applications,rather thanWindows-based applications,are best suited for

lightweight test automationharnesses.Console applications easily integrate into legacy test

systems and can be easily manipulated in aWindows environment.If you do design a harness

as a Windows application,make sure that itcan be fully manipulated from the command line.

通常,輕量級自動測試套件使用控制檯應用程序比使用windows應用程序實現更合適。因爲控制檯應用程序更容易與過去的測試系統整合,在windows的環境中更容易操作。如果你設計了一個windows 應用程序的測試套件,請確保可以完全從控制檯操作。

This solution assumes you have placed a usingSystem.IO;statement in your harness so

you can access the FileStream andStreamReader classes without having to fully qualify them.

We also assume that the test case data fileis named TestCases.txt and is located two directo-

ries above the test harnessexecutable.Relative paths to test case data files are generally better

than absolute paths like C:\\Here\\There\\TestCases.txt because relative pathsallow you to

move the test harness root directory andsubdirectories as a whole without breaking the har-

ness paths.However,relative paths may breakyour harness if the directory structure of your

test system changes.A good alternative isto parameterize the path and name of the test case

data file:

在這個解決方案中我們假設在套件中放置了using System.IO;語句,這樣我們不用全部引用,就可以使用FileStream和StreamReader類了。我還假設存儲測試用例的文件名爲TestCases.txt,並且放在測試套件執行程序的上兩級目錄下。相對路徑通常要比絕對路徑(如C:\\Here\\There\\TestCases.txt)靈活,因爲如果我們要將測試套件的根目錄以及子目錄作爲整體移動,相對路徑就不需要改變路徑。但是,如果測試系統的目錄結構變了,在相對路徑下,測試套件可能會失效。一種好的方式是參數化測試用例文件的路徑和名稱:

static void Main(string[]args)

{

string testCaseFile=args[0];

FileStream fs=new FileStream(testCaseFile,FileMode.Open);

//etc.

}

Then you can call the harness along thelines of

然後,我們可以像下面這樣調用套件:

C:\Harness\bin\Debug>Run.exe..\..\TestCases.txt

In this solution,FileStream andStreamReader objects are used.Alternatively,you can use

static methods in the System.IO.File classsuch as File.Open().If you expect that two or more

test harnesses may be accessing the testcase data file simultaneously,you can use the over-

loaded FileStream constructor that includesa FileShare parameter to specify how the file will

be shared.

在這個解決方案中,我們使用FileStream和StreamReader對象。也可以使用System.IO中的靜態方法,如File.Open()。如果你想讓兩個或者更多的測試套件同時訪問測試用例文件,你可以使用包含FileShare參數的重載的FileStream構造器,FileShare參數用來說明文件將如何被共享。

發佈了10 篇原創文章 · 獲贊 0 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章