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)。测试套件运行时间增加或减少的另外一个原因是测试用例文件中测试的数量。

自动化测试的一个优点是可以很快执行大量的测试用例。当我们处理大量的测试结果时,我们可能只想记录摘要性度量值(如测试用例通过的数目和失败的数目),并且只要详细记录失败的测试用例。在这样的情况下,确定并且记录测试花费的时间很重要,因为当我们没有详细的测试结果供分析的时候,它可以暴露隐藏的测试套件的问题。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章