NUnit DateTime assertion problem on jenkins



我使用NHibernate和 Spring.net 模板来创建DAO。 我编写了一些用于保存和检索实体的测试。问题在于我们本地机器上的所有测试通过,但在 Jenkins 上,DateTime 变量存在奇怪的问题:

消息:

Expected: 2011-06-16 15:19:23.765
But was:  2011-06-16 15:19:23.765

任何线索可能是什么原因?

最干净的解决方案是:

Assert.That(actual, Is.EqualTo(expected).Within(tolerance).Milliseconds);

在 Within(容差)之后 您可以指定从毫秒到天的任何内容。

如果您使用的是 http://nuget.org/List/Packages/NUnit.Snippets 那么它只是

atiewms tab tab

我怀疑这两个日期在亚毫秒范围内略有不同,可能是由于存储不支持日期/时间值到"刻度"精度。

如果你真的很幸运,NUnit 可能会为DateTime提供"在一定容忍范围内相等",就像它为double提供一样。如果没有,这样的事情就可以了:

Assert.IsTrue(Math.Abs(oneDate.TotalSeconds - anotherDate.TotalSeconds) < 0.001)
要么

,要么可能将两个DateTime值舍入或截断到适当的毫秒,然后使用 AreEqual 。这肯定会给出更有用的失败消息。

尝试

Assert.Equals(oneDate.ToString("s"), anotherDate.ToString("s"));

其中"s"表示

yyyy-MM-ddTHH:mm:ss

(ISO 8601)。

最新更新