Introduction
Every successful software development follows a lifecycle known as SDLC (Software Development Life Cycle).As per Wikipedia:
"SDLC is a process followed for a software project, within a software organization. It consists of a detailed plan describing how to develop, maintain, replace and alter or enhance specific software. The life cycle defines a methodology for improving the quality of software and the overall development process."
As we are a developer we normally understand the requirements and write the code. Apart from those we also spend some time software testing.
Software testing is a process of executing a program or application with the intent of finding the software bugs.
It can also be described as the process of validating and verifying a software program or application or product.
I have Googled and tried to find out what kind of testing a developer can do. I got lot of solutions from the internet, and the best one I found is
here. So mainly a developer must have knowledge of Unit Testing.
What is unit Testing?
Let's try to find the exact definition of Unit.
UNIT
In this IT world a unit refers to simply a smallest piece of code which takes an input ,does certain operation, and gives an output.
Out of all these Nunit is the most-used testing Framework.
What Is NUnit?
NUnit is a unit-testing framework for all .Net languages.
NUnit is Open Source software and NUnit 3.0 is released under the MIT license. This framework is very easy to work with and has user friendly attributes for working.
You can check the details of Nunit from
here.
If you want to learn what are the main differences between MsTest and Nunit I recommend you check the following
Link.
You can also check several other links that are available on the internet. I did a small inspection on internet and found the following result in using different Testing frameworks. As per the Slant website, this is what most people recommend on using different unit Testing frameworks.
To work with Nunit let's set up a simple console application as follows.
Write the following class in this project.
- namespace DemoProject {
- public class EmployeeDetails {
- public int id {
- get;
- set;
- }
- public string Name {
- get;
- set;
- }
- public double salary {
- get;
- set;
- }
- public string Geneder {
- get;
- set;
- }
- }
- }
Now in the class program write some conditions which need to be tested.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace DemoProject {
- public class Program {
- public string Login(string UserId, string Password) {
- if (string.IsNullOrEmpty(UserId) || string.IsNullOrEmpty(Password)) {
- return "Userid or password could not be Empty.";
- } else {
- if (UserId == "Admin" && Password == "Admin") {
- return "Welcome Admin.";
- }
- return "Incorrect UserId or Password.";
- }
- }
- public List < EmployeeDetails > AllUsers() {
- List < EmployeeDetails > li = new List < EmployeeDetails > ();
- li.Add(new EmployeeDetails {
- id = 100, Name = "Bharat", Geneder = "male", salary = 40000
- });
- li.Add(new EmployeeDetails {
- id = 101, Name = "sunita", Geneder = "Female", salary = 50000
- });
- li.Add(new EmployeeDetails {
- id = 103, Name = "Karan", Geneder = "male", salary = 40000
- });
- li.Add(new EmployeeDetails {
- id = 104, Name = "Jeetu", Geneder = "male", salary = 23000
- });
- li.Add(new EmployeeDetails {
- id = 105, Name = "Manasi", Geneder = "Female", salary = 80000
- });
- li.Add(new EmployeeDetails {
- id = 106, Name = "Ranjit", Geneder = "male", salary = 670000
- });
- return li;
- }
- public List < EmployeeDetails > getDetails(int id) {
- List < EmployeeDetails > li1 = new List < EmployeeDetails > ();
- Program p = new Program();
- var li = p.getUserDetails();
- foreach(var x in li) {
- if (x.id == id) {
- li1.Add(x);
- }
- }
- return li1;
- }
- static void Main(string[] args) {}
- }
- }
So here I have written 3 methods or we can say some requirements which we want to test by using Unit Test. So please add a new project (Class Library Type) in the Solution Explorer as follows.
The Nunit framework does not require any specific project type, but most of the time people will add a class library to separate their code from their unit tests. You need to reference the nunit.framework.dll yourself. But if you use nuget this will be done for you automatically while installing NUnit.
So for using Nunit we need to go for Nuget Package Manager and download it.
Now after installing the Nunit we need one more dll that needs to be installed in the project.
NUnit Test Adapter for Visual Studio
The NUnit Test Adapter allows you to run NUnit tests inside Visual Studio.
Note
If you do not add this component in your project you will not be able to find your tests in test Explorer.
Note
You need to install both the Libraries in the project where you are writing the test methods only.
Before writing any test case let's be aware of all the components we are using in Nunit. If you check the Nunit site you will get all the attributes and other component details there.
https://www.nunit.org/
Now our next job is to add the Demo Project dll in DemoProjectTest References.