Creating NUnit Test Project

  1. Create a class library.
  2. Manage Nuget package and install:

    • NUnit
    • NUnit Test Adapter for vs2012, vs2013 and vs2015

      Manage Nuget package

  3. The reference should be added to class library,

    class library

  4. Create a console project in the same solution and add the reference of the project to the class library.

  5. In the unit test project add the below code to test the console project.
    1. [TestFixture]  
    2. public class UnitTest1 {  
    3.     DecimalToBinary obj = null;  
    4.     Fibonacci fObj = null;  
    5.     FizzBizz fbObj = null;  
    6.     public UnitTest1()  
    7.         {  
    8.             obj = new DecimalToBinary();  
    9.             fObj = new Fibonacci();  
    10.             fbObj = new FizzBizz();  
    11.         }  
    12.         [Test]  
    13.     public void FizzBizzPass()   
    14.         {  
    15.             var ExpectedResult = "Fizz";  
    16.             var originalResult = string.Empty;  
    17.             originalResult = fbObj.checkFizzBuzz(6);  
    18.             Assert.AreEqual(ExpectedResult, originalResult);  
    19.   
    20.         }  
    21.         [Test]  
    22.     public void FizzBizzFail()   
    23.         {  
    24.             var ExpectedResult = "FizzBizz";  
    25.             var originalResult = string.Empty;  
    26.             originalResult = fbObj.checkFizzBuzz(6);  
    27.             Assert.AreEqual(ExpectedResult, originalResult);  
    28.   
    29.         }  
    30.         [Test]  
    31.     public void DecimalToBinaryPass()   
    32.         {  
    33.             var ExpectedResult = "1000";  
    34.             var originalResult = string.Empty;  
    35.             originalResult = obj.getDecimalNumber(8);  
    36.             Assert.AreEqual(ExpectedResult, originalResult);  
    37.   
    38.         }  
    39.         [Test]  
    40.     public void DecimalToBinaryFail()   
    41.         {  
    42.             var ExpectedResult = "1001";  
    43.             var originalResult = string.Empty;  
    44.             originalResult = obj.getDecimalNumber(8);  
    45.             Assert.That(ExpectedResult, Is.EqualTo(originalResult));  
    46.   
    47.         }  
    48.         [Test]  
    49.     public void FibonacciPass()   
    50.         {  
    51.             List < int > ExpectedResult = new List < int > ();  
    52.             var result = fObj.GetFibonacci(1);  
    53.             ExpectedResult.Add(0);  
    54.             ExpectedResult.Add(1);  
    55.             ExpectedResult.Add(1);  
    56.             CollectionAssert.AreEqual(ExpectedResult, result);  
    57.         }  
    58.         [Test]  
    59.     public void FibonacciFail()  
    60.         {  
    61.         List < int > ExpectedResult = new List < int > ();  
    62.         var result = fObj.GetFibonacci(1);  
    63.         ExpectedResult.Add(0);  
    64.         ExpectedResult.Add(1);  
    65.         ExpectedResult.Add(1);  
    66.         ExpectedResult.Add(2);  
    67.         CollectionAssert.AreEqual(ExpectedResult, result);  
    68.     }  
    69. }  
  6. To test the methods,

    methods

    test explorer

  7. Other ways to test
  8. Install NUit Download.

    NUit

    Run Nunit and open the test project dll.

    project dll

    Click on run to know the output.

    output


Similar Articles