net自動化測試之道API測試-確定測試套件運行的總時間

Determining a Test Run Total Elapsed Time

確定測試套件運行的總時間

Problem

You want to determine the total elapsed runtime for a test run.

問題

如何確定測試套件運行花費的時間呢?

Design

Use the DateTime.Now property to record thetime when the test run started and when the test

run ended.Then use a TimeSpan object tocalculate the elapsed time for the test run.

設計

使用DateTime.Now屬性記錄測試套件運行開始的時刻和測試套件運行結束的時刻。然後使用TimeSpan對象計算測試套件運行花費的時間。

 解決方案

DateTime startTime=DateTime.Now;

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

{

//run tests

}

DateTime endTime=DateTime.Now;

TimeSpan elapsedTime=endTime-startTime;

Console.WriteLine("Elapsedtime="+elapsedTime.ToString());

 

Comments

Calling the DateTime.Now property retrievesthe current system time on the test harness

machine.You fetch a start time before yourtests execute and an end time after the test run

concludes.To determine the elapsed time,youfind the difference between the start and end

times.DateTime objects support anoverloaded subtraction operator(“–”)that returns a

TimeSpan object.You can think of a DateTimevalue as being an instant in time and a TimeSpan

value as being a time-duration. You have tobe somewhat careful about exactly where you place the statements that find

the test run start and end times.Theguiding principle is to place them as much as possible so

that you capture time spent executing yourtests,but not so much that you are capturing test

harness overhead activities that can vary.

The purpose of recording andstoring/displaying the total elapsed time of your daily test

run is so that you can detect anysignificant change in the performance characteristics of your

API methods.If the total elapsed time of atest run increases greatly one day,then you need to

investigate.If you discover that a codechange in one of the methods under test produced the

performance degradation,you’ll find outimmediately and can decide to recast the code or

accept the performance penalty.If a codechange was not the cause of the performance hit,

then you may have a problem with your testharness system(for example some rogue process

running and using up CPU time.)Anothercause of a change in test run elapsed time would be

increasing(or decreasing)the number oftests in the test case data file.

One of the advantages of test automation isthat you can execute many thousands of test

cases quickly.When you are dealing with ahuge number of test case results,you may want to

log only summary metrics(the number ofcases that passed and the number that failed)and

details only about failed test cases.In asituation like this,determining and logging the test

run elapsed time is important because itcan uncover test harness problems that can be hid-

den when you don’t have detailed testresults to examine.

 

註解

調用DateTime.Now屬性將獲得測試套件執行所在機器的當前系統時間。在測試套件執行前,我們獲得開始時間,測試套件執行結束後,我獲得結束時間。要確定花費的時間,我們要得到開始時間和結束時間之間的差。DateTime對象支持一個重載的減法操作,返回TimeSpan對象。我們可以將DateTime類型的值看作瞬時時間,而TimeSpan類型的值爲一個時間區間。我們必須注意要在何處記錄開始時間和結束時間語句。指導原則是:….(這個地方我還沒理解它到底是啥意思,但是大概說的是要準確統計測試套件運行時間,一定要知道統計的對象是“運行時間”)

記錄並存儲/顯示每天測試運行的時間的目的是便於我們追蹤API方法的性能特性的任何有意義的變化。如果某天測試套件運行總時間有大幅度的增加,則我們需要分析一下原因。如果我們發現改變某個被測方法的代碼導致性能下降,我們將會很快發現,並且可以決定重新修改代碼,還是接受當前性能帶來的不良影響。如果性能下降的不是因爲代碼改變,那麼可能是測試套件系統的問題(如流氓進程運行並且佔用了全部的CPU)。測試套件運行時間增加或減少的另外一個原因是測試用例文件中測試的數量。

自動化測試的一個優點是可以很快執行大量的測試用例。當我們處理大量的測試結果時,我們可能只想記錄摘要性度量值(如測試用例通過的數目和失敗的數目),並且只要詳細記錄失敗的測試用例。在這樣的情況下,確定並且記錄測試花費的時間很重要,因爲當我們沒有詳細的測試結果供分析的時候,它可以暴露隱藏的測試套件的問題。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章