生成序列的可读单元测试?
假设我有一些返回 IEnumerable
对象的方法。此方法利用 yield return
关键字来生成无限序列。斐波那契算法示例:
public static IEnumerable<long> Fibonacci()
{
long x = 0L;
long y = 1L;
long z;
yield return x;
yield return y;
while (true)
{
z = x + y;
yield return z;
y = x;
x = z;
}
}
如何正确地为此类序列创建单元测试?我所说的“适当”也指可读。
我可以编写这样的单元测试:
[TestMethod]
public void FibonacciTest()
{
var actual = MyClass.Fibonacci();
var @enum = actual.GetEnumerator();
Assert.IsTrue(@enum.MoveNext();
Assert.AreEqual(@enum.Current), 0);
Assert.IsTrue(@enum.MoveNext();
Assert.AreEqual(@enum.Current), 1);
Assert.IsTrue(@enum.MoveNext();
Assert.AreEqual(@enum.Current), 1);
Assert.IsTrue(@enum.MoveNext();
Assert.AreEqual(@enum.Current), 2);
Assert.IsTrue(@enum.MoveNext();
Assert.AreEqual(@enum.Current), 3);
Assert.IsTrue(@enum.MoveNext();
Assert.AreEqual(@enum.Current), 5);
Assert.IsTrue(@enum.MoveNext();
}
此测试有效,但我认为它不可读。编写序列单元测试的一般准则(斐波那契算法只是一个例子)是什么?
PS:我正在使用 Visual Studio OOB 测试套件 + Pex。
Let's suppose I have some method that returns a IEnumerable<int>
object. This methods make use of yield return
keyword to produce a infinite sequence. Example of the Fibonacci algorithm :
public static IEnumerable<long> Fibonacci()
{
long x = 0L;
long y = 1L;
long z;
yield return x;
yield return y;
while (true)
{
z = x + y;
yield return z;
y = x;
x = z;
}
}
How can I properly create unit test for such sequence ? By proper I also mean readable.
I can write unit tests like this :
[TestMethod]
public void FibonacciTest()
{
var actual = MyClass.Fibonacci();
var @enum = actual.GetEnumerator();
Assert.IsTrue(@enum.MoveNext();
Assert.AreEqual(@enum.Current), 0);
Assert.IsTrue(@enum.MoveNext();
Assert.AreEqual(@enum.Current), 1);
Assert.IsTrue(@enum.MoveNext();
Assert.AreEqual(@enum.Current), 1);
Assert.IsTrue(@enum.MoveNext();
Assert.AreEqual(@enum.Current), 2);
Assert.IsTrue(@enum.MoveNext();
Assert.AreEqual(@enum.Current), 3);
Assert.IsTrue(@enum.MoveNext();
Assert.AreEqual(@enum.Current), 5);
Assert.IsTrue(@enum.MoveNext();
}
This test works, but I don't think it is readable. What are general (Fibonacci alogrithm was only a example) guidelines for writing unit tests for sequences ?
PS: I'm using Visual Studio OOB Test suite + Pex.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
怎么样:
顺便说一句,我要指出的是,你实际上并没有无限序列,因为斐波那契数列中只有这么多的成员可以容纳在 long 中 (或任何固定大小的数据类型)。您不妨测试所有 92 个。
How about something like:
By the way, I would point out that you don't really have an infinite sequence there since there are only so many members of the Fibonacci series that can fit in
long
(or any fixed-size data-type for that matter). You might as well test all 92 of them.