First open the microsoft visual studio and select the New project and select the Class library. Change the name of the Class name to your respected name.Click OK.
before writing a code always use a namespace which is a good way of writing a code.
The below code is written in the C# language
The First Class library :
using System;
namespace MyApp
{
public class MyMath
{
public int Add(int i, int j)
{
return i + j;
}
}
}
Compile the code and make sure where you have saved the files.
Now create an another class library with the same above process we have done.The code inside the class library should contain as below code. Before you proceed you need to add the previous class library DLL as a reference and Add the Nunit Test DLL as a reference as you have done for Class library.
The Unit Test Reference is mandatory and the Previous class DLL(first class library you have created, whose methods you want to use), After adding the refereces add the code as below.
Second Class Library which contains Nunit reference
using System;
using NUnit.Framework;
using MyAppTest;
using MyApp;
namespace MyAppTest
{
[TestFixture]
public class Class1
{
[Test]
public void MyAddTest()
{
MyMath m = new MyMath();
Assert.AreSame(m.Add(2, 3), 8,"Both are wrong");
}
[Test]
public void MyAddTest2()
{
MyMath m = new MyMath();
Assert.AreSame(m.Add(2, 3), 5, "Both are correct");
}
[Test]
public void MyAddTest3()
{
MyMath m = new MyMath();
Assert.AreSame(m.Add(2, 3), 6, "Both are wrong");
}
}
}
After you are compiling this code. open the Nunit Editor. Click on New project and add the Compiled DLL for that and click ok. After this Click on RUN, You will have a window which will show the Expected Results and the Actual Results.
Assertions are the way to test for fail-pass test. NUnit framework support following assertions:
Assert()
AssertEquals()
AssertNotNull()
AssertNotNull()
AssertNull()
AssertSame()
Fail()
Below is the attached image: