全部是在標題,我不知道它是否是一個好的做法或不:在單元測試中,如果運行速度太慢時拋出異常,那麼衡量方法的執行時間是否是一種很好的做法?
[TestMethod]
public void Compute_Array_In_Less_1_Second()
{
Stopwatch watcher = new Stopwatch();
int[] A = Enumerable.Range(0, 50000).Select(i => GetRandomNumber(MIN_INT, MAX_INT)).ToArray();
watcher.Start();
int[] R = Program.MethodThatReturnsAnArray(A);
watcher.Stop();
if (watcher.ElapsedMilliseconds > 1000)
Assert.Fail("The method runs in more 1 second");
}
謝謝
單元測試應測試代碼的功能是否正常工作。您通常不會使用它們來測試性能測試下的性能:-) – Magrangs 2012-03-12 16:22:32