tdd – 使用Assert.Inconclusive
我想知道有人应该使用Assert.Inconclusive()。
我正在使用它,如果我的单元测试将要失败的原因除了它是什么。 为Sum()编写单元测试很简单。 平均线的失败并不明确,因为它失败的原因,它失败的原因除了它应该测试。 这就是为什么我会检查Sum()返回正确的结果,否则我Assert.Inconclusive()。 这被认为是好的做法吗?什么是Assert.Inconclusive?还是应该通过隔离框架来解决前面的例子?
当我使用VS来生成单元测试时,我得到了Assert.Inclusive的生成测试方法,通常我更改断言,当我工作时,其他的东西。我在测试结果中使用Assert.Inconclusive的问号作为标记来快速告诉我哪些测试还没有完成。
那么这只是我使用它的方式。从它的名字“不确定”,我想你可以用来表示你的不确定性的状态,只要你记录它的意思。 然而,从您的Average()方法的描述中,我认为您的单元测试可能不够原子,只能涵盖一个“单位”,一个具体的场景。有时候,我为单个方法编写2或3单元测试方法。或者您可以将Average()方法打破涵盖单一职责的较小方法。这样,您可以单元测试这些较小的方法,然后单元测试您的Average()1。 约翰尼斯 这是我如何实现Sum()和Average()方法。 public static class MyMath { private static void ValidateInput(ICollection<int> numbers) { if (numbers == null) throw new ArgumentNullException("numbers","Null input. Nothing to compute!"); if (numbers.Count == 0) throw new ArgumentException("Input is empty. Nothing to compute!"); } public static int Sum(int[] numbers) { ValidateInput(numbers); var total = 0; foreach (var number in numbers) total += number; return total; } public static double Average(int[] numbers) { ValidateInput(numbers); return Sum(numbers) / numbers.Length; } } 为了简单起见,我只是从ValidateInput(ICollection< int>)方法中抛出ArgumentException异常。您还可以检查在ValidateInput(ICollection< int>)方法中溢出的可能性并抛出OverflowException。 这就是说,我将如何测试Average(int [])函数。 [TestMethod] public void AverageTest_GoodInput() { int[] numbers = {1,2,3}; const double expected = 2.0; var actual = MyMath.Average(numbers); Assert.AreEqual(expected,actual); } [TestMethod] [ExpectedException(typeof(ArgumentNullException))] public void AverageTest_NullInput() { int[] numbers = null; MyMath.Average(numbers); } [TestMethod] [ExpectedException(typeof(ArgumentException))] public void AverageTest_EmptyInput() { var numbers = new int[0]; MyMath.Average(numbers); } 通过这些测试设置,我可以确定,当所有测试通过时,我的功能是正确的。那么,除了溢出的情况。现在我可以返回到ValidateInput(ICollection< int>)方法来添加逻辑来检查溢出,然后再添加一个测试,期望OverflowException被抛出,导致溢出的那种输入。或者如果您喜欢使用TDD来进行相反的操作。 我希望这有助于澄清这个想法。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |