Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I test if two dates are within a certain tolerance in NUnit?

Tags:

nunit

How do I test if two dates are within a certain tolerance in NUnit?

like image 505
Josh Kodroff Avatar asked Oct 21 '25 04:10

Josh Kodroff


2 Answers

You may want to look at the "Within" method that lives off of the Constraint object.

For example:

Assert.That(DateTime.Now, Is.EqualTo(DateTime.Now.AddMilliseconds(1000)).Within(101));

It's usually used to give a tolerance to doubles and floats, but since in the end a DateTime is a double, it might suit your needs.

like image 156
CubanX Avatar answered Oct 25 '25 19:10

CubanX


TimeSpan tolerance = new TimeSpan(0,1,0);  // e.g. 1 minute

Assert.IsTrue((firstDateTime-SecondDateTime).Duration() > tolerance);
like image 42
Howard Avatar answered Oct 25 '25 17:10

Howard