c# - 在 Visual Studio 2010 中进行单元测试 (c#)。我做错了什么?测试总是失败

标签 c# unit-testing

我有一种形式:FormPrincipal.cs

在这种形式中,有一个名为 InsertionSort() 的公共(public)方法采用 2 个参数:int[](代表整数列表)和 int 代表大小(列表的元素数)。 此函数使用“插入排序”算法简单地对列表进行排序

该方法不返回任何内容(因为在我的应用程序的逻辑中,列表只是有序的,因为数组是一个点,所以原始列表只是被修改为正确的顺序)。

我正在尝试测试函数是否对列表进行排序。

我在 Visual Studio 中创建了一个单元测试(不是作为一个单独的项目),但它总是说“失败”而不显示任何消息。我做错了什么? 代码如下:

 [TestMethod()]
    public void InsertionSortTest()
    {
        FormPrincipal target = new FormPrincipal(<some parameters>);
        target.loadData(); // function which load the list to be ordered
        int[] list1 = new int[10];
        list1 = {1,4,3,5,2,6,7,9,8,0);
        target.InsertionSort(list1,10);
        bool listaOrderedOrNot = isListOrdered(list1, 10); // isListOrder is just a function in the same file of the test where I loop the array checking if elements are growing.
        Assert.Inconclusive("A method that does not return a value cannot be verified.");
        // I tried to do the following assert command..
        Assert.AreEqual(listaOrderedOrNot, true,"ordered");
        Assert.IsTrue(listaOrderedOrNot, "ordered");
        Assert.IsFalse(listaOrdenadaOrNot, "NOT ordered");
    }

我想这可能取决于 InsertionSort() 不返回任何东西但 Visual Studio 不给出错误并且不打印消息(例如“已订购”或“未订购”)的事实。

当我运行时,测试失败了

谢谢你的帮助

最佳答案

移除Assert.Inconclusive("一个没有返回值的方法无法被验证。");
这基本上告诉 VS 你没有实现你的测试。


当你有一个数组时,你可以简单地使用 array.Length 来获取数组的大小,不需要传递大小。


您的排序算法不属于表单类。

关于c# - 在 Visual Studio 2010 中进行单元测试 (c#)。我做错了什么?测试总是失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9501738/

相关文章:

c# - VS2010 混淆了 System.Xml.XmlElement 和 System.Xml.Linq.XElement?

c# - Oracle.DataAccess DataRow.Field<十进制> InvalidCastException

unit-testing - 是否有与 MSTest 的 [ClassCleanup] 和 [ClassInitialize] 等效的非静态等效项?

c# - 多重绑定(bind)错误

c# - IIS 0x80070020 错误

c# - 在运行时传递 Type 参数

angular - 测试包含自定义表单控件的 Angular 组件?

java - 关于现场可访问性的单元测试实践

c# - 实例化其他类的单元测试类

c# - FakeItEasy:重置假电话历史/忽略电话