Take advantage of xUnit and ASP.Net Core’s test host package to run integration tests on your ASP.Net Core web applications Credit: Thinkstock Testing is an essential part of developing any application. There are various types of tests. Unit tests are used to check if the output of blocks or units of code conforms to the desired results. Integration tests are used to check if the different parts of the application work as expected when they are assembled together. In this article I will discuss how we can write and execute integration tests using ASP.Net Core. Create a new ASP.Net Core project First off, let’s create an ASP.Net Core project in Visual Studio. Assuming that .Net Core is already installed on your system, follow these steps to create an ASP.Net Core application in Visual Studio 2017. In the Visual Studio IDE, click on File > New > Project. Select “ASP.Net Core Web Application (.Net Core)” from the list of the templates displayed. Specify a name for the project. Click OK to save the project. In the “New .Net Core Web Application…” window, select “Web API.” Ensure that “Enable Docker Support” is unchecked. Select “No Authentication” as we won’t be using authentication in this example Click OK. A new ASP.Net Core project will then be created with an example Controller to build and execute RESTful HTTP services. Next, create a simple controller named DefaultController and enter the following code. [Produces(“application/json”)] [Route(“api/Default”)] public class DefaultController : Controller { // GET: api/Default [HttpGet] public string Get() { return “Hello World!”; } } Now that we have our controller, we’re ready to dive in. We’ll look at how we can write integration tests for this controller in the next section. Create an xUnit test project in ASP.Net Core Unit testing using xUnit, Moq, and Fluent Assertions is both useful and fun, and the newer versions of .Net Core support them. In this example, we’ll take advantage of xUnit. Now that our ASP.Net Core Web project has been created, let’s create a test project to write our integration tests. The first step is to create a unit test project, by following these steps. In the Visual Studio IDE, click on File > New > Project. Select “.NET Core” from the list of installed templates. Select “xUnit Test Project (.Net Core)” from the list of the templates displayed to the right. Specify a name for the project. Click OK to save. This will create a unit test project and a default test file. Next, rename this default test file to DefaultTests.cs. To unit test your Web API methods, you should have a web server running. If you have your web and test projects in the same solution, you will typically want to invoke two instances of Visual Studio and run each project in a separate instance. Here’s exactly where a test server comes to the rescue. Install the test host in ASP.Net Core ASP.Net Core includes a handy test host that you can add to your integration test projects via NuGet. To install this test host package, select the integration test project in the Solution Explorer window, then right click and select “Manage NuGet Packages…” Then click “Browse,” search for the Microsoft.AspNetCore.TestHost package, and install it. The following code snippet shows how you can create an instance of TestServer and use it to create an instance of HttpClient. public class DefaultTests { private readonly TestServer testServer; private readonly HttpClient httpClient; public DefaultTests() { testServer = new TestServer(new WebHostBuilder() .UseStartup<Startup>()); httpClient = testServer.CreateClient(); } } Use the .Net Core TestServer to run the xUnit test method The code snippet given below shows how you can use the TestServer to run your unit test method. Here the GetMessageTest unit test method reads a string asynchronously and then checks to see if the string object is null. [Fact] public async void GetMessageTest() { var response = await httpClient.GetAsync(“http://localhost:45660/api/default”); response.EnsureSuccessStatusCode(); var result = await response.Content.ReadAsStringAsync(); Assert.NotNull(result); } And here is the complete DefaultTests test class for your reference. public class DefaultTests { private readonly TestServer testServer; private readonly HttpClient httpClient; public DefaultTests() { testServer = new TestServer(new WebHostBuilder() .UseStartup<Startup>()); httpClient = testServer.CreateClient(); } [Fact] public async void GetMessageTest() { var response = await httpClient.GetAsync(“http://localhost:45660/api/default”); response.EnsureSuccessStatusCode(); var result = await response.Content.ReadAsStringAsync(); Assert.NotNull(result); } } Note that it is good practice to isolate the unit tests and integration tests of your application. The reason is that while unit tests often use fakes and mocks, integration tests often need to incorporate the infrastructure concerns, i.e., database resources, file system resources, web requests and responses, etc. Also, you should aim to write fewer integration tests than unit tests as integration tests tend to run much longer. Don’t use an integration test when a unit test will do. Related content feature What is Rust? Safe, fast, and easy software development Unlike most programming languages, Rust doesn't make you choose between speed, safety, and ease of use. Find out how Rust delivers better code with fewer compromises, and a few downsides to consider before learning Rust. By Serdar Yegulalp Nov 20, 2024 11 mins Rust Programming Languages Software Development how-to Kotlin for Java developers: Classes and coroutines Kotlin was designed to bring more flexibility and flow to programming in the JVM. Here's an in-depth look at how Kotlin makes working with classes and objects easier and introduces coroutines to modernize concurrency. By Matthew Tyson Nov 20, 2024 9 mins Java Kotlin Programming Languages analysis Azure AI Foundry tools for changes in AI applications Microsoft’s launch of Azure AI Foundry at Ignite 2024 signals a welcome shift from chatbots to agents and to using AI for business process automation. By Simon Bisson Nov 20, 2024 7 mins Microsoft Azure Generative AI Development Tools news Microsoft unveils imaging APIs for Windows Copilot Runtime Generative AI-backed APIs will allow developers to build image super resolution, image segmentation, object erase, and OCR capabilities into Windows applications. By Paul Krill Nov 19, 2024 2 mins Generative AI APIs Development Libraries and Frameworks Resources Videos