👉🏼 Click here to Join I ❤️ .NET WhatsApp Channel to get 🔔 notified about new articles and other updates.
Functional testing your ASP.NET WEB API

Functional testing your ASP.NET WEB API


18 Articles


In this article, let's learn about how to do Functional Test in WebAPI in ASP.NET Core.

Table of Contents

  1. Introduction
  2. Why Functional Testing?
  3. Prerequisite
  4. Steps to do Functional Testing
  5. Summary


Functional Testing ensure that an app's components function correctly at a level that includes the app's supporting infrastructure, such as the database, file system, and network. The main difference between functional testing and unit testing is that application's infrastructure components like database, file system, etc are mocked with unit tests. But with functional testing, we want to ensure that the whole app is working as expected with all of these components combined together. ASP.NET Core supports functional tests using a unit test framework with a test web host and an in-memory test server.

Why Functional Testing ?

Functional tests evaluate an app's components on a broader level than unit tests. Unit tests are used to test isolated software components, such as individual class methods. Functional tests confirm that two or more app components work together to produce an expected result, possibly including every component required to fully process a request.

The characteristics of functional tests include:

  • Use the actual components that the app uses in production.
  • Require more code and data processing.
  • Take longer to run.

Therefore, limit the use of functional tests to the most important infrastructure scenarios. If a behavior can be tested using either a unit test or an functional test, choose the unit test.


You need to have an ASP.NET Core WebAPI project. If you don't have one, you can create one from Visual Studio.

Configure new project

We are going to learn this with .NET 7 Web API project.

New project

The default project template contains a WeatherForecastController. We are going to use this controller for our functional testing. Here is the API response when we run the project.

API response

Steps to do Functional Testing

Let's see the steps to do functional testing in ASP.NET Core Web API.

  1. Create a XUnit Test Project.

    Create XUnit Project
  2. Add Package Reference to Nuget Package Microsoft.AspNetCore.Mvc.Testing.
  3. Optionally make sure you have all Nuget Packages updated to latest version in all your projects.
  4. Add Project Reference to your API project in your test project.

    Test project with Nuget and Project Reference
  5. Now we need to expose the implicitly defined Program class to the test project by adding public partial class Program { } to the end of Program.cs in Web API.

    Add partial modifier to Program
  6. WebApplicationFactory<TEntryPoint> is used to create a TestServer for the functional tests. TEntryPoint is the entry point class of the SUT, usually Program.cs.
  7. We can add CustomeWebApplicationFactory.cs in our test project to create a custom WebApplicationFactory<TEntryPoint>. We can then add a static RunTest helper method with Func<HttpClient, Task> delegate to run the test. This method will create a TestServer and HttpClient instance and then call the delegate with the HttpClient instance. The TestServer and HttpClient instances are disposed after the delegate completes. This is shown in the below code.

    Code Sample - Custom Web Application Factory

That's it we are all set. Now its time to write our first functional test. Let's write a test to check if the API returns the expected response.

We can add a new class WeatherForecastControllerTests.cs in our test project and add the following code.

Code Sample - First Functional Test

In the above code, we are inheriting from CustomeWebApplicationFactory and the call RunTest helper method from within our test passing http client instance. This will make a HTTP GET call to weatherforecast endpoint and deserialize the response to List<WeatherForecast>. We are then asserting the status code and count of our response. We can also assert the values of the response in the same way.

The tests will run in the following flow.

  1. Start Test Method.
  2. Create HTTP Client.
  3. Create WebApplicationFactory.
  4. Run Program.cs.
  5. Service Registration.
  6. Build Web Application.
  7. Test Code Execution.

Now lets run the test and see the result.

Passing Functional Test

The tests are passing. We can also write tests to check if the API returns the expected response for different HTTP methods like POST, PUT, DELETE, etc. in the same way.


In this article, we learnt about what is funtional test and how to write functional test in ASP.NET Web API. Functional test help us to test end to end functionality of our application without mocking any infrastructure dependencies. This is very useful to test the application in real world scenario. In our next article we will learn about how to do functional testing in ASP.NET Web API with database.

👉🏼 Click here to Join I ❤️ .NET WhatsApp Channel to get 🔔 notified about new articles and other updates.
  • Webapi
  • Functional Test
  • Integration Test