Home > Software engineering >  How to unit test in c#
How to unit test in c#

Time:10-31

Hello I trying to do Testing in my Sort

 public class Sort
        {
    
            public string Word { get; set; }

    public Sort(string word)
    {
        Word = word;
        
    }
            public string MergeSort()
            {
           
                    var tempLetter = "";
                    string arrangedSort = "";
                   
                List<string> ListLetters = new List<string>();
                    arrangedSort = "";
                    for (int i = 0; i < Word.Length; i  )
                    {
                        ListLetters.Add(Word.Substring(i, 1)); 
                    }
                       for (int i = 0; i < Word.Length; i  )
                                    {
                                        for (int j = i; j < Word.Length; j  )
                                        {
                                            if (char.Parse(ListLetters[i]) > char.Parse(ListLetters[j]))
                                            {
                                                tempLetter = ListLetters[i];
                                                ListLetters[i] = ListLetters[j];
                                                ListLetters[j] = tempLetter;
                                            }
                                        }
                                    }
    
                                
                    foreach (var listLetter in ListLetters)
                    {
                        arrangedSort  = listLetter;
                    }
                    return arrangedSort;
            }
    }

This is my Unit Test

 [TestClass]
public class SortTests
{
    [TestMethod]
    public void MergeSortTesting()
    {
        var sort = new Sort("dcba");

        int result = sort.MergeSort();

        Assert.AreEqual("abcd", result);
    }
}

But it didn't work may I know what is the cause why it didn't work. Sorry Beginner in c#. Kindly help me what is wrong with the code? I also tried searching the solution how to unit testing work but I can't get the solution

enter image description here

CodePudding user response:

First, lets clean up your class a little bit, remove code that you don't need. The less code, the less code there is to read and understand later. In this case, you don't need a class at all. Well, C# being an OOP language forces you to put all code in a class out of principle, but it has no state to hold, so it can be a static class. Static classes don't need to be instantiated, as you will see later. In addition, getting a character array from a string and transforming a character array to a string is not something you have to do by hand. When you remove all that stuff, your algorithm becomes much more clear and concise. I remember merge sort differently, but that is up to you to figure out I guess.

public static class SortAlgorithms
{
    public static string MergeSort(string word)
    {
       var listLetters = word.ToArray();

       for (int i = 0; i < listLetters.Length; i  )
       {
           for (int j = i; j < listLetters.Length; j  )
           {
               if (listLetters[i] > listLetters[j])
               {
                   var tempLetter = listLetters[i];
                   listLetters[i] = listLetters[j];
                   listLetters[j] = tempLetter;
               }
           }
       }

       return new string(listLetters);
    }
}

Now on to the tests: Since we renamed the test class to no longer conflict with your actual class and change the output from int to string, it will actually compile. You also don't need this detour of creating and initializing an object any more.

[TestClass]
public class SortAlgorithmTests
{
    [TestMethod]
    public void MergeSortTesting()
    {
        var input = "dcba";
        var output = SortAlgorithms.MergeSort(input);

        Assert.AreEqual("abcd", output);
    }
}

CodePudding user response:

Because you don't initialize the Word Also, The unit test class must have a different name from the main class Sort. and also don't forget [TestMethod] on each method the in unit tests class.
Try this:

public class Sort
{

    public string Word { get; set; }

    public Sort(string word)
    {
        Word = word;
    }

    public string MergeSort()
    {
   
            var tempLetter = "";
            string arrangedSort = "";
           
        List<string> ListLetters = new List<string>();
            arrangedSort = "";
            for (int i = 0; i < Word.Length; i  )
            {
                ListLetters.Add(Word.Substring(i, 1)); 
            }
               for (int i = 0; i < Word.Length; i  )
                            {
                                for (int j = i; j < Word.Length; j  )
                                {
                                    if (char.Parse(ListLetters[i]) > char.Parse(ListLetters[j]))
                                    {
                                        tempLetter = ListLetters[i];
                                        ListLetters[i] = ListLetters[j];
                                        ListLetters[j] = tempLetter;
                                    }
                                }
                            }

                        
            foreach (var listLetter in ListLetters)
            {
                arrangedSort  = listLetter;
            }
            return arrangedSort;
    }
}

Then in your unit test:

[TestClass]
public class SortTests
{
    [TestMethod]
    public void MergeSortTesting()
    {
        var sort = new Sort("dcba");

        int result = sort.MergeSort();

        Assert.AreEqual("abcd", result);
    }
}
  • Related