How to implement NUnit Test in visual studio 2012
Owned by Bantyder Bahru (Unlicensed)
Download Source
Install NUnit Test Adapter
This test adapter is used as a bridge between Visual Studio and NUnit framework can be used from within Visual Studio IDE.
- Click Tools->Extensions and Updates…
- It will open Extensions and Updates dialog box , click online and Type nunit in the search box
- Click download button will download and install necessary components on your machine.
- After installation you have to restart visual studio
To implement NUnit in your Test Project
- Create your unit test project
- From menu Click PROJECT->Manage NuGet Package…
- You will presented NuGet Package Manger
- Click Online and type nunit in search dialog box
- Now click on the install button to install NUnit Package in the unit test project ,
Note: Make sure you have selected your unit test project. - Now NUnit is ready
- Reference project you want to test , in this case reference SimpleOrder.Business
- If you need some mocking install Moq in you test project.
- Everything is ready to write your test.
- Right click on your test Project and add UnitTest class
- In your test class make sure to replace the following namespace from using
using Microsoft.VisualStudio.TestTools.UnitTesting; //by using NUnit.Framework;
12. In your test class
Add class attribute [TestFixture] instead of [TestClass]
Add test method attribute [Test] instead of [TestMethod]
13. And our test method will be as shown below
Product Service NUnit Unit Test Expand source
using System; using System.Collections.Generic; using System.Linq; using NUnit.Framework; using Moq; using SimpleOrder.Business.Models; using SimpleOrder.Business.Repository; using SimpleOrder.Business.Service; using SimpleOrder.Business.UnitWork; namespace SimpleOrder.Business.NUnit.Tests.Services { [TestFixture] public class ProductServiceTest { [Test] public void Can_Add_Product_Service() { //Arrange List<Product> products = new List<Product>() { new Product(){Description="P1",ProductId=1,ProductName="p1Name"}, new Product(){Description="P2",ProductId=2,ProductName="p2Name"}, new Product(){Description="P3",ProductId=3,ProductName="p3Name"}, new Product(){Description="P4",ProductId=4,ProductName="p4Name"}, new Product(){Description="P5",ProductId=5,ProductName="p5Name"}, new Product(){Description="P6",ProductId=6,ProductName="p6Name"}, }; Mock<IGenericRepository<Product>> mock1 = new Mock<IGenericRepository<Product>>(); //Here we are going to mock repository GetAll method mock1.Setup(m => m.GetAll()).Returns(products); //Here we are going to mock repository Add method mock1.Setup(m => m.Add(It.IsAny<Product>())).Returns((Product target) => { var original = products.FirstOrDefault( q => q.ProductId == target.ProductId); if (original != null) { return false; } products.Add(target); return true; }); //Now we have our repository ready for property injection //Here we are going to mock our IUnitOfWork Mock<IUnitOfWork> mock = new Mock<IUnitOfWork>(); //Here we are going to inject our repository to the property //mock.SetupProperty(m => m.ProductRepository).SetReturnsDefault(mock1.Object); mock.Setup(m => m.ProductRepository).Returns(mock1.Object); //Now our UnitOfWork is ready to be injected to the service //Here we inject UnitOfWork to constractor of our service ProductService productService = new ProductService(mock.Object); //Act productService.AddProduct(new Product { ProductId = 7, ProductName = "P7Name", Description = "P7" }); var result = productService.GetAllProduct(); var newProduct = result.FirstOrDefault(t => t.ProductId == 7); //Assert Assert.AreEqual(products.Count, result.Count); Assert.AreEqual("P7Name", newProduct.ProductName); Assert.AreEqual("P7", newProduct.Description); } [Test] public void Can_Get_All_Product_Service() { //Arrange List<Product> products = new List<Product>() { new Product(){Description="P1",ProductId=1,ProductName="p1Name"}, new Product(){Description="P2",ProductId=2,ProductName="p2Name"}, new Product(){Description="P3",ProductId=3,ProductName="p3Name"}, new Product(){Description="P4",ProductId=4,ProductName="p4Name"}, new Product(){Description="P5",ProductId=5,ProductName="p5Name"}, new Product(){Description="P6",ProductId=6,ProductName="p6Name"}, }; Mock<IGenericRepository<Product>> mock1 = new Mock<IGenericRepository<Product>>(); //Here we are going to mock repository GetAll method mock1.Setup(m => m.GetAll()).Returns(products); //Now we have our repository ready for property injection //Here we are going to mock our IUnitOfWork Mock<IUnitOfWork> mock = new Mock<IUnitOfWork>(); //Here we are going to inject our repository to the property mock.Setup(m => m.ProductRepository).Returns(mock1.Object); //Now our UnitOfWork is ready to be injected to the service //Here we inject UnitOfWork to constractor of our service ProductService productService = new ProductService(mock.Object); //Act var result = productService.GetAllProduct(); //Assert Assert.AreEqual(products,result); } [Test] public void Can_Update_Product_Service() { //Arrange List<Product> products = new List<Product>() { new Product(){Description="P1",ProductId=1,ProductName="p1Name"}, new Product(){Description="P2",ProductId=2,ProductName="p2Name"}, new Product(){Description="P3",ProductId=3,ProductName="p3Name"}, new Product(){Description="P4",ProductId=4,ProductName="p4Name"}, new Product(){Description="P5",ProductId=5,ProductName="p5Name"}, new Product(){Description="P6",ProductId=6,ProductName="p6Name"}, }; Mock<IGenericRepository<Product>> mock1 = new Mock<IGenericRepository<Product>>(); //Here we are going to mock repository Edit method mock1.Setup(m => m.Edit(It.IsAny<Product>())).Returns((Product target) => { var original = products.FirstOrDefault( q => q.ProductId == target.ProductId); if (original == null) { return false; } original.ProductName = target.ProductName; original.Description = target.Description; return true; }); //Now we have our repository ready for property injection //Here we are going to mock our IUnitOfWork Mock<IUnitOfWork> mock = new Mock<IUnitOfWork>(); //Here we are going to inject our repository to the property mock.Setup(m => m.ProductRepository).Returns(mock1.Object); //Now our UnitOfWork is ready to be injected to the service //Here we inject UnitOfWork to constractor of our service ProductService productService = new ProductService(mock.Object); //Act var updatedProduct = new Product() {ProductId = 1, ProductName = "p1NameModified", Description = "p1Modified"}; productService.UpdateProduct(updatedProduct); var actualProduct = products.FirstOrDefault(t => t.ProductId == 1); //Assert Assert.AreEqual(actualProduct.ProductName ,updatedProduct.ProductName); Assert.AreEqual(actualProduct.Description, updatedProduct.Description); } [Test] public void Can_Delete_Product_Service() { //Arrange List<Product> products = new List<Product>() { new Product(){Description="P1",ProductId=1,ProductName="p1Name"}, new Product(){Description="P2",ProductId=2,ProductName="p2Name"}, new Product(){Description="P3",ProductId=3,ProductName="p3Name"}, new Product(){Description="P4",ProductId=4,ProductName="p4Name"}, new Product(){Description="P5",ProductId=5,ProductName="p5Name"}, new Product(){Description="P6",ProductId=6,ProductName="p6Name"}, }; Mock<IGenericRepository<Product>> mock1 = new Mock<IGenericRepository<Product>>(); //Here we are going to mock repository FindById Method mock1.Setup(m => m.FindById(It.IsAny<int>())).Returns((int i) => products.Single(x => x.ProductId == i)); //Here we are going to mock repository Delete method mock1.Setup(m => m.Delete(It.IsAny<Product>())).Returns((Product target) => { var original = products.FirstOrDefault( q => q.ProductId == target.ProductId); if (original == null) { return false; } products.Remove(target); return true; }); //Now we have our repository ready for property injection //Here we are going to mock our IUnitOfWork Mock<IUnitOfWork> mock = new Mock<IUnitOfWork>(); //Here we are going to inject our repository to the property mock.Setup(m => m.ProductRepository).Returns(mock1.Object); //Now our UnitOfWork is ready to be injected to the service //Here we inject UnitOfWork to constractor of our service ProductService productService = new ProductService(mock.Object); //Act productService.DeleteProduct(6); var result = products.FirstOrDefault(t => t.ProductId == 6); //Assert Assert.IsNull(result); Assert.IsTrue(products.Count ==5); } [Test] public void Can_Find_By_Id() { //Arrange List<Product> products = new List<Product>() { new Product(){Description="P1",ProductId=1,ProductName="p1Name"}, new Product(){Description="P2",ProductId=2,ProductName="p2Name"}, new Product(){Description="P3",ProductId=3,ProductName="p3Name"}, new Product(){Description="P4",ProductId=4,ProductName="p4Name"}, new Product(){Description="P5",ProductId=5,ProductName="p5Name"}, new Product(){Description="P6",ProductId=6,ProductName="p6Name"}, }; Mock<IGenericRepository<Product>> mock1 = new Mock<IGenericRepository<Product>>(); //Here we are going to mock repository FindById Method mock1.Setup(m => m.FindById(It.IsAny<int>())).Returns((int i) => products.Single(x => x.ProductId == i)); //Now we have our repository ready for property injection //Here we are going to mock our IUnitOfWork Mock<IUnitOfWork> mock = new Mock<IUnitOfWork>(); //Here we are going to inject our repository to the property mock.Setup(m => m.ProductRepository).Returns(mock1.Object); //Now our UnitOfWork is ready to be injected to the service //Here we inject UnitOfWork to constractor of our service ProductService productService = new ProductService(mock.Object); //Act var result = productService.GetProduct(6); var expected = products.Single(t => t.ProductId == 6); //Assert Assert.AreEqual(expected,result); } } }
14. Now you can Right click your test project and click Run Unit Tests and the result will be displayed in the test explorer.