How To Integrate Swagger With .NET Core WebAPI Application

Today, we are going to learn how to integrate swagger with .NET Core Web API application. In addition, when we host this API and we want to test the API with automation, we use Postman. To get this automated, we use Newman.
 
Postman is widely used for API testing. This is one of the best and most easily available tools.
 
You can download the latest version of Postman from here.
 
You can install Newman from here. Here, all the instructions are given for ease of the users.
 
Let’s start demo of implementation of swagger and testing with Postman, with Newman automation.
 
First, we will create a .NET Core Web API application.
 
How To Integrate Swagger With .NET Core WebAPI Application
 
If we run this application, it should be successfully running.
 
You can read more about swagger and Open API Specification from here. Now, I am considering you have the basic idea of swagger definition.
 
Let’s integrate Swagger in the Web API application. I am going to use the swashbuckle NuGet package for creating swagger in Web API.
 
You can get more info on installing which package version and how to configure Web API with swagger at the below link.
 
Now, considering that project is up and running with mentioned code changes and Swagger is being generated, it should be accessible using swagger URL, as mentioned below.
 
How To Integrate Swagger With .NET Core WebAPI Application
 
We can import this swagger in postman using the import functionality of postman tool.
 
How To Integrate Swagger With .NET Core WebAPI Application
 
Paste swagger JSON inside Paste Raw text box. Click on Import.
 
How To Integrate Swagger With .NET Core WebAPI Application
 
After importing Swagger it will create test cases, as below.
 
How To Integrate Swagger With .NET Core WebAPI Application
 
Now, we will add an environment to automate your APIs for different environments, like “Local”, ”Dev”, ” QA”, ”UAT”, and ”PROD”.
 
Click the “Settings” button in the top-right corner highlighted with yellow color.
 
How To Integrate Swagger With .NET Core WebAPI Application
 
Click “Add” on below screen.
 
How To Integrate Swagger With .NET Core WebAPI Application
 
Here, you can create Global variables, Environments, and environment variables as per the environment.
 
How To Integrate Swagger With .NET Core WebAPI Application
 
Click the "Add" button to save.
 
You can select an environment for which you want to run the test cases. Also, use the created environment variables as highlighted in “{{url}}”.
 
How To Integrate Swagger With .NET Core WebAPI Application
 
Postman Tests are JavaScript codes added to requests that help you verify results such as successful or failed status, comparison of expected results, etc. It usually starts with “pm.test”. It can be compared to asserts, verify commands available in other tools.
 
Let's create some basic tests for our parameterize requests.
 
How To Integrate Swagger With .NET Core WebAPI Application
 
Now let us execute this test case and check whether test script executes properly and gives us the desired output.
 
How To Integrate Swagger With .NET Core WebAPI Application
 
You can view output in the response window.
 
Now it’s time to use Newman to automate it. As already explained
 
First export environment variables and test scripts as json.
 
Click to “Settings” icon in the right corner and you can download JSON file for environment variables.
 
How To Integrate Swagger With .NET Core WebAPI Application
 
Now, export test scripts from the collection.
 
How To Integrate Swagger With .NET Core WebAPI Application
 
How To Integrate Swagger With .NET Core WebAPI Application
 
Once you have saved both files to local folder/directory, you can execute them using PowerShell as below.
 
How To Integrate Swagger With .NET Core WebAPI Application
 
 Newman run “testscript file” – e “environment file”
 
Below output shows the test case result.
 
How To Integrate Swagger With .NET Core WebAPI Application
 
This way we can include this PowerShell Newman script in any CI/CD tool or we can run locally to check the status of running test cases across all environments. This helps to monitor the health of APIs.
 
This concludes test case automation with Postman and Newman.


Similar Articles