How to write Nunit Test

The Nunit framework identifies the test scripts by having the annotation @Test. Let's create the class FirstTest and write a method with annotation [Test] to validate the random number range.

Example program:

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

namespace Testing.Steps
{
    internal class FirstTest
    {
       
        [Test]
        public void testMethod()
        {
            Random rnd = new Random();
            int randomNum = rnd.Next(1, 500);
            if (randomNum >=50 && randomNum <=250)
            {
                Console.WriteLine("Random Number Test Pass");
            }
            else
            {
                Assert.Fail("Random Number is not in between 50 and 250 it is - " + randomNum);
            }
        }
    }
}
 

The Nunit framework identifies and executes the above test. 

 

Related Tutorials