jeudi 28 avril 2016

Unit Test Finishes before Thread.Sleep time

My program is a simple timer (it's a Pomodoro Timer). I'm trying to incorporate unit testing into my personal projects.

I run my tests on each timer class' run() method and assert that the endTime - startTime is within 1 second of how long I set the timer.

MyTimer.run() invokes Thread.sleep(timerMilliseconds).

My unit tests pass in less time than each timer is set for.

How is this possible?

Eg. Rest is set to 5 seconds, unit test passes in < 1ms

[TestMethod]
        public void testRun () {
            TimeSpan fiveSeconds = new TimeSpan(5000);
            Rest rest = new Rest(fiveSeconds, null);
            TimeSpan now = DateTime.Now.TimeOfDay;
            try {
                rest.run(); //Thread.Sleep(base.getMinutes().Milliseconds);
            }
            catch (Exception e) {
                Assert.Fail(e.ToString());
            }

            Assert.AreEqual(now.Add(fiveSeconds).TotalMilliseconds, DateTime.Now.TimeOfDay.TotalMilliseconds, 1000 , "Actual sleep time not expected");
        }

Aucun commentaire:

Enregistrer un commentaire