Read appsettings JSON in .NET Core Test Project
Today in this article, we shall learn how to Read appsettings JSON in .NET Core Test Project. This could be a genuine need for accessing the configuration files in the TEST project from time to time.
Today in this article, we will cover below aspects,
The below example shows how I am able to load the apsetting.json in the test project. This apsettings.json actually belongs to the software under test. i.e. Main API project for which we are trying to write effective test cases.
Please make sure in your main project this apsettings.json is set to copy to the output directory location by default.
Read appsettings.JSON configuration in Unit Test project
As a general thumb rule, Unit test cases should be stateless as possible. And most times we mock everything needed for test cases as dependencies. But sometimes if you are following data-driven tests, then you might want to load some data from aspetting.json or XML as required.
Let’s read the below-given sample values from apseetings.json in one of our Unit test cases.
appsettings.json
{
"Books": {
"Price": "23.4",
"Category": "Software",
"Author": "John Murph"
}
}
If you are using XUnit as a Test framework then please define the constructor. This constructor will act as [TestInitialize] similar to MSTest.
In the XUnit framework constructor gets called once before running any of the test cases and helps you define any test data required for the test execution if any.
Lets us now define the Constructor and initialize IConfiguration,
Once executed, all the apsettings.json values will be available as kay value pairs in the IConfiguration object.
Read custom JSON/XML in Unit Test project
Reading apsettings.json was pretty straightforward as explained above. If you have any custom JSON or XML files, you can very much load them in a similar way as defined above.
We shall now add custom JSON to our test project itself and try to access it in the Test cases.
Please set the Copy to Output directory property as Copy always or Copy if newer.
Next please set the JSON file details within ConfigurationBuilder as below,
public class BooksServiceTests
{
private readonly IConfiguration _configuration;
public BooksServiceTests()
{
_configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile(@"TestData/BookTestData.json", false, false)
.AddEnvironmentVariables()
.Build();
}
...
}
Reading XML or INI files using Configuration Builder
Reading XML or INI files using Configuration Builder is simple by adding the required provider which is explained in the below article in detail,
The above-discussed approach can be used in Integration Testing or Functional Test also. But there are better approaches i.e. Using Test Fixtures to define the proper test data context.
Please refer to the below article for more details,
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.