MS-Test in C# .net

Muhammad Irfan 1 Reputation point
2021-02-24T12:19:48.207+00:00

Hi Sir,
Hope you are doing well!
I need some help in MS-Unit testing in C# .net program, I watched all of your youtube videos but....
So,
Below is my question, Please help!
Write Automated MS-Unit Test in C#.NET for the following C#.NET program. The program converts a decimal value into binary? (10 Marks)

using System;
namespace BinaryClass
{
class Program
{
static void Main(string[] args)
{
Program p = new Program();
Console.WriteLine("Binary Value of 4 = "+p.IntToBinaryString(4));
}
public string IntToBinaryString(int number)
{
const int mask = 1;
var binary = string.Empty;
while (number > 0)
{
binary = (number & mask) + binary;
number = number >> 1;
}
return binary;
}
}
}

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,279 questions
Visual Studio Testing
Visual Studio Testing
Visual Studio: A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.Testing: The act or process of applying tests as a means of analysis or diagnosis.
329 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Ken Tucker 5,846 Reputation points
    2021-02-24T13:16:49.727+00:00

    I would put the function you want to test in another class

    using System;
    
    namespace BinaryClass
    {
         class Program
         {
              static void Main(string[] args)
              {
                   Console.WriteLine("Hello World!");
                  BinaryConverter p = new BinaryConverter();
                  Console.WriteLine("Binary Value of 4 = " + p.IntToBinaryString(4));
             }
    
    
       }
    
       public class BinaryConverter
       {
            public string IntToBinaryString(int number)
           {
                  const int mask = 1;
                  var binary = string.Empty;
                  while (number > 0)
                  {
                      binary = (number & mask) + binary;
                      number = number >> 1;
                  }
                 return binary;
             }
         }
    }
    

    Then you can add a unit test project to the solution and add a reference to the binaryclass project

    You can test what the function you expect the function to return against what is actually returned. You run the tests in the unit test window

    public class UnitTest1
    {
        [TestMethod]
        public void TestBinary4()
        {
            BinaryClass.BinaryConverter p = new BinaryClass.BinaryConverter();
            string expected = "100";
            string actual = p.IntToBinaryString(4);
    
            Assert.AreEqual(expected,actual);
        }
    } 
    

  2. Michael Taylor 48,581 Reputation points
    2021-02-24T15:02:52.247+00:00

    This looks like a homework assignment or test question. Asking for others to answer the question for you is cheating.