REST API Unit Testing

Neova Solutions
5 min readMay 11, 2020

Why do we need Unit testing?

● The developer knows where our code “ breaks “ as compared to the tester, so while developing if we implement Unit testing then we can improve code quality, so this will help in reducing bug-fixing, cost and time.

● If we ignore Unit testing it leads to higher defect fixing cost and time during System Integration and Beta testing after the application is completed. Proper unit testing done at initial stage at developer’s end saves both time and money.

What is Unit testing?

Testing an individual unit, such as a method (function) in a class, from other modules of your application. A Unit test doesn’t test the whole module; it tests the smallest unit that makes up a more significant module

Advantages:

  • With Unit testing, the speed of development will be faster.
  • Traditional testing involves setting breakpoints, debugging the code and providing inputs through GUI(Graphical User Interface). But Unit testing will simplify testing without GUI.
  • It takes less time to find and fix the bugs during Unit testing.
  • Reusable codes also reduce the effort and save time as the code needs to be modular in Unit testing and also bugs are fixed in the initial stage.

Disadvantage:

Different frameworks available in market for Unit testing:

1. MSTest — It is an in-built testing framework provided by Microsoft.

2. NUNIT — It is an open-source testing framework that is entirely written in c#.

3. XUNIT — This framework supports programming language i.e JUnit for java and .net.

Let’s do a quick comparison to evaluate better framework:

NUnit VS MSTest:

  • Nunit contains an [TestCase] attribute that allows implementing parameterized tests. This feature is not available in MSTest
  • MsTest’s ExpectedException attribute is a bug where the expected message is never really asserted even if it’s wrong — the test will pass.
  • NUnit contains an articulate version of Assert API (as already mentioned — Assert.That..)
  • Nunit is much faster
  • NUnit can run tests in both versions like 32 and 64 bit (MSTest only runs them in 32 bit IIRC)
  • NUnit allows an abstract set of classes to be test fixtures (so you can inherit test fixtures). MsTest does not contain test fixtures.
  • NUnit contains PNunit (running parallel tests with NUnit). MsTest only adds this ability in visual studio 2010 IDE

How to integrate NUnit Framework?

NUnit — provided by Microsoft.NET open-source Unit testing Framework. It provides a rich set of assertions as static methods of the Assert class. NUnit Assert class is used to determine whether a particular test method gives expected results or not. In a test method, we write code to check the business object behavior.

NUnit Features -

  • Run test suites parallelly.
  • Strong support for data-driven tests.
  • Supports multiple platforms including .NET CORE, Compact Framework, and Silverlight.
  • Every test case can be added to more than one category, to allow for selective running.
  • These Tests can be run from a console runner, within Visual Studio through a Test Adapter, or 3rd party runners.

Attribute difference Test class vs Test fixture

Using the “ Test fixture” attribute we can provide parameters for class but using “ Test class “ will allow parameters for only Test attributes method.

E.G — [TestFixture(“hello”, “hello”, “goodbye”)]

REST API:-

A RESTful API is used to send HTTP requests like GET, PUT, POST, and DELETE data through existing protocols.

Working Flow of REST Request:-

After sending a Rest request from the user it will go to API=> App layer=> Controller=> Repository=> Database Services, performs execution and sends a response back to the server.

How to Integrate REST API with Unit testing using NUnit Framework

Step-1: Download and install Nunit3-console or follow the links given below:-

https://github.com/nunit/nunit-console/releases/download/v3.10/NUnit.Console-3.10.0.zip

Step-2: Open the respective Unit testing project in Visual Studio Or Create a new Unit testing project as:

Step-3: Open NuGet Package Manager Console for that project and type following commands in the given order:

  • Install-Package NUnit -Version 3.12.0
  • Install-Package RestSharp -Version 103.1
  • Install-Package NUnit3TestAdapter -Version 3.13.0

Perform this for each Unit testing project.

Step-4: Performing Nunit we need to declare Test class and Test attribute for function

Step-5: Build and execute the test project

Step-6: There are 2 ways of running Unit test:

1)Through Test Explorer in Visual Studio: Click on Test tab → Select Windows tab → Click on Test Explorer

After this a new pane will open on the left side which will contain all the test cases found by the test explorer. These are the test cases which have attributes [Test] above them in a test fixture in Unit testing projects.

The NUnit 3 Test Adapter which we installed earlier, allows you to run NUnit 3 tests inside Visual Studio.

2)Through Command Prompt: Note: Always Open Command prompt in administrator mode for running NUnit console command-line test cases

There are 3 ways of Testing:

  1. Test by using Default parameters
  2. Test by providing parameters
  3. Test by providing invalid parameters

1) Change directory to installation directory of NUnit console

eg: C:\WINDOWS\system32>cd C:\Program Files (x86)\NUnit.org\nunit-console

2) Sample test case

C:\Program Files (x86)\NUnit.org\nunit-console>nunit3-console.exe
--testparm:hotelbookingid=AIR
--test:"HotelBooking.DataLayer.Tests.HotelSeriviceTests.GetBookingSystemSettings"
"E:\PROJECTS\UnitTests\HotelBooking.DataLayer.Tests\bin\Debug\HotelBooking.DataLayer.Tests.dll"

Where,

--test param → parameters to be passed to test case

(If multiple parameters need to be passed following the same syntax for each param with space in between and if no parameters are required then skip this)

--test → namespace of the test case class which contain program filename and test method name"E:\PROJECTS\UnitTests\HotelBooking.DataLayer.Tests\bin\Debug\HotelBooking.DataLayer.Tests.dll" → DLL path to Unit testing project folder

If we don’t pass any test parameters, then the tests will run with default parameters given in the code. Conclusion

In this article, we explained how to write Unit tests for Rest API and the purpose was to get an idea about how Unit tests are written and executed and a major advantage of this Unit testing is that we know the actual rest request failed reason.

Originally published at https://www.neovasolutions.com on May 11, 2020.

--

--

Neova Solutions

We transform ideas into beautiful products. Since 2007, we are empowering startups to build disruptive products that are feature-rich and robust.