Skip to main content
 首页 » 编程设计

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

2024年02月23日16mengfanrong

我有一种形式: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 来获取数组的大小,不需要传递大小。


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