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.
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
.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.
And here is the complete
DefaultTests
test class for your reference.
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.
https://www.infoworld.com
No comments:
Post a Comment