Unit Testing Search and Sort method(s) I just decided to write a unit test, and see how to do it.  This is my unit test:



[TestMethod]
public void TestSearchSort()
{
    System.IO.File.WriteAllText(@"C:\Users\Hosch250\Documents\Visual Studio 2013\Projects\ConsoleApplication14\ConsoleApplication14\TestFile.cs", string.Empty);

    var TestInstance = new Program();
    var titles = new List&lt;string&gt;();
    string[] query = { "main", "menu" };

    Program.Search(ref titles, ref query);

    var expectedTitles = new List&lt;string&gt;();
    string[] expectedTitleStrings = { "The Main Menu", "OneNote", "The Text Menu",
                                    "The Text Block Menu", "The Table Menu", "The Table Cells Menu",
                                    "The Draw Menu", "The Drawn Items Menu", "The Picture Menu", 
                                    "The File Menu", "Draw", "Windows Phone Notebooks",
                                    "Windows Phone Sections", "Windows Phone Pages" };
    expectedTitles.AddRange(expectedTitleStrings);

    using (var file = new System.IO.StreamWriter(@"C:\Users\Hosch250\Documents\Visual Studio 2013\Projects\ConsoleApplication14\ConsoleApplication14\TestFile.cs", true))
    {
        file.WriteLine("Expected Count: " + expectedTitles.Count);
        file.WriteLine("Actual Count: " + titles.Count);
    }

    Assert.AreEqual(expectedTitles.Count, titles.Count);
    for (var i = 0; i &lt; titles.Count; i++)
    {
        using (var file = new System.IO.StreamWriter(@"C:\Users\Hosch250\Documents\Visual Studio 2013\Projects\ConsoleApplication14\ConsoleApplication14\TestFile.cs", true))
        {
            file.WriteLine("Expected Title: " + expectedTitles[i]);
            file.WriteLine("Actual Title: " + titles[i]);
        }
        Assert.AreEqual(expectedTitles[i], titles[i]);
    }
}


This is the method it tests:

public static void Search(ref List&lt;string&gt; resultTitles, ref string[] query)
{
    List&lt;int&gt; weight = new List&lt;int&gt;();

    int position = -1;

    foreach (string[] array in SearchKeys.Keys)
    {
        position++;
        int length = array.Length;
        int middle = length / 2;
        char firstCharMidArray = array[middle][0];

        foreach (string s in query)
        {
            int min = array[middle][0] &lt; s[0] ? middle : 0;
            int max = array[middle][0] &lt;= s[0] ? array.Length : middle + 1;

            for (int i = min; i &lt; max; i++)
            {
                weight.Add(0);

                if (array[i] == s)
                {
                    if (weight[position] == 0)
                    {
                        resultTitles.Add(array[0]);
                    }
                    weight[position]++;

                }
            }
        }
    }

    StableSort(ref resultTitles, ref weight);
}


I write to the file in the test method so I can see where/how it failed.  How did I do?  Should I be doing anything different?  Should I have more tests?  I most certainly cannot run every possible combination of search terms.
I write to the file in the test method so I can see where/how it failed.


Don't do that. This:


using (var file = new System.IO.StreamWriter(@"C:\Users\Hosch250\Documents\Visual Studio 2013\Projects\ConsoleApplication14\ConsoleApplication14\TestFile.cs", true))
{
    file.WriteLine("Expected Count: " + expectedTitles.Count);
    file.WriteLine("Actual Count: " + titles.Count);
}



Has just become a possible reason for your test to fail that has nothing to do with your test. For example, if this was part of a test suite in a repo on GitHub, I couldn't run this test successfully. It's important that tests be portable and deal only with the code under test. 

If you really want to know exactly which item caused your test to fail, use the overload that takes a message string.