HiveBrain v1.2.0
Get Started
← Back to all entries
snippetcsharpMinor

Unit Testing Search and Sort method(s)

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
searchmethodtestingandsortunit

Problem

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();
string[] query = { "main", "menu" };

Program.Search(ref titles, ref query);

var expectedTitles = new List();
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

This is the method it tests:

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

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]

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 ev

Solution

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.

Code Snippets

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);
}

Context

StackExchange Code Review Q#79638, answer score: 9

Revisions (0)

No revisions yet.