Visual Studio: How to create NUnit Test

By FoxLearn 6/25/2017 8:10:29 PM   3.16K
How to create NUnit Test in C#

To play demo, you need to create a MyMath class as below

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NUnitTestDemo
{
    public class MyMath
    {
        public int Add(int a,int b)
        {
            return a + b*2;
        }

        public int Sub(int a,int b)
        {
            return a - b;
        }
    }
}

To work with extensions and updates, click on the Tool->Extension and Updates and then select NUnit Test Adapter. A test class can be created as shown in the listing below:

using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NUnitTestDemo
{
    [TestFixture]
    class MyTestCase
    {
        [TestCase]
        public void Add()
        {
            MyMath math = new MyMath();
            Assert.AreEqual(31, math.Add(20, 11));
        }

        [TestCase]
        public void Sub()
        {
            MyMath math = new MyMath();
            Assert.AreEqual(10, math.Sub(20, 10));
        }
    }
}

VIDEO TUTORIALS

 

Related