Read appsettings JSON in .NET Core Test Project -II

Today in this article, we shall continue to learn one more approach to perform Read appsettings JSON in .NET Core Test Project like Integration Test or Functional Project. In our last article, we learned the simple approach to read JSON or XML files for the Unit test projects.

Today in this article, we will cover below aspects,

Please visit Part I of the below article,

Read appsettings JSON in .NET Core Test Project

We shall see overall below approaches for the integration TEST project project

  • Read appsettings JSON for Integration Test or Functional test project
  • Read custom JSON/XML test data for Integration or Functional Test project

Let’s create a Test Project and name it as BooksService.Integration.Tests.

Please add a TestClass named as BooksServiceIntegrationTests

public class BooksServiceIntegrationTests : IClassFixture<AppTestFixture>
    {
        readonly AppTestFixture _fixture;
        readonly HttpClient _client;
        public BooksServiceIntegrationTests(AppTestFixture fixture)
        {
            _fixture = fixture;
            _client = _fixture.CreateClient();
        }

My test data is defined as below in the test project,

Read appsettings JSON in NET Core Test Project

Below is how I have defined a test fixture method that loads the required test data,

Loading regular Apsettings.json

In the XUnit framework constructor gets called once before running any of the test cases.

Lets us now define and initialize IConfiguration as below. The below technique lets you load the Application-specific configuration easily. Please make sure to set up the “copy to the output directory” properties, as explained in the article.

public IConfiguration GetTestDataConfiguration()
        {
            return new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile(@"apsettings.json", true, false)
                .AddEnvironmentVariables()
                .Build();
        }

Loading Custom Apsettings.json

Loading custom apsettings.json can be done using the below logic,

public IConfiguration GetTestDataConfiguration()
        {
            return new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile(@"Request\Request_ID_1.json", false, false)
                .AddJsonFile(@"Response\Response_ID_1.json", true, false)
                .AddEnvironmentVariables()
                .Build();
        }

Here AppTestFixture uses the Startup class to lanch in-memory instance of the Target service.

So we are done with Test Host setup and provision to create a client against that server.

Let’s now see how to create integration tests that use test data from configuration files and perform testing.

This test data can be made available in the custom JSON/XML file or from.

An instance of the fixture data is initialized just before the first test in the class is run and hence can be used to associate test data required for each test class and their all methods or required test methods as needed.

Class fixtures have a single parameterless constructor and may take collection fixture types as constructor arguments.

Read appsettings JSON Test project

Reference :

Do you have any comments or ideas or any better suggestions to share?

Please sound off your comments below.

Happy Coding !!



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.



Leave a Reply

Your email address will not be published. Required fields are marked *